Permalink
Browse files

Break down methods in HandleInput and OrderLine

  • Loading branch information...
SelenaSmall committed Sep 3, 2017
1 parent ddd08f5 commit e7df573d38fd1178cc567e3d0b70d65406d17b5a
Showing with 66 additions and 86 deletions.
  1. +21 −32 lib/handle_input.rb
  2. +3 −3 lib/order.rb
  3. +39 −49 lib/order_line.rb
  4. +3 −2 spec/order_spec.rb
@@ -1,13 +1,12 @@
require_relative 'item'
# require_relative 'order'
require_relative 'order_line'
# HandleInput class
class HandleInput
attr_reader :order
# Actions
ACTIONS = %w[LIST SHOP VIEW EXIT].freeze
ACTIONS = %w[LIST SHOP VIEW].freeze
def initialize(order)
@order = order
@@ -19,8 +18,6 @@ def initialize(order)
def interpret(command)
return unless ACTIONS.detect { |a| a == command }
$stdout.print "#{command} \n"
if command == 'LIST'
# list all availble items and package sizes
end
@@ -44,41 +41,33 @@ def interpret(command)
end
end
# loop { shop if command == 'SHOP' }
if command == 'VIEW'
# show invoice
end
if command == 'EXIT'
# exit
end
# show invoice total
puts order.find_order_total if command == 'VIEW'
end
PATTERN = /^\s*\d+\s*(watermelon||pineapple||rockmelon)$/
# Order items
def shop(input)
# TODO: OR if it doesn't match a specific order pattern
unless input.match?(PATTERN)
puts "That's not a valid input"
return
end
# break it down into qty, item
# Break input down into qty, item
line = input.split(/\W+/)
# TODO: Ensure the order_item is valid
# Create a order_line containing the new item
order_line = OrderLine.new(line[0].to_i, Item.new(line[1]))
# Find optimal qty of packs
product = order_line.optimal(order_line.order_item.name)
# Present order_line for invoice
order_line.present_line(product)
# order = Order.new
add_to_order = @order.add_item(order_line)
# puts add_to_order
exec(OrderLine.new(line[0].to_i, Item.new(line[1])))
end
puts order.find_order_total
# Add item order_line to order
def exec(order_line)
order.add_item(
order_line,
order_line.present_line(
order_line.optimal(order_line.order_item.name)
)
)
order_line
end
@@ -6,14 +6,14 @@ def initialize
@items = []
end
def add_item(order_item)
@items.push(order_item)
def add_item(order_item, breakdown)
@items.push([order_item, breakdown])
end
def find_order_total
total = 0
@items.each do |item|
total += item.total_quantity_of_items
total += item[0].total_quantity_of_items
end
total
@@ -11,56 +11,16 @@ def initialize(order_qty, order_item)
end
def optimal(product)
pack = @order_item.packs(product)
# pack = @order_item.watermelon if product == 'watermelon'
pack_qtys = []
pack.each do |p|
pack_qtys << [p.qty, p.price]
end
# sort packs in descending order
p_desc = pack_qtys.sort { |a, b| b <=> a }
whole_packs = []
left_over_qty = order_qty
# Find the # of largest size packs required to make up the qty
# If it's not exact, top it up with next largest packs, etc etc
p_desc.each do |p, v|
volume = left_over_qty.to_f / p
next unless volume >= 1
product = volume.to_int * p
left_over_qty -= product
@order_item.packs(product).each { |p| pack_qtys << [p.qty, p.price] }
whole_packs << [volume.to_int, p, v]
end
# puts whole_packs.each
# sort pack in ascending order
p_asc = pack_qtys.sort { |a, b| a <=> b }
whole_packs = whole_packs(pack_qtys, [], order_qty)
p_asc.each do |p, v|
break if left_over_qty.zero?
return whole_packs[0] if whole_packs[1].zero?
if left_over_qty <= p
# Update qty of packs if entry already exists
whole_packs.map do |a|
a[0] += 1 if a[1].to_i == p
left_over_qty = 0
end
total_packs = left_over_items(pack_qtys, whole_packs[0], whole_packs[1])
next if whole_packs.detect { |a| a[1] == p }
# Otherwise, add a new entry to make up the qty
whole_packs << [1, p, v]
end
next
end
whole_packs
total_packs[0]
end
def present_line(whole_packs)
@@ -75,13 +35,43 @@ def present_line(whole_packs)
puts "#{@order_qty} #{@order_item.name} #{@line_total}"
sub_item.each do |s|
puts s
end
sub_item.each { |s| puts s }
end
def total_quantity_of_items
# @order_qty * @line_total
@line_total
end
private
def whole_packs(pack_qtys, packs, left_over_qty)
pack_qtys.sort { |a, b| b <=> a }.each do |p, v|
volume = left_over_qty.to_f / p
next unless volume >= 1
left_over_qty -= volume.to_int * p
packs << [volume.to_int, p, v]
end
[packs, left_over_qty]
end
def left_over_items(pack_qtys, packs, left_over_qty)
pack_qtys.sort { |a, b| a <=> b }.each do |p, v|
break if left_over_qty.zero?
next unless left_over_qty <= p
packs.map { |a| a[0] += 1 if a[1].to_i == p }
left_over_qty = 0
next if packs.detect { |a| a[1] == p }
packs << [1, p, v]
end
[packs, left_over_qty]
end
end
@@ -15,9 +15,10 @@
describe '#add_item' do
it 'should return an Array of items' do
instance = Order.new
orderline = OrderLine.new(10, Item.new('watermelon'))
order_line = OrderLine.new(10, Item.new('watermelon'))
breakdown = order_line.present_line(order_line.optimal(order_line.order_item.name))
expect(instance.add_item(orderline)).to be_a Array
expect(instance.add_item(order_line, breakdown)).to be_a Array
end
end

0 comments on commit e7df573

Please sign in to comment.