Skip to content

Commit 56b6587

Browse files
committed
Add additional item packs. Refactor shop_menu into it's own method
1 parent f232c0b commit 56b6587

7 files changed

Lines changed: 133 additions & 44 deletions

File tree

app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Read user input
99
loop do
10-
$stdout.print "Would you like to LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"
10+
$stdout.print "Main menu: LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"
1111

1212
input = gets.chomp
1313

lib/handle_input.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,34 @@ def initialize(order)
2020
def interpret(command)
2121
return unless ACTIONS.detect { |a| a == command }
2222

23-
if command == 'LIST'
23+
if command.match?('LIST')
2424
# list all availble items and package sizes
2525
end
2626

27-
# return shop if command == 'SHOP'
28-
if command == 'SHOP'
29-
$stdout.print "Select qty and items, example: 3 watermelon \n"
27+
return shop_menu if command.match?('SHOP')
3028

31-
loop do
32-
input = $stdin.gets.chomp
29+
return invoice.print_order(order) if command.match?('VIEW')
30+
end
3331

34-
next if input.empty?
32+
private
3533

36-
unless 'BACK'.match?(input)
37-
shop(input)
38-
next
39-
end
34+
def shop_menu
35+
$stdout.print "Type BACK at any time to return to the main menu. \nAdd qty and items to backet, example input: 3 watermelons \n"
4036

41-
$stdout.print "Returning to main menu \n"
42-
break
43-
end
44-
end
37+
loop do
38+
input = $stdin.gets.chomp
39+
40+
next if input.empty?
4541

46-
# View Invoice
47-
return invoice.print_order(order) if command == 'VIEW'
42+
break if 'BACK'.match?(input)
43+
44+
shop(input)
45+
next
46+
end
4847
end
4948

50-
PATTERN = /^\s*\d+\s*(watermelon||pineapple||rockmelon)$/
49+
# Order input pattern
50+
PATTERN = /^\s*\d+\s*(watermelons||pineapples||rockmelons)$/
5151

5252
# Order items
5353
def shop(input)

lib/item.rb

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,38 @@
44

55
# Item class
66
class Item
7+
# Products
8+
PRODUCTS = %w[watermelons pineapples rockmelons].freeze
9+
710
attr_reader :name, :pack
811

912
def initialize(name)
1013
@name = name
1114
@pack = nil
1215
end
1316

14-
def packs(fruit)
15-
if fruit == 'watermelon'
16-
threepack = Pack.new(3, Money.new(699, 'NZD'))
17-
fivepack = Pack.new(5, Money.new(899, 'NZD'))
17+
def packs(product)
18+
return unless PRODUCTS.detect { |a| a == product }
19+
20+
send(product)
21+
end
22+
23+
private
24+
25+
def watermelons
26+
[Pack.new(3, Money.new(699, 'NZD')),
27+
Pack.new(5, Money.new(899, 'NZD'))]
28+
end
29+
30+
def pineapples
31+
[Pack.new(2, Money.new(995, 'NZD')),
32+
Pack.new(5, Money.new(1695, 'NZD')),
33+
Pack.new(8, Money.new(2495, 'NZD'))]
34+
end
1835

19-
[threepack, fivepack]
20-
end
36+
def rockmelons
37+
[Pack.new(3, Money.new(595, 'NZD')),
38+
Pack.new(5, Money.new(995, 'NZD')),
39+
Pack.new(9, Money.new(1699, 'NZD'))]
2140
end
2241
end

spec/handle_input_spec.rb

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
require './lib/handle_input'
44

55
describe HandleInput do
6+
describe '#initialize' do
7+
it 'should have an order attribute which is an instance of Order' do
8+
instance = HandleInput.new(Order.new)
9+
10+
expect(instance.order).to be_a Order
11+
end
12+
13+
it 'should have an invoice attribute which is an instance of Invoice' do
14+
instance = HandleInput.new(Order.new)
15+
16+
expect(instance.invoice).to be_a Invoice
17+
end
18+
end
19+
620
describe '#interpret' do
721
it 'should return nil unless command matches a value in ACTIONS array' do
822
instance = HandleInput.new(Order.new)
@@ -15,31 +29,55 @@
1529

