Skip to content

Commit ddd08f5

Browse files
committed
Add orderline to order and calculate order total
1 parent 120a507 commit ddd08f5

File tree

7 files changed

+90
-24
lines changed

7 files changed

+90
-24
lines changed

app.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/usr/bin/ruby
22

3+
require_relative './lib/order'
34
require_relative './lib/handle_input'
45

5-
command = HandleInput.new
6-
7-
$stdout.print "Would you like to LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"
6+
command = HandleInput.new(Order.new)
87

98
# Read user input
109
loop do
10+
$stdout.print "Would you like to LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"
11+
1112
input = gets.chomp
1213

1314
next if input.empty?

lib/handle_input.rb

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
require_relative 'item'
2+
# require_relative 'order'
23
require_relative 'order_line'
34

45
# HandleInput class
56
class HandleInput
7+
attr_reader :order
8+
69
# Actions
710
ACTIONS = %w[LIST SHOP VIEW EXIT].freeze
811

12+
def initialize(order)
13+
@order = order
14+
end
15+
916
# Interpret method
1017
# @param command [String]
1118
# @return
@@ -18,7 +25,26 @@ def interpret(command)
1825
# list all availble items and package sizes
1926
end
2027

21-
return shop if command == 'SHOP'
28+
# return shop if command == 'SHOP'
29+
if command == 'SHOP'
30+
$stdout.print "Select qty and items, example: 3 watermelon \n"
31+
32+
loop do
33+
input = $stdin.gets.chomp
34+
35+
next if input.empty?
36+
37+
unless 'BACK'.match?(input)
38+
shop(input)
39+
next
40+
end
41+
42+
$stdout.print "Returning to main menu \n"
43+
break
44+
end
45+
end
46+
47+
# loop { shop if command == 'SHOP' }
2248

2349
if command == 'VIEW'
2450
# show invoice
@@ -29,13 +55,8 @@ def interpret(command)
2955
end
3056
end
3157

32-
def shop
33-
# retrieve another input
34-
$stdout.print "Select qty and items, example: 3 watermelon \n"
35-
36-
input = $stdin.gets.chomp
37-
38-
return if input.empty? # TODO: OR if it doesn't match a specific order pattern
58+
def shop(input)
59+
# TODO: OR if it doesn't match a specific order pattern
3960

4061
# break it down into qty, item
4162
line = input.split(/\W+/)
@@ -51,6 +72,14 @@ def shop
5172
# Present order_line for invoice
5273
order_line.present_line(product)
5374

75+
# order = Order.new
76+
77+
add_to_order = @order.add_item(order_line)
78+
79+
# puts add_to_order
80+
81+
puts order.find_order_total
82+
5483
order_line
5584
end
5685
end

lib/order.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
# Order class
22
class Order
3-
attr_reader :items, :basket
3+
attr_reader :items
44

5-
def initialize(basket)
5+
def initialize
66
@items = []
7-
@basket = basket
7+
end
8+
9+
def add_item(order_item)
10+
@items.push(order_item)
11+
end
12+
13+
def find_order_total
14+
total = 0
15+
@items.each do |item|
16+
total += item.total_quantity_of_items
17+
end
18+
19+
total
820
end
921
end

lib/order_line.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,17 @@ def present_line(whole_packs)
7171
sub_total << k * i
7272
end
7373

74-
total = sub_total.inject(0) { |sum, x| sum + x }
74+
@line_total = sub_total.inject(0) { |sum, x| sum + x }
7575

76-
puts "#{@order_qty} #{@order_item.name} #{total}"
76+
puts "#{@order_qty} #{@order_item.name} #{@line_total}"
7777

7878
sub_item.each do |s|
7979
puts s
8080
end
8181
end
82+
83+
def total_quantity_of_items
84+
# @order_qty * @line_total
85+
@line_total
86+
end
8287
end

spec/handle_input_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
describe HandleInput do
66
describe '#interpret' do
77
it 'should return nil unless command matches a value in ACTIONS array' do
8-
instance = HandleInput.new
8+
instance = HandleInput.new(Order.new)
99

1010
expect(instance.interpret('hello')).to be_nil
1111
end
@@ -17,9 +17,9 @@
1717
end
1818

1919
it 'should return an instance of OrderLine' do
20-
instance = HandleInput.new
20+
instance = HandleInput.new(Order.new)
2121

22-
expect(instance.shop).to be_a OrderLine
22+
expect(instance.shop('10 watermelon')).to be_a OrderLine
2323
end
2424
end
2525
end

spec/item_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@
1919
expect(instance.packs('watermelon')).to be_a Array
2020
end
2121
end
22+
23+
describe '#packs' do
24+
it 'should nil unless fruit is valid' do
25+
instance = Item.new('Watermelons')
26+
27+
expect(instance.packs('hello')).to be_nil
28+
end
29+
end
2230
end

spec/order_spec.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@
44

55
describe Order do
66
describe '#initialize' do
7-
it 'should have a items attribute which is an empty Array' do
8-
instance = Order.new(Basket.new)
7+
it 'should have an items attribute which is an empty Array' do
8+
instance = Order.new
99

1010
expect(instance.items).to be_a Array
1111
expect(instance.items).to be_empty
1212
end
13+
end
14+
15+
describe '#add_item' do
16+
it 'should return an Array of items' do
17+
instance = Order.new
18+
orderline = OrderLine.new(10, Item.new('watermelon'))
19+
20+
expect(instance.add_item(orderline)).to be_a Array
21+
end
22+
end
1323

14-
it 'should have a basket attribute which is an instance of Basket' do
15-
instance = Order.new(Basket.new)
24+
describe '#find_order_total' do
25+
it 'should return 0 if there are no items in the items array' do
26+
instance = Order.new
1627

17-
expect(instance.basket).to be_a Basket
28+
expect(instance.find_order_total).to eq 0
1829
end
1930
end
2031
end

0 commit comments

Comments
 (0)