Skip to content

Commit 120a507

Browse files
committed
Produce a valid order_line including qty price and total or optimal item packs
1 parent 424c2d5 commit 120a507

File tree

7 files changed

+58
-80
lines changed

7 files changed

+58
-80
lines changed

lib/handle_input.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class HandleInput
1010
# @param command [String]
1111
# @return
1212
def interpret(command)
13-
return unless ACTIONS.include?(command)
13+
return unless ACTIONS.detect { |a| a == command }
1414

1515
$stdout.print "#{command} \n"
1616

@@ -43,19 +43,14 @@ def shop
4343
# TODO: Ensure the order_item is valid
4444

4545
# Create a order_line containing the new item
46-
# puts line[1]
4746
order_line = OrderLine.new(line[0].to_i, Item.new(line[1]))
4847

49-
# line_item = OrderLine.new(6, Item.new('watermelon'))
50-
order_line.optimal(order_line.order_item.name)
48+
# Find optimal qty of packs
49+
product = order_line.optimal(order_line.order_item.name)
5150

52-
# create the order_line
53-
# order_line = OrderLine.new(line[0], line[1])
51+
# Present order_line for invoice
52+
order_line.present_line(product)
5453

55-
# return the order line
56-
$stdout.print "#{order_line.order_qty} #{order_line.order_item.name} \n"
57-
# puts order_line.optimal(order_line.order_item.name)
58-
# puts order_line
5954
order_line
6055
end
6156
end

lib/item.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ def initialize(name)
1111
@pack = nil
1212
end
1313

14-
def watermelon
15-
threepack = Pack.new(3, Money.new(699, 'NZD'))
16-
fivepack = Pack.new(5, Money.new(899, 'NZD'))
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'))
1718

18-
[threepack, fivepack]
19+
[threepack, fivepack]
20+
end
1921
end
2022
end

lib/order_line.rb

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,72 @@ def initialize(order_qty, order_item)
1111
end
1212

1313
def optimal(product)
14-
# get item.pack_sizes
15-
pack = @order_item.watermelon if product == 'watermelon'
14+
pack = @order_item.packs(product)
15+
# pack = @order_item.watermelon if product == 'watermelon'
1616

1717
pack_qtys = []
1818
pack.each do |p|
19-
pack_qtys << p.qty
19+
pack_qtys << [p.qty, p.price]
2020
end
2121

2222
# sort packs in descending order
2323
p_desc = pack_qtys.sort { |a, b| b <=> a }
2424

25-
whole_packs = {}
25+
whole_packs = []
2626
left_over_qty = order_qty
2727

2828
# Find the # of largest size packs required to make up the qty
2929
# If it's not exact, top it up with next largest packs, etc etc
30-
p_desc.each do |p|
30+
p_desc.each do |p, v|
3131
volume = left_over_qty.to_f / p
3232

3333
next unless volume >= 1
3434

3535
product = volume.to_int * p
3636
left_over_qty -= product
3737

38-
whole_packs[p] = volume.to_int
38+
whole_packs << [volume.to_int, p, v]
3939
end
40+
# puts whole_packs.each
4041

4142
# sort pack in ascending order
4243
p_asc = pack_qtys.sort { |a, b| a <=> b }
4344

44-
p_asc.each do |p|
45+
p_asc.each do |p, v|
4546
break if left_over_qty.zero?
4647

4748
if left_over_qty <= p
48-
whole_packs[p] += 1 unless whole_packs[p].nil?
49-
whole_packs[p] = 1 if whole_packs[p].nil?
50-
51-
left_over_qty = 0
49+
# Update qty of packs if entry already exists
50+
whole_packs.map do |a|
51+
a[0] += 1 if a[1].to_i == p
52+
left_over_qty = 0
53+
end
54+
55+
next if whole_packs.detect { |a| a[1] == p }
56+
# Otherwise, add a new entry to make up the qty
57+
whole_packs << [1, p, v]
5258
end
5359

5460
next
5561
end
5662

57-
puts whole_packs
5863
whole_packs
5964
end
65+
66+
def present_line(whole_packs)
67+
sub_total = []
68+
sub_item = []
69+
whole_packs.each do |k, v, i|
70+
sub_item << "#{k}x #{v} packs @ #{i} "
71+
sub_total << k * i
72+
end
73+
74+
total = sub_total.inject(0) { |sum, x| sum + x }
75+
76+
puts "#{@order_qty} #{@order_item.name} #{total}"
77+
78+
sub_item.each do |s|
79+
puts s
80+
end
81+
end
6082
end

lib/watermelon.rb

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

spec/item_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
end
1313
end
1414

15-
describe '#watermelon' do
15+
describe '#packs' do
1616
it 'should return an Array of Packs' do
1717
instance = Item.new('Watermelons')
1818

19-
expect(instance.watermelon).to be_a Array
19+
expect(instance.packs('watermelon')).to be_a Array
2020
end
2121
end
2222
end

spec/order_line_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@
1313
end
1414

1515
describe '#optimal' do
16-
it 'should return whole_packs' do
17-
instance = OrderLine.new(3, Item.new('watermelon'))
16+
it 'should return whole_packs which is an Array' do
17+
instance = OrderLine.new(12, Item.new('watermelon'))
18+
19+
expect(instance.optimal(instance.order_item.name)).to be_a Array
20+
end
21+
end
22+
23+
describe '#present_line' do
24+
it 'should return whole_packs which is an Array' do
25+
instance = OrderLine.new(12, Item.new('watermelon'))
1826

19-
expect(instance.optimal(instance.order_item.name)).to be_a Hash
27+
expect(instance.present_line(instance.optimal(instance.order_item.name))).to be_a Array
2028
end
2129
end
2230
end

spec/watermelon_spec.rb

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

0 commit comments

Comments
 (0)