Created
July 9, 2011 09:02
-
-
Save akitsukada/1073452 to your computer and use it in GitHub Desktop.
2011/7/9 TDDBC Tokyo で高井さんに教えてもらいながら一緒に書いたコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class Money | |
def initialize amount = 0 | |
@amount = amount | |
end | |
def to_i | |
@amount | |
end | |
YEN_10000 = Money.new 10000 | |
YEN_5000 = Money.new 5000 | |
YEN_2000 = Money.new 2000 | |
YEN_1000 = Money.new 1000 | |
YEN_500 = Money.new 500 | |
YEN_100 = Money.new 100 | |
YEN_50 = Money.new 50 | |
YEN_10 = Money.new 10 | |
YEN_5 = Money.new 5 | |
YEN_1 = Money.new 1 | |
end | |
class VendingMachine | |
attr_reader :posted_total | |
attr_accessor :randomizer | |
def initialize | |
@posted_total = 0 | |
@stock = Hash.new | |
@randomizer = -> { rand(100) <= 5 } | |
end | |
def post money | |
case money.to_i | |
when 1, 5, 2000, 5000, 10000 | |
raise | |
else | |
@posted_total += money.to_i | |
end | |
end | |
def add_stock item, num | |
@stock[item.id] = [item, num] | |
end | |
def purchasable_ids | |
ret = [] | |
@stock.each do |id, pair| | |
item, num = pair | |
if @posted_total > item.price | |
ret << item.id | |
end | |
end | |
ret | |
end | |
def sell_item id | |
item, num = @stock[id] | |
sn = unless num == 1 | |
@randomizer.call ? 2 : 1 | |
else | |
1 | |
end | |
if (num - sn) >= 0 | |
@stock[id] = [item, num - sn] | |
else | |
raise | |
end | |
end | |
def num_stock | |
@stock.inject(0) do |r, ary| | |
r += ary.last.last | |
end | |
end | |
end | |
class Item | |
attr_accessor :id | |
attr_accessor :name | |
attr_accessor :price | |
def initialize id, name, price | |
@id = id | |
@name = name | |
@price = price | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'vending_machine' | |
describe Money do | |
context "1000円札" do # thousand_yen_bill | |
describe "#initialize" do | |
it "初期化" do | |
Money::YEN_1000.should be_kind_of Money | |
Money::YEN_1000.to_i.should eql 1000 | |
end | |
end | |
end | |
end | |
describe VendingMachine do | |
context "自動販売機" do | |
before do | |
@vm = VendingMachine.new | |
end | |
describe "#posted_total" do | |
it "投入金額が返ること" do | |
@vm.posted_total.should eql 0 | |
end | |
end | |
describe "#post" do | |
it "お金を投入できる" do | |
tyb = Money::YEN_1000 | |
@vm.post(tyb) | |
@vm.posted_total.should eql 1000 | |
end | |
it "1円を投入できない" do | |
oyc = Money::YEN_1 | |
lambda{@vm.post(oyc)}.should raise_exception | |
end | |
it "5円を投入できない" do | |
fyc = Money::YEN_5 | |
lambda{@vm.post(fyc)}.should raise_exception | |
end | |
end | |
describe "#num_stock" do | |
it "在庫数が算出される" do | |
@vm.num_stock.should eql 0 | |
end | |
end | |
context "在庫が1この場合" do | |
before do | |
cola = Item.new(1, 'コーラ',120) | |
@vm.add_stock(cola, 1) | |
end | |
it "あたりが出ない" do | |
@vm.randomizer = -> {true} | |
@vm.post Money::YEN_1000 | |
@vm.sell_item 1 | |
@vm.num_stock.should eql 0 | |
end | |
end | |
context "在庫が5個ある場合" do | |
before do | |
cola = Item.new(1, 'コーラ',120) | |
@vm.add_stock(cola, 5) | |
end | |
describe "#add_stock" do | |
it "在庫が追加できる" do | |
@vm.num_stock.should eql 5 | |
end | |
end | |
describe "#purchasable_ids" do | |
it "現在買える商品のIDのリストが返る" do | |
@vm.purchasable_ids.should eql [] | |
@vm.post Money::YEN_100 | |
@vm.purchasable_ids.should eql [] | |
@vm.post Money::YEN_100 | |
@vm.purchasable_ids.should eql [1] | |
end | |
end | |
describe "#sell_item" do | |
it "商品を買える" do | |
@vm.randomizer = -> {false} | |
@vm.post Money::YEN_1000 | |
@vm.sell_item 1 | |
@vm.num_stock.should eql 4 | |
end | |
it "あたりがでる" do | |
@vm.randomizer = -> {true} | |
@vm.post Money::YEN_1000 | |
@vm.sell_item 1 | |
@vm.num_stock.should eql 3 | |
end | |
end | |
end | |
describe "#change_total" do | |
it "お釣り用の総額を" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment