Skip to content

Commit 4b96f15

Browse files
committed
List available items and pack sizes. Move shop methods out into their own class
1 parent 56b6587 commit 4b96f15

File tree

9 files changed

+145
-104
lines changed

9 files changed

+145
-104
lines changed

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 "Main menu: LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"
10+
$stdout.print "Main menu: LIST available products, SHOP (add items to your order), VIEW basket, EXIT \n"
1111

1212
input = gets.chomp
1313

lib/handle_input.rb

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
require_relative 'item'
22
require_relative 'invoice'
3+
require_relative 'list'
4+
require_relative 'shop'
35
require_relative 'order_line'
46

57
# HandleInput class
68
class HandleInput
7-
attr_reader :order, :invoice
9+
attr_reader :order, :invoice, :list, :shop
810

911
# Actions
1012
ACTIONS = %w[LIST SHOP VIEW].freeze
1113

1214
def initialize(order)
1315
@order = order
1416
@invoice = Invoice.new
17+
@list = List.new
18+
@shop = Shop.new(order)
1519
end
1620

1721
# Interpret method
@@ -20,57 +24,10 @@ def initialize(order)
2024
def interpret(command)
2125
return unless ACTIONS.detect { |a| a == command }
2226

23-
if command.match?('LIST')
24-
# list all availble items and package sizes
25-
end
27+
return list.selection if command.match?('LIST')
2628

27-
return shop_menu if command.match?('SHOP')
29+
return shop.menu if command.match?('SHOP')
2830

2931
return invoice.print_order(order) if command.match?('VIEW')
3032
end
31-
32-
private
33-
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"
36-
37-
loop do
38-
input = $stdin.gets.chomp
39-
40-
next if input.empty?
41-
42-
break if 'BACK'.match?(input)
43-
44-
shop(input)
45-
next
46-
end
47-
end
48-
49-
# Order input pattern
50-
PATTERN = /^\s*\d+\s*(watermelons||pineapples||rockmelons)$/
51-
52-
# Order items
53-
def shop(input)
54-
unless input.match?(PATTERN)
55-
puts "That's not a valid input"
56-
return
57-
end
58-
59-
# Break input down into qty, item
60-
line = input.split(/\W+/)
61-
62-
exec(OrderLine.new(line[0].to_i, Item.new(line[1])))
63-
end
64-
65-
# Add item order_line to order
66-
def exec(order_line)
67-
order.add_item(
68-
order_line,
69-
order_line.present_line(
70-
order_line.optimal(order_line.order_item.name)
71-
)
72-
)
73-
74-
order_line
75-
end
7633
end

lib/invoice.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
# Invoice class
22
class Invoice
3-
attr_reader :printorder
4-
5-
def initialize
6-
@printorder = []
7-
end
8-
93
def print_order(order)
104
puts "Customer Invoice \n"
115
puts "Items rounded up to nearest available qty\n"

lib/list.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# List class
2+
class List
3+
def selection
4+
puts "Products Available \n"
5+
6+
puts 'Watermelons'
7+
Item.new('watermelons').packs('watermelons').each do |i|
8+
puts "#{i.qty} pack @ #{i.price}"
9+
end
10+
puts "---------------\n"
11+
12+
puts 'Pineapples'
13+
Item.new('pineapples').packs('pineapples').each do |i|
14+
puts "#{i.qty} pack @ #{i.price}"
15+
end
16+
puts "---------------\n"
17+
18+
puts 'Rockmelons'
19+
Item.new('rockmelons').packs('rockmelons').each do |i|
20+
puts "#{i.qty} pack @ #{i.price}"
21+
end
22+
puts "---------------\n"
23+
end
24+
end

