Skip to content

Commit f232c0b

Browse files
committed
Define Invoice object
1 parent e7df573 commit f232c0b

File tree

9 files changed

+119
-33
lines changed

9 files changed

+119
-33
lines changed

lib/basket.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/handle_input.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
require_relative 'item'
2+
require_relative 'invoice'
23
require_relative 'order_line'
34

45
# HandleInput class
56
class HandleInput
6-
attr_reader :order
7+
attr_reader :order, :invoice
78

89
# Actions
910
ACTIONS = %w[LIST SHOP VIEW].freeze
1011

1112
def initialize(order)
1213
@order = order
14+
@invoice = Invoice.new
1315
end
1416

1517
# Interpret method
@@ -41,8 +43,8 @@ def interpret(command)
4143
end
4244
end
4345

44-
# show invoice total
45-
puts order.find_order_total if command == 'VIEW'
46+
# View Invoice
47+
return invoice.print_order(order) if command == 'VIEW'
4648
end
4749

4850
PATTERN = /^\s*\d+\s*(watermelon||pineapple||rockmelon)$/

lib/invoice.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Invoice class
2+
class Invoice
3+
attr_reader :printorder
4+
5+
def initialize
6+
@printorder = []
7+
end
8+
9+
def print_order(order)
10+
puts "Customer Invoice \n"
11+
puts "Items rounded up to nearest available qty\n"
12+
puts "qty\titem\t\t\tsub"
13+
puts '-------------------------------------'
14+
order.items.each do |item|
15+
puts "#{item[0].order_qty} #{item[0].order_item.name} \t\t\t#{item[0].presenter_line_total}"
16+
item[1].each { |i| puts "\t#{i}\n" }
17+
end
18+
puts '-------------------------------------'
19+
puts "TOTAL \t\t\t\t#{order.find_order_total} \n\n"
20+
end
21+
end

lib/order.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def add_item(order_item, breakdown)
1313
def find_order_total
1414
total = 0
1515
@items.each do |item|
16-
total += item[0].total_quantity_of_items
16+
total += item[0].presenter_line_total
1717
end
1818

1919
total

lib/order_line.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@ def optimal(product)
2323
total_packs[0]
2424
end
2525

26-
def present_line(whole_packs)
26+
def present_line(packs)
2727
sub_total = []
2828
sub_item = []
29-
whole_packs.each do |k, v, i|
30-
sub_item << "#{k}x #{v} packs @ #{i} "
29+
packs.each do |k, v, i|
3130
sub_total << k * i
31+
sub_item << "#{k}x #{v} packs @ #{i}"
3232
end
3333

3434
@line_total = sub_total.inject(0) { |sum, x| sum + x }
3535

36-
puts "#{@order_qty} #{@order_item.name} #{@line_total}"
37-
38-
sub_item.each { |s| puts s }
36+
sub_item.each { |s| s }
3937
end
4038

41-
def total_quantity_of_items
39+
def presenter_line_total
4240
@line_total
4341
end
4442

spec/basket_spec.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

spec/handle_input_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
expect(instance.interpret('hello')).to be_nil
1111
end
12+
13+
it 'should return nil if command matches VIEW' do
14+
instance = HandleInput.new(Order.new)
15+
16+
expect(instance.interpret('VIEW')).to be_nil
17+
end
1218
end
1319

1420
describe '#shop' do
@@ -21,5 +27,19 @@
2127

2228
expect(instance.shop('10 watermelon')).to be_a OrderLine
2329
end
30+
31+
it 'should return nil if input does not match PATTERN' do
32+
instance = HandleInput.new(Order.new)
33+
34+
expect(instance.shop('fruit')).to be_nil
35+
end
36+
end
37+
38+
describe '#exec' do
39+
it 'should return an instance of OrderLine' do
40+
instance = HandleInput.new(Order.new)
41+
42+
expect(instance.exec(OrderLine.new(10, Item.new('watermelon')))).to be_a OrderLine
43+
end
2444
end
2545
end

spec/invoice_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/invoice'
4+
5+
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+
14+
describe '#print_order' do
15+
it 'should return nil' do
16+
instance = Invoice.new
17+
18+
expect(instance.print_order(Order.new)).to be_nil
19+
end
20+
end
21+
end

spec/order_line_spec.rb

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,32 @@
1010
expect(instance.order_qty).to be_a Integer
1111
expect(instance.order_qty).to eq 3
1212
end
13+
14+
it 'should have a order_item attribute which is an instance of Item' do
15+
instance = OrderLine.new(3, Item.new('watermelon'))
16+
17+
expect(instance.order_item).to be_a Item
18+
end
19+
20+
it 'should have a order_packs attribute which is nil' do
21+
instance = OrderLine.new(3, Item.new('watermelon'))
22+
23+
expect(instance.order_packs).to be_nil
24+
end
1325
end
1426

1527
describe '#optimal' do
16-
it 'should return whole_packs which is an Array' do
28+
it 'should return total_packs[0] which is an Array' do
1729
instance = OrderLine.new(12, Item.new('watermelon'))
1830

1931
expect(instance.optimal(instance.order_item.name)).to be_a Array
2032
end
33+
34+
it 'should return whole_packs[0] which is an Array' do
35+
instance = OrderLine.new(10, Item.new('watermelon'))
36+
37+
expect(instance.optimal(instance.order_item.name)).to be_a Array
38+
end
2139
end
2240

2341
describe '#present_line' do
@@ -27,4 +45,31 @@
2745
expect(instance.present_line(instance.optimal(instance.order_item.name))).to be_a Array
2846
end
2947
end
48+
49+
describe '#presenter_line_total' do
50+
it 'should return line_total which is an instance of Money' do
51+
instance = OrderLine.new(12, Item.new('watermelon'))
52+
instance.present_line(instance.optimal(instance.order_item.name))
53+
54+
expect(instance.presenter_line_total).to be_a Money
55+
end
56+
end
57+
58+
describe '#whole_packs' do
59+
it 'should return an Array' do
60+
instance = OrderLine.new(12, Item.new('watermelon'))
61+
pack_qtys = [[3, 6.99], [5, 8.99]]
62+
63+
expect(instance.send(:whole_packs, pack_qtys, [], @order_qty)).to be_a Array
64+
end
65+
end
66+
67+
describe '#left_over_items' do
68+
it 'should return an Array' do
69+
instance = OrderLine.new(12, Item.new('watermelon'))
70+
pack_qtys = [[3, 6.99], [5, 8.99]]
71+
72+
expect(instance.send(:left_over_items, pack_qtys, [], 1)).to be_a Array
73+
end
74+
end
3075
end

0 commit comments

Comments
 (0)