Skip to content

Commit e7df573

Browse files
committed
Break down methods in HandleInput and OrderLine
1 parent ddd08f5 commit e7df573

File tree

4 files changed

+66
-86
lines changed

4 files changed

+66
-86
lines changed

lib/handle_input.rb

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
require_relative 'item'
2-
# require_relative 'order'
32
require_relative 'order_line'
43

54
# HandleInput class
65
class HandleInput
76
attr_reader :order
87

98
# Actions
10-
ACTIONS = %w[LIST SHOP VIEW EXIT].freeze
9+
ACTIONS = %w[LIST SHOP VIEW].freeze
1110

1211
def initialize(order)
1312
@order = order
@@ -19,8 +18,6 @@ def initialize(order)
1918
def interpret(command)
2019
return unless ACTIONS.detect { |a| a == command }
2120

22-
$stdout.print "#{command} \n"
23-
2421
if command == 'LIST'
2522
# list all availble items and package sizes
2623
end
@@ -44,41 +41,33 @@ def interpret(command)
4441
end
4542
end
4643

47-
# loop { shop if command == 'SHOP' }
48-
49-
if command == 'VIEW'
50-
# show invoice
51-
end
52-
53-
if command == 'EXIT'
54-
# exit
55-
end
44+
# show invoice total
45+
puts order.find_order_total if command == 'VIEW'
5646
end
5747

48+
PATTERN = /^\s*\d+\s*(watermelon||pineapple||rockmelon)$/
49+
50+
# Order items
5851
def shop(input)
59-
# TODO: OR if it doesn't match a specific order pattern
52+
unless input.match?(PATTERN)
53+
puts "That's not a valid input"
54+
return
55+
end
6056

61-
# break it down into qty, item
57+
# Break input down into qty, item
6258
line = input.split(/\W+/)
6359

64-
# TODO: Ensure the order_item is valid
65-
66-
# Create a order_line containing the new item
67-
order_line = OrderLine.new(line[0].to_i, Item.new(line[1]))
68-
69-
# Find optimal qty of packs
70-
product = order_line.optimal(order_line.order_item.name)
71-
72-
# Present order_line for invoice
73-
order_line.present_line(product)
74-
75-
# order = Order.new
76-
77-
add_to_order = @order.add_item(order_line)
78-
79-
# puts add_to_order
60+
exec(OrderLine.new(line[0].to_i, Item.new(line[1])))
61+
end
8062

81-
puts order.find_order_total
63+
# Add item order_line to order
64+
def exec(order_line)
65+
order.add_item(
66+
order_line,
67+
order_line.present_line(
68+
order_line.optimal(order_line.order_item.name)
69+
)
70+
)
8271

8372
order_line
8473
end

lib/order.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ def initialize
66
@items = []
77
end
88

9-
def add_item(order_item)
10-
@items.push(order_item)
9+
def add_item(order_item, breakdown)
10+
@items.push([order_item, breakdown])
1111
end
1212

1313
def find_order_total
1414
total = 0
1515
@items.each do |item|
16-
total += item.total_quantity_of_items
16+
total += item[0].total_quantity_of_items
1717
end
1818

1919
total

lib/order_line.rb

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,16 @@ def initialize(order_qty, order_item)
1111
end
1212

1313
def optimal(product)
14-
pack = @order_item.packs(product)
15-
# pack = @order_item.watermelon if product == 'watermelon'
16-
1714
pack_qtys = []
18-
pack.each do |p|
19-
pack_qtys << [p.qty, p.price]
20-
end
21-
22-
# sort packs in descending order
23-
p_desc = pack_qtys.sort { |a, b| b <=> a }
24-
25-
whole_packs = []
26-
left_over_qty = order_qty
27-
28-
# Find the # of largest size packs required to make up the qty
29-
# If it's not exact, top it up with next largest packs, etc etc
30-
p_desc.each do |p, v|
31-
volume = left_over_qty.to_f / p
32-
33-
next unless volume >= 1
34-
35-
product = volume.to_int * p
36-
left_over_qty -= product
15+
@order_item.packs(product).each { |p| pack_qtys << [p.qty, p.price] }
3716

38-
whole_packs << [volume.to_int, p, v]
39-
end
40-
# puts whole_packs.each
41-
42-
# sort pack in ascending order
43-
p_asc = pack_qtys.sort { |a, b| a <=> b }
17+
whole_packs = whole_packs(pack_qtys, [], order_qty)
4418

45-
p_asc.each do |p, v|
46-
break if left_over_qty.zero?
19+
return whole_packs[0] if whole_packs[1].zero?
4720

48-
if left_over_qty <= p
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
21+
total_packs = left_over_items(pack_qtys, whole_packs[0], whole_packs[1])
5422

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]
58-
end
59-
60-
next
61-
end
62-
63-
whole_packs
23+
total_packs[0]
6424
end
6525

6626
def present_line(whole_packs)
@@ -75,13 +35,43 @@ def present_line(whole_packs)
7535

7636
puts "#{@order_qty} #{@order_item.name} #{@line_total}"
7737

78-
sub_item.each do |s|
79-
puts s
80-
end
38+
sub_item.each { |s| puts s }
8139
end
8240

8341
def total_quantity_of_items
84-
# @order_qty * @line_total
8542
@line_total
8643
end
44+
45+
private
46+
47+
def whole_packs(pack_qtys, packs, left_over_qty)
48+
pack_qtys.sort { |a, b| b <=> a }.each do |p, v|
49+
volume = left_over_qty.to_f / p
50+
51+
next unless volume >= 1
52+
53+
left_over_qty -= volume.to_int * p
54+
55+
packs << [volume.to_int, p, v]
56+
end
57+
58+
[packs, left_over_qty]
59+
end
60+
61+
def left_over_items(pack_qtys, packs, left_over_qty)
62+
pack_qtys.sort { |a, b| a <=> b }.each do |p, v|
63+
break if left_over_qty.zero?
64+
65+
next unless left_over_qty <= p
66+
67+
packs.map { |a| a[0] += 1 if a[1].to_i == p }
68+
left_over_qty = 0
69+
70+
next if packs.detect { |a| a[1] == p }
71+
72+
packs << [1, p, v]
73+
end
74+
75+
[packs, left_over_qty]
76+
end
8777
end

spec/order_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
describe '#add_item' do
1616
it 'should return an Array of items' do
1717
instance = Order.new
18-
orderline = OrderLine.new(10, Item.new('watermelon'))
18+
order_line = OrderLine.new(10, Item.new('watermelon'))
19+
breakdown = order_line.present_line(order_line.optimal(order_line.order_item.name))
1920

20-
expect(instance.add_item(orderline)).to be_a Array
21+
expect(instance.add_item(order_line, breakdown)).to be_a Array
2122
end
2223
end
2324

0 commit comments

Comments
 (0)