Skip to content

Instantly share code, notes, and snippets.

@kmdsbng
Created June 4, 2012 00:21
Show Gist options
  • Select an option

  • Save kmdsbng/2865590 to your computer and use it in GitHub Desktop.

Select an option

Save kmdsbng/2865590 to your computer and use it in GitHub Desktop.
# -*- encoding: utf-8 -*-
# 2012/6/3 tddbc大阪2.0 課題の回答
# 言語: Ruby
# 自動販売機クラス
# インタフェイス
# add_amount : コイン投入(状態変更)
# haraimodosi : 払い戻しする(状態変更)
# total_amount : 投入金額返す
# turisen : つり銭溜まってる金額を返す
# sales_amount : 売上金額返す
# list_juice : 保持してるジュース情報出力
# juice_count : ジュースの本数返す
# buy : 購入(状態変更)
# buy? : 買えるか?
class VendingMachine
VALID_AMOUNT = [10, 50, 100, 500, 1000]
attr_reader :total_amount, :turisen, :sales_amount
def initialize
@total_amount = 0
@turisen = 0
@product_hash = {'コーラ' => {:price => 120, :count => 5}}
@sales_amount = 0
end
def haraimodosi
@turisen += @total_amount
@total_amount = 0
end
def add_amount(amount)
if valid_amount?(amount)
@total_amount += amount
else
@turisen += amount
end
end
def valid_amount?(amount)
VALID_AMOUNT.include?(amount)
end
def list_juices
@product_hash
end
def juice_count(product_name)
@product_hash[product_name][:count]
end
def buy(product_name)
return unless buy?(product_name)
@sales_amount += @product_hash[product_name][:price]
@total_amount -= @product_hash[product_name][:price]
@product_hash[product_name][:count] -= 1
end
def buy?(product_name)
enough_amount?(product_name) &&
product_exist?(product_name)
end
def enough_amount?(product_name)
@product_hash[product_name][:price] <= total_amount
end
def product_exist?(product_name)
0 < @product_hash[product_name][:count]
end
end
case $0
when __FILE__
when /spec[^\/]*$/
describe "VendingMachine" do
before do
@vm = VendingMachine.new
end
context "初期状態" do
it "初期状態では投入金額が0円" do
@vm.total_amount.should == 0
end
it "初期状態ではつり銭は0円" do
@vm.turisen.should == 0
end
it "払い戻し処理すると、投入金額は0円になる" do
@vm.haraimodosi
@vm.total_amount.should == 0
end
end
context "10円投入" do
before do
@vm.add_amount(10)
end
it "10円を導入すると投入金額は10円になる" do
@vm.total_amount.should == 10
end
it "10円を投入して、払い戻し処理すると、つり銭は10円" do
@vm.haraimodosi
@vm.turisen.should == 10
end
it "10円を投入して、払い戻し処理すると、投入金額は0円になる" do
@vm.haraimodosi
@vm.total_amount.should == 0
end
end
context "500円投入" do
before do
@vm.add_amount(500)
end
it "500円を導入すると投入金額は500円になる" do
@vm.total_amount.should == 500
end
it "500円を投入して、払い戻し処理すると、つり銭は500円" do
@vm.haraimodosi
@vm.turisen.should == 500
end
it "500円を投入して、払い戻し処理すると、投入金額は0円になる" do
@vm.haraimodosi
@vm.total_amount.should == 0
end
end
context "複数回投入" do
before do
@vm.add_amount(50)
@vm.add_amount(100)
end
it "50円と100円投入すると、投入金額は150円" do
@vm.total_amount.should == 150
end
it "50円と100円投入して、払い戻し処理すると、つり銭は150円" do
@vm.haraimodosi
@vm.turisen.should == 150
end
it "50円と100円投入して、払い戻し処理すると、投入金額は0円になる" do
@vm.haraimodosi
@vm.total_amount.should == 0
end
end
context "金額投入" do
it "10円50円100円500円1000円投入すると、投入金額は1660円になる" do
[10, 50, 100, 500, 1000].each {|yen|
@vm.add_amount(yen)
}
@vm.total_amount.should == 1660
end
end
context "1円投入する" do
before do
@vm.add_amount(1)
end
it "投入金額0円" do
@vm.total_amount.should == 0
end
it "つり銭1円" do
@vm.turisen.should == 1
end
end
context "1円2回投入する" do
it "つり銭2円" do
@vm.add_amount(1)
@vm.add_amount(1)
@vm.turisen.should == 2
end
end
context "1円と100円投入する" do
it "払い戻しするとつり銭101円" do
@vm.add_amount(1)
@vm.add_amount(100)
@vm.haraimodosi
@vm.turisen.should == 101
end
end
context "5円投入する" do
before do
@vm.add_amount(5)
end
it "投入金額0円" do
@vm.total_amount.should == 0
end
it "つり銭5円" do
@vm.turisen.should == 5
end
end
context "ジュースの管理" do
it "初期状態でジュースの情報を取得" do
@vm.list_juices.should == {'コーラ' => {:price => 120, :count => 5}}
end
it "初期状態で、コーラの本数を尋ねると、5本と返す" do
@vm.juice_count('コーラ').should == 5
end
end
context "購入" do
it "初期状態で、売上は0円" do
@vm.sales_amount.should == 0
end
def buy_cola?
@vm.buy?('コーラ')
end
def buy_cola
@vm.buy('コーラ')
end
it "初期状態で、コーラは購入できない" do
buy_cola?.should == false
end
it "120円投入すると、コーラを購入できる" do
@vm.add_amount(100)
@vm.add_amount(10)
@vm.add_amount(10)
buy_cola?.should == true
end
it "200円投入すると、コーラを購入できる" do
@vm.add_amount(100)
@vm.add_amount(100)
buy_cola?.should == true
end
context "投入金額足りない" do
it "購入操作をおこなっても売上金額は0円" do
buy_cola
@vm.sales_amount.should == 0
end
it "購入操作をおこなってもコーラの本数は5本" do
buy_cola
@vm.juice_count('コーラ').should == 5
end
end
context "コーラ一本購入" do
before do
@vm.add_amount(100)
@vm.add_amount(10)
@vm.add_amount(10)
buy_cola
end
it "コーラ一本購入すると売上は120円" do
@vm.sales_amount.should == 120
end
it "コーラ一本購入すると、コーラの本数は4本" do
@vm.juice_count('コーラ').should == 4
end
it "コーラ一本購入すると、投入金額は120円減る" do
@vm.total_amount.should == 0
end
it "コーラ一本購入すると、ジュース情報取得すると、コーラの本数は4本" do
@vm.list_juices['コーラ'][:count].should == 4
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment