Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Produce a valid order_line including qty price and total or optimal i…
- Loading branch information
|
@@ -10,7 +10,7 @@ class HandleInput |
|
|
# @param command [String] |
|
|
# @return |
|
|
def interpret(command) |
|
|
return unless ACTIONS.include?(command) |
|
|
return unless ACTIONS.detect { |a| a == command } |
|
|
|
|
|
$stdout.print "#{command} \n" |
|
|
|
|
@@ -43,19 +43,14 @@ def shop |
|
|
# TODO: Ensure the order_item is valid |
|
|
|
|
|
# Create a order_line containing the new item |
|
|
# puts line[1] |
|
|
order_line = OrderLine.new(line[0].to_i, Item.new(line[1])) |
|
|
|
|
|
# line_item = OrderLine.new(6, Item.new('watermelon')) |
|
|
order_line.optimal(order_line.order_item.name) |
|
|
# Find optimal qty of packs |
|
|
product = order_line.optimal(order_line.order_item.name) |
|
|
|
|
|
# create the order_line |
|
|
# order_line = OrderLine.new(line[0], line[1]) |
|
|
# Present order_line for invoice |
|
|
order_line.present_line(product) |
|
|
|
|
|
# return the order line |
|
|
$stdout.print "#{order_line.order_qty} #{order_line.order_item.name} \n" |
|
|
# puts order_line.optimal(order_line.order_item.name) |
|
|
# puts order_line |
|
|
order_line |
|
|
end |
|
|
end |
|
@@ -11,10 +11,12 @@ def initialize(name) |
|
|
@pack = nil |
|
|
end |
|
|
|
|
|
def watermelon |
|
|
threepack = Pack.new(3, Money.new(699, 'NZD')) |
|
|
fivepack = Pack.new(5, Money.new(899, 'NZD')) |
|
|
def packs(fruit) |
|
|
if fruit == 'watermelon' |
|
|
threepack = Pack.new(3, Money.new(699, 'NZD')) |
|
|
fivepack = Pack.new(5, Money.new(899, 'NZD')) |
|
|
|
|
|
[threepack, fivepack] |
|
|
[threepack, fivepack] |
|
|
end |
|
|
end |
|
|
end |
|
@@ -11,50 +11,72 @@ def initialize(order_qty, order_item) |
|
|
end |
|
|
|
|
|
def optimal(product) |
|
|
# get item.pack_sizes |
|
|
pack = @order_item.watermelon if product == 'watermelon' |
|
|
pack = @order_item.packs(product) |
|
|
# pack = @order_item.watermelon if product == 'watermelon' |
|
|
|
|
|
pack_qtys = [] |
|
|
pack.each do |p| |
|
|
pack_qtys << p.qty |
|
|
pack_qtys << [p.qty, p.price] |
|
|
end |
|
|
|
|
|
# sort packs in descending order |
|
|
p_desc = pack_qtys.sort { |a, b| b <=> a } |
|
|
|
|
|
whole_packs = {} |
|
|
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| |
|
|
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 |
|
|
|
|
|
whole_packs[p] = volume.to_int |
|
|
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 } |
|
|
|
|
|
p_asc.each do |p| |
|
|
p_asc.each do |p, v| |
|
|
break if left_over_qty.zero? |
|
|
|
|
|
if left_over_qty <= p |
|
|
whole_packs[p] += 1 unless whole_packs[p].nil? |
|
|
whole_packs[p] = 1 if whole_packs[p].nil? |
|
|
|
|
|
left_over_qty = 0 |
|
|
# 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 |
|
|
|
|
|
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 |
|
|
|
|
|
puts whole_packs |
|
|
whole_packs |
|
|
end |
|
|
|
|
|
def present_line(whole_packs) |
|
|
sub_total = [] |
|
|
sub_item = [] |
|
|
whole_packs.each do |k, v, i| |
|
|
sub_item << "#{k}x #{v} packs @ #{i} " |
|
|
sub_total << k * i |
|
|
end |
|
|
|
|
|
total = sub_total.inject(0) { |sum, x| sum + x } |
|
|
|
|
|
puts "#{@order_qty} #{@order_item.name} #{total}" |
|
|
|
|
|
sub_item.each do |s| |
|
|
puts s |
|
|
end |
|
|
end |
|
|
end |
Oops, something went wrong.
|
@@ -12,11 +12,11 @@ |
|
|
end |
|
|
end |
|
|
|
|
|
describe '#watermelon' do |
|
|
describe '#packs' do |
|
|
it 'should return an Array of Packs' do |
|
|
instance = Item.new('Watermelons') |
|
|
|
|
|
expect(instance.watermelon).to be_a Array |
|
|
expect(instance.packs('watermelon')).to be_a Array |
|
|
end |
|
|
end |
|
|
end |
|
@@ -13,10 +13,18 @@ |
|
|
end |
|
|
|
|
|
describe '#optimal' do |
|
|
it 'should return whole_packs' do |
|
|
instance = OrderLine.new(3, Item.new('watermelon')) |
|
|
it 'should return whole_packs which is an Array' do |
|
|
instance = OrderLine.new(12, Item.new('watermelon')) |
|
|
|
|
|
expect(instance.optimal(instance.order_item.name)).to be_a Array |
|
|
end |
|
|
end |
|
|
|
|
|
describe '#present_line' do |
|
|
it 'should return whole_packs which is an Array' do |
|
|
instance = OrderLine.new(12, Item.new('watermelon')) |
|
|
|
|
|
expect(instance.optimal(instance.order_item.name)).to be_a Hash |
|
|
expect(instance.present_line(instance.optimal(instance.order_item.name))).to be_a Array |
|
|
end |
|
|
end |
|
|
end |
Oops, something went wrong.