1630
expect(instance.interpret('VIEW')).to be_nil
1731
end
32+
33+
before do
34+
$stdin = StringIO.new('3 watermelons')
35+
$stdin = StringIO.new('BACK')
36+
end
37+
38+
it 'should return nil if command matches SHOP' do
39+
instance = HandleInput.new(Order.new)
40+
41+
expect(instance.interpret('SHOP')).to be_nil
42+
end
43+
end
44+
45+
describe '#shop_menu' do
46+
before do
47+
$stdin = StringIO.new('3 watermelons')
48+
$stdin = StringIO.new('BACK')
49+
end
50+
51+
it 'should return nil' do
52+
instance = HandleInput.new(Order.new)
53+
54+
expect(instance.send(:shop_menu)).to be_nil
55+
end
1856
end
1957

2058
describe '#shop' do
2159
before do
22-
$stdin = StringIO.new('3 watermelon')
60+
$stdin = StringIO.new('3 watermelons')
2361
end
2462

2563
it 'should return an instance of OrderLine' do
2664
instance = HandleInput.new(Order.new)
2765

28-
expect(instance.shop('10 watermelon')).to be_a OrderLine
66+
expect(instance.send(:shop, '10 watermelons')).to be_a OrderLine
2967
end
3068

3169
it 'should return nil if input does not match PATTERN' do
3270
instance = HandleInput.new(Order.new)
3371

34-
expect(instance.shop('fruit')).to be_nil
72+
expect(instance.send(:shop, 'fruit')).to be_nil
3573
end
3674
end
3775

3876
describe '#exec' do
3977
it 'should return an instance of OrderLine' do
4078
instance = HandleInput.new(Order.new)
4179

42-
expect(instance.exec(OrderLine.new(10, Item.new('watermelon')))).to be_a OrderLine
80+
expect(instance.send(:exec, OrderLine.new(10, Item.new('watermelons')))).to be_a OrderLine
4381
end
4482
end
4583
end

spec/item_spec.rb

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,58 @@
55
describe Item do
66
describe '#initialize' do
77
it 'should have a name attribute which is a String' do
8-
instance = Item.new('Watermelons')
8+
instance = Item.new('watermelons')
99

1010
expect(instance.name).to be_a String
11-
expect(instance.name).to eq 'Watermelons'
11+
expect(instance.name).to eq 'watermelons'
12+
end
13+
14+
it 'should have a pack attribute which is nil' do
15+
instance = Item.new('watermelons')
16+
17+
expect(instance.pack).to be_nil
1218
end
1319
end
1420

1521
describe '#packs' do
1622
it 'should return an Array of Packs' do
17-
instance = Item.new('Watermelons')
23+
instance = Item.new('watermelons')
1824

19-
expect(instance.packs('watermelon')).to be_a Array
25+
expect(instance.packs('watermelons')).to be_a Array
2026
end
2127
end
2228

2329
describe '#packs' do
2430
it 'should nil unless fruit is valid' do
25-
instance = Item.new('Watermelons')
31+
instance = Item.new('watermelons')
2632

2733
expect(instance.packs('hello')).to be_nil
2834
end
35+
36+
it 'should return packs available for the fruit which is an Array' do
37+
instance = Item.new('watermelons')
38+
39+
expect(instance.packs('watermelons')).to be_a Array
40+
end
41+
end
42+
43+
describe '#watermelons' do
44+
it 'should an Array' do
45+
instance = Item.new('watermelons')
46+
47+
expect(instance.send(:watermelons)).to be_a Array
48+
end
49+
50+
it 'should an Array' do
51+
instance = Item.new('pineapples')
52+
53+
expect(instance.send(:pineapples)).to be_a Array
54+
end
55+
56+
it 'should an Array' do
57+
instance = Item.new('rockmelons')
58+
59+
expect(instance.send(:rockmelons)).to be_a Array
60+
end
2961
end
3062
end

spec/order_line_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,50 @@
55
describe OrderLine do
66
describe '#initialize' do
77
it 'should have a order_qty attribute which is an Integer' do
8-
instance = OrderLine.new(3, Item.new('watermelon'))
8+
instance = OrderLine.new(3, Item.new('watermelons'))
99

1010
expect(instance.order_qty).to be_a Integer
1111
expect(instance.order_qty).to eq 3
1212
end
1313

1414
it 'should have a order_item attribute which is an instance of Item' do
15-
instance = OrderLine.new(3, Item.new('watermelon'))
15+
instance = OrderLine.new(3, Item.new('watermelons'))
1616

1717
expect(instance.order_item).to be_a Item
1818
end
1919

2020
it 'should have a order_packs attribute which is nil' do
21-
instance = OrderLine.new(3, Item.new('watermelon'))
21+
instance = OrderLine.new(3, Item.new('watermelons'))
2222

2323
expect(instance.order_packs).to be_nil
2424
end
2525
end
2626

2727
describe '#optimal' do
2828
it 'should return total_packs[0] which is an Array' do
29-
instance = OrderLine.new(12, Item.new('watermelon'))
29+
instance = OrderLine.new(12, Item.new('watermelons'))
3030

3131
expect(instance.optimal(instance.order_item.name)).to be_a Array
3232
end
3333

3434
it 'should return whole_packs[0] which is an Array' do
35-
instance = OrderLine.new(10, Item.new('watermelon'))
35+
instance = OrderLine.new(10, Item.new('watermelons'))
3636

3737
expect(instance.optimal(instance.order_item.name)).to be_a Array
3838
end
3939
end
4040

4141
describe '#present_line' do
4242
it 'should return whole_packs which is an Array' do
43-
instance = OrderLine.new(12, Item.new('watermelon'))
43+
instance = OrderLine.new(12, Item.new('watermelons'))
4444

4545
expect(instance.present_line(instance.optimal(instance.order_item.name))).to be_a Array
4646
end
4747
end
4848

4949
describe '#presenter_line_total' do
5050
it 'should return line_total which is an instance of Money' do
51-
instance = OrderLine.new(12, Item.new('watermelon'))
51+
instance = OrderLine.new(12, Item.new('watermelons'))
5252
instance.present_line(instance.optimal(instance.order_item.name))
5353

5454
expect(instance.presenter_line_total).to be_a Money
@@ -57,7 +57,7 @@
5757

5858
describe '#whole_packs' do
5959
it 'should return an Array' do
60-
instance = OrderLine.new(12, Item.new('watermelon'))
60+
instance = OrderLine.new(12, Item.new('watermelons'))
6161
pack_qtys = [[3, 6.99], [5, 8.99]]
6262

6363
expect(instance.send(:whole_packs, pack_qtys, [], @order_qty)).to be_a Array
@@ -66,7 +66,7 @@
6666

6767
describe '#left_over_items' do
6868
it 'should return an Array' do
69-
instance = OrderLine.new(12, Item.new('watermelon'))
69+
instance = OrderLine.new(12, Item.new('watermelons'))
7070
pack_qtys = [[3, 6.99], [5, 8.99]]
7171

7272
expect(instance.send(:left_over_items, pack_qtys, [], 1)).to be_a Array

spec/order_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
describe '#add_item' do
1616
it 'should return an Array of items' do
1717
instance = Order.new
18-
order_line = OrderLine.new(10, Item.new('watermelon'))
18+
order_line = OrderLine.new(10, Item.new('watermelons'))
1919
breakdown = order_line.present_line(order_line.optimal(order_line.order_item.name))
2020

2121
expect(instance.add_item(order_line, breakdown)).to be_a Array

0 commit comments

Comments
 (0)