lib/shop.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Shop class
2+
class Shop
3+
attr_reader :order
4+
# Order input pattern
5+
PATTERN = /^\s*\d+\s*(watermelons||pineapples||rockmelons)$/
6+
7+
def initialize(order)
8+
@order = order
9+
end
10+
11+
def menu
12+
$stdout.print "Type BACK at any time to return to the main menu. \nAdd qty and items to backet, example input: 3 watermelons \n"
13+
14+
loop do
15+
input = $stdin.gets.chomp
16+
17+
next if input.empty?
18+
19+
break if 'BACK'.match?(input)
20+
21+
choose_items(input)
22+
next
23+
end
24+
end
25+
26+
def choose_items(input)
27+
unless input.match?(PATTERN)
28+
puts "That's not a valid input"
29+
return
30+
end
31+
32+
# Break input down into qty, item
33+
line = input.split(/\W+/)
34+
35+
exec(OrderLine.new(line[0].to_i, Item.new(line[1])))
36+
end
37+
38+
def exec(order_line)
39+
order.add_item(
40+
order_line,
41+
order_line.present_line(
42+
order_line.optimal(order_line.order_item.name)
43+
)
44+
)
45+
46+
order_line
47+
end
48+
end

spec/handle_input_spec.rb

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,43 +41,4 @@
4141
expect(instance.interpret('SHOP')).to be_nil
4242
end
4343
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
56-
end
57-
58-
describe '#shop' do
59-
before do
60-
$stdin = StringIO.new('3 watermelons')
61-
end
62-
63-
it 'should return an instance of OrderLine' do
64-
instance = HandleInput.new(Order.new)
65-
66-
expect(instance.send(:shop, '10 watermelons')).to be_a OrderLine
67-
end
68-
69-
it 'should return nil if input does not match PATTERN' do
70-
instance = HandleInput.new(Order.new)
71-
72-
expect(instance.send(:shop, 'fruit')).to be_nil
73-
end
74-
end
75-
76-
describe '#exec' do
77-
it 'should return an instance of OrderLine' do
78-
instance = HandleInput.new(Order.new)
79-
80-
expect(instance.send(:exec, OrderLine.new(10, Item.new('watermelons')))).to be_a OrderLine
81-
end
82-
end
8344
end

spec/invoice_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
require './lib/invoice'
44

55
describe Invoice do
6-
describe '#initialize' do
7-
it 'should have a printorder attribute which is an Array' do
8-
instance = Invoice.new
9-
10-
expect(instance.printorder).to be_a Array
11-
end
12-
end
13-
146
describe '#print_order' do
157
it 'should return nil' do
168
instance = Invoice.new

spec/list_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/list'
4+
5+
describe List do
6+
describe '#selection' do
7+
it 'should return nil' do
8+
instance = List.new
9+
10+
expect(instance.selection).to be_nil
11+
end
12+
end
13+
end

spec/shop_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/shop'
4+
5+
describe Shop do
6+
describe '#initialize' do
7+
it 'should have an order attribute which is an instance of Order' do
8+
instance = Shop.new(Order.new)
9+
10+
expect(instance.order).to be_a Order
11+
end
12+
end
13+
14+
describe '#menu' do
15+
before do
16+
$stdin = StringIO.new('3 watermelons')
17+
$stdin = StringIO.new('BACK')
18+
end
19+
20+
it 'should return nil' do
21+
instance = Shop.new(Order.new)
22+
23+
expect(instance.send(:menu)).to be_nil
24+
end
25+
end
26+
27+
describe '#choose_items' do
28+
before do
29+
$stdin = StringIO.new('3 watermelons')
30+
end
31+
32+
it 'should return an instance of OrderLine' do
33+
instance = Shop.new(Order.new)
34+
35+
expect(instance.send(:choose_items, '10 watermelons')).to be_a OrderLine
36+
end
37+
38+
it 'should return nil if input does not match PATTERN' do
39+
instance = Shop.new(Order.new)
40+
41+
expect(instance.send(:choose_items, 'fruit')).to be_nil
42+
end
43+
end
44+
45+
describe '#exec' do
46+
it 'should return an instance of OrderLine' do
47+
instance = Shop.new(Order.new)
48+
49+
expect(instance.send(:exec, OrderLine.new(10, Item.new('watermelons')))).to be_a OrderLine
50+
end
51+
end
52+
end

0 commit comments

Comments
 (0)