Guest User

Untitled

a guest
Oct 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. # -*- encoding: utf-8 -*-
  2.  
  3. # 2012/6/3 tddbc大阪2.0 課題の回答
  4. # 言語: Ruby
  5.  
  6. # 自動販売機クラス
  7. # インタフェイス
  8. # add_amount : コイン投入(状態変更)
  9. # haraimodosi : 払い戻しする(状態変更)
  10. # total_amount : 投入金額返す
  11. # turisen : つり銭溜まってる金額を返す
  12. # sales_amount : 売上金額返す
  13. # list_juice : 保持してるジュース情報出力
  14. # juice_count : ジュースの本数返す
  15. # buy : 購入(状態変更)
  16. # buy? : 買えるか?
  17. class VendingMachine
  18. VALID_AMOUNT = [10, 50, 100, 500, 1000]
  19. attr_reader :total_amount, :turisen, :sales_amount
  20.  
  21. def initialize
  22. @total_amount = 0
  23. @turisen = 0
  24. @product_hash = {'コーラ' => {:price => 120, :count => 5}}
  25. @sales_amount = 0
  26. end
  27.  
  28. def haraimodosi
  29. @turisen += @total_amount
  30. @total_amount = 0
  31. end
  32.  
  33. def add_amount(amount)
  34. if valid_amount?(amount)
  35. @total_amount += amount
  36. else
  37. @turisen += amount
  38. end
  39. end
  40.  
  41. def valid_amount?(amount)
  42. VALID_AMOUNT.include?(amount)
  43. end
  44.  
  45. def list_juices
  46. @product_hash
  47. end
  48.  
  49. def juice_count(product_name)
  50. @product_hash[product_name][:count]
  51. end
  52.  
  53. def buy(product_name)
  54. return unless buy?(product_name)
  55. @sales_amount += @product_hash[product_name][:price]
  56. @total_amount -= @product_hash[product_name][:price]
  57. @product_hash[product_name][:count] -= 1
  58. end
  59.  
  60. def buy?(product_name)
  61. enough_amount?(product_name) &&
  62. product_exist?(product_name)
  63. end
  64.  
  65. def enough_amount?(product_name)
  66. @product_hash[product_name][:price] <= total_amount
  67. end
  68.  
  69. def product_exist?(product_name)
  70. 0 < @product_hash[product_name][:count]
  71. end
  72. end
  73.  
  74.  
  75. case $0
  76. when __FILE__
  77. when /spec[^\/]*$/
  78. describe "VendingMachine" do
  79.  
  80. before do
  81. @vm = VendingMachine.new
  82. end
  83.  
  84. context "初期状態" do
  85. it "初期状態では投入金額が0円" do
  86. @vm.total_amount.should == 0
  87. end
  88.  
  89. it "初期状態ではつり銭は0円" do
  90. @vm.turisen.should == 0
  91. end
  92.  
  93. it "払い戻し処理すると、投入金額は0円になる" do
  94. @vm.haraimodosi
  95. @vm.total_amount.should == 0
  96. end
  97. end
  98.  
  99. context "10円投入" do
  100. before do
  101. @vm.add_amount(10)
  102. end
  103.  
  104. it "10円を導入すると投入金額は10円になる" do
  105. @vm.total_amount.should == 10
  106. end
  107.  
  108. it "10円を投入して、払い戻し処理すると、つり銭は10円" do
  109. @vm.haraimodosi
  110. @vm.turisen.should == 10
  111. end
  112.  
  113. it "10円を投入して、払い戻し処理すると、投入金額は0円になる" do
  114. @vm.haraimodosi
  115. @vm.total_amount.should == 0
  116. end
  117. end
  118.  
  119. context "500円投入" do
  120. before do
  121. @vm.add_amount(500)
  122. end
  123.  
  124. it "500円を導入すると投入金額は500円になる" do
  125. @vm.total_amount.should == 500
  126. end
  127.  
  128. it "500円を投入して、払い戻し処理すると、つり銭は500円" do
  129. @vm.haraimodosi
  130. @vm.turisen.should == 500
  131. end
  132.  
  133. it "500円を投入して、払い戻し処理すると、投入金額は0円になる" do
  134. @vm.haraimodosi
  135. @vm.total_amount.should == 0
  136. end
  137. end
  138.  
  139. context "複数回投入" do
  140. before do
  141. @vm.add_amount(50)
  142. @vm.add_amount(100)
  143. end
  144.  
  145. it "50円と100円投入すると、投入金額は150円" do
  146. @vm.total_amount.should == 150
  147. end
  148.  
  149. it "50円と100円投入して、払い戻し処理すると、つり銭は150円" do
  150. @vm.haraimodosi
  151. @vm.turisen.should == 150
  152. end
  153.  
  154. it "50円と100円投入して、払い戻し処理すると、投入金額は0円になる" do
  155. @vm.haraimodosi
  156. @vm.total_amount.should == 0
  157. end
  158. end
  159.  
  160. context "金額投入" do
  161. it "10円50円100円500円1000円投入すると、投入金額は1660円になる" do
  162. [10, 50, 100, 500, 1000].each {|yen|
  163. @vm.add_amount(yen)
  164. }
  165. @vm.total_amount.should == 1660
  166. end
  167. end
  168.  
  169. context "1円投入する" do
  170. before do
  171. @vm.add_amount(1)
  172. end
  173.  
  174. it "投入金額0円" do
  175. @vm.total_amount.should == 0
  176. end
  177.  
  178. it "つり銭1円" do
  179. @vm.turisen.should == 1
  180. end
  181. end
  182.  
  183. context "1円2回投入する" do
  184. it "つり銭2円" do
  185. @vm.add_amount(1)
  186. @vm.add_amount(1)
  187. @vm.turisen.should == 2
  188. end
  189. end
  190.  
  191. context "1円と100円投入する" do
  192. it "払い戻しするとつり銭101円" do
  193. @vm.add_amount(1)
  194. @vm.add_amount(100)
  195. @vm.haraimodosi
  196. @vm.turisen.should == 101
  197. end
  198. end
  199.  
  200. context "5円投入する" do
  201. before do
  202. @vm.add_amount(5)
  203. end
  204.  
  205. it "投入金額0円" do
  206. @vm.total_amount.should == 0
  207. end
  208.  
  209. it "つり銭5円" do
  210. @vm.turisen.should == 5
  211. end
  212.  
  213. end
  214.  
  215. context "ジュースの管理" do
  216. it "初期状態でジュースの情報を取得" do
  217. @vm.list_juices.should == {'コーラ' => {:price => 120, :count => 5}}
  218. end
  219.  
  220. it "初期状態で、コーラの本数を尋ねると、5本と返す" do
  221. @vm.juice_count('コーラ').should == 5
  222. end
  223. end
  224.  
  225. context "購入" do
  226. it "初期状態で、売上は0円" do
  227. @vm.sales_amount.should == 0
  228. end
  229.  
  230. def buy_cola?
  231. @vm.buy?('コーラ')
  232. end
  233.  
  234. def buy_cola
  235. @vm.buy('コーラ')
  236. end
  237.  
  238. it "初期状態で、コーラは購入できない" do
  239. buy_cola?.should == false
  240. end
  241.  
  242. it "120円投入すると、コーラを購入できる" do
  243. @vm.add_amount(100)
  244. @vm.add_amount(10)
  245. @vm.add_amount(10)
  246. buy_cola?.should == true
  247. end
  248.  
  249. it "200円投入すると、コーラを購入できる" do
  250. @vm.add_amount(100)
  251. @vm.add_amount(100)
  252. buy_cola?.should == true
  253. end
  254.  
  255. context "投入金額足りない" do
  256. it "購入操作をおこなっても売上金額は0円" do
  257. buy_cola
  258. @vm.sales_amount.should == 0
  259. end
  260.  
  261. it "購入操作をおこなってもコーラの本数は5本" do
  262. buy_cola
  263. @vm.juice_count('コーラ').should == 5
  264. end
  265. end
  266.  
  267.  
  268. context "コーラ一本購入" do
  269. before do
  270. @vm.add_amount(100)
  271. @vm.add_amount(10)
  272. @vm.add_amount(10)
  273. buy_cola
  274. end
  275.  
  276. it "コーラ一本購入すると売上は120円" do
  277. @vm.sales_amount.should == 120
  278. end
  279.  
  280. it "コーラ一本購入すると、コーラの本数は4本" do
  281. @vm.juice_count('コーラ').should == 4
  282. end
  283.  
  284. it "コーラ一本購入すると、投入金額は120円減る" do
  285. @vm.total_amount.should == 0
  286. end
  287.  
  288. it "コーラ一本購入すると、ジュース情報取得すると、コーラの本数は4本" do
  289. @vm.list_juices['コーラ'][:count].should == 4
  290. end
  291. end
  292. end
  293.  
  294. end
  295. end
Add Comment
Please, Sign In to add comment