vending_machine 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/machine/machine.rb +7 -4
- data/lib/vending_machine/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '08a9750307c60476f6c4e68a1f2edf9800d0c944791e1e5c2dd1157d1a8c3fe6'
|
|
4
|
+
data.tar.gz: c03495d5304e6cff454a8a45aaf011c669db8113ab1653489af1fd1455829577
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d80412d3e605d85c44d786be06ed3db144d4d234fafe85cbd775fd9157896ee89cd79a542a496e996d58f00143f9a7545934365ddb2719c4b76d57ca795739f7
|
|
7
|
+
data.tar.gz: ec5810e1e15681af351b79ff6bdd5542ee1b7349c4ef81ad919f3487314ad70b60210cc12457870c78db5b068effc98ca7a568fb05c23bf2184ff978aefb8996
|
data/README.md
CHANGED
|
@@ -99,7 +99,7 @@ machine.select_product(product: "Snickers", coins: ["€2"])
|
|
|
99
99
|
### Running the machine locally
|
|
100
100
|
Simply run `gem install vending_machine`.
|
|
101
101
|
|
|
102
|
-
Then call a
|
|
102
|
+
Then call a Ruby console of your choice (for example `pry` or `irb`)
|
|
103
103
|
|
|
104
104
|
Requiring the installed gem will then let you interact with it.
|
|
105
105
|
|
|
@@ -117,4 +117,4 @@ This machine comes preloaded with some change and products for testing.
|
|
|
117
117
|
```
|
|
118
118
|
|
|
119
119
|
### Tests
|
|
120
|
-
To run the tests simply `cd` into the root of the directory and run `rake`
|
|
120
|
+
To run the tests simply `cd` into the root of the directory and run `rake`
|
data/lib/machine/machine.rb
CHANGED
|
@@ -35,11 +35,12 @@ class Machine
|
|
|
35
35
|
private def find_product(product:, cash_values:)
|
|
36
36
|
product_price = product.cash_value
|
|
37
37
|
if correct_change_given?(product_price: product_price, coins: cash_values)
|
|
38
|
-
|
|
38
|
+
increment_available_change(cash_values)
|
|
39
39
|
decrement_available_products(product)
|
|
40
40
|
return product_and_change(product)
|
|
41
41
|
end
|
|
42
42
|
raise OutOfMoneyError if remaining_change.empty?
|
|
43
|
+
increment_available_change(cash_values)
|
|
43
44
|
change = change(product_price: product_price, coins: cash_values)
|
|
44
45
|
coins = calculate_coin_denominations(change: change)
|
|
45
46
|
decrement_available_products(product)
|
|
@@ -55,9 +56,7 @@ class Machine
|
|
|
55
56
|
|
|
56
57
|
private def correct_change_given?(product_price:, coins: [])
|
|
57
58
|
raise ArgumentError unless coins.is_a? Array
|
|
58
|
-
|
|
59
|
-
change = change(product_price: product_price, coins: coins)
|
|
60
|
-
change <= 0
|
|
59
|
+
true if exact_change?(coins: coins) && (product_price - coins.first).zero?
|
|
61
60
|
end
|
|
62
61
|
|
|
63
62
|
private def change(product_price:, coins:)
|
|
@@ -127,6 +126,10 @@ class Machine
|
|
|
127
126
|
change_array.each { |change| available_change[change] -= 1 }
|
|
128
127
|
end
|
|
129
128
|
|
|
129
|
+
private def increment_available_change(cash)
|
|
130
|
+
cash.each { |coin| available_change[coin] += 1 }
|
|
131
|
+
end
|
|
132
|
+
|
|
130
133
|
private def decrement_available_products(product)
|
|
131
134
|
products[product.name][:available] -= 1
|
|
132
135
|
end
|