Skip to content
Permalink
Browse files

Create orderline

  • Loading branch information
SelenaSmall committed Sep 2, 2017
1 parent ffba141 commit 424c2d597de3150800030276ab9faff66701f70d
Showing with 110 additions and 33 deletions.
  1. +16 −4 lib/handle_input.rb
  2. +9 −1 lib/item.rb
  3. +52 −1 lib/order_line.rb
  4. +2 −11 lib/watermelon.rb
  5. +12 −0 spec/handle_input_spec.rb
  6. +8 −0 spec/item_spec.rb
  7. +6 −4 spec/order_line_spec.rb
  8. +5 −12 spec/watermelon_spec.rb
@@ -33,17 +33,29 @@ def shop
# retrieve another input
$stdout.print "Select qty and items, example: 3 watermelon \n"

input = gets.chomp
input = $stdin.gets.chomp

return if input.empty? #TODO: OR if it doesn't match a specific order pattern
return if input.empty? # TODO: OR if it doesn't match a specific order pattern

# break it down into qty, item
line = input.split(/\W+/)

# 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)

# create the order_line
order_line = OrderLine.new(line[0], line[1])
# order_line = OrderLine.new(line[0], line[1])

# return the order line
$stdout.print "#{order_line.order_qty} #{order_line.order_item} \n"
$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
@@ -4,9 +4,17 @@

# Item class
class Item
attr_reader :name
attr_reader :name, :pack

def initialize(name)
@name = name
@pack = nil
end

def watermelon
threepack = Pack.new(3, Money.new(699, 'NZD'))
fivepack = Pack.new(5, Money.new(899, 'NZD'))

[threepack, fivepack]
end
end
@@ -1,9 +1,60 @@
require_relative 'item'

# OrderLine class
class OrderLine
attr_reader :order_qty, :order_item
attr_reader :order_qty, :order_item, :order_packs

def initialize(order_qty, order_item)
@order_qty = order_qty
@order_item = order_item
@order_packs = nil
end

def optimal(product)
# get item.pack_sizes
pack = @order_item.watermelon if product == 'watermelon'

pack_qtys = []
pack.each do |p|
pack_qtys << p.qty
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|
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
end

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

p_asc.each do |p|
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
end

next
end

puts whole_packs
whole_packs
end
end
@@ -6,26 +6,17 @@ class Watermelon < Item

OPTIONS = [3, 5].freeze

def initialize(name)
super(name)
def initialize
@pack = nil
end

# Pack size method
# @Param qty [Int]
# @Return @pack
def pack_size(qty)
def pack_sizes(qty)
return unless OPTIONS.include?(qty)

return @pack = Pack.new(3, Money.new(699, 'NZD')) if qty == 3
@pack = Pack.new(5, Money.new(899, 'NZD')) if qty == 5
end
end

# watermelon = Watermelon.new('watermelon')
# puts watermelon

# size = watermelon.pack_size(3)

# puts size.qty
# puts size.price
@@ -10,4 +10,16 @@
expect(instance.interpret('hello')).to be_nil
end
end

describe '#shop' do
before do
$stdin = StringIO.new('3 watermelon')
end

it 'should return an instance of OrderLine' do
instance = HandleInput.new

expect(instance.shop).to be_a OrderLine
end
end
end
@@ -11,4 +11,12 @@
expect(instance.name).to eq 'Watermelons'
end
end

describe '#watermelon' do
it 'should return an Array of Packs' do
instance = Item.new('Watermelons')

expect(instance.watermelon).to be_a Array
end
end
end
@@ -5,16 +5,18 @@
describe OrderLine do
describe '#initialize' do
it 'should have a order_qty attribute which is an Integer' do
instance = OrderLine.new(3, 'watermelon')
instance = OrderLine.new(3, Item.new('watermelon'))

expect(instance.order_qty).to be_a Integer
expect(instance.order_qty).to eq 3
end
end

it 'should have a order_item attribute which is an instance of Item' do
instance = OrderLine.new(3, Watermelon.new('watermelon'))
describe '#optimal' do
it 'should return whole_packs' do
instance = OrderLine.new(3, Item.new('watermelon'))

expect(instance.order_item).to be_a Item
expect(instance.optimal(instance.order_item.name)).to be_a Hash
end
end
end
@@ -4,31 +4,24 @@

describe Watermelon do
describe '#initialize' do
it 'should have a name attribute which is an String' do
instance = Watermelon.new('watermelon')

expect(instance.name).to be_a String
expect(instance.name).to eq 'watermelon'
end

it 'should have a pack attribute which is nil' do
instance = Watermelon.new('watermelon')
instance = Watermelon.new

expect(instance.pack).to be_nil
end
end

describe '#pack_size' do
it 'should return nil if selection is not present in OPTIONS array' do
instance = Watermelon.new('watermelon')
instance = Watermelon.new

expect(instance.pack_size(1)).to be_nil
expect(instance.pack_sizes(1)).to be_nil
end

it 'should return an instance of Pack if selection is present in OPTIONS array' do
instance = Watermelon.new('watermelon')
instance = Watermelon.new

expect(instance.pack_size(3)).to be_a Pack
expect(instance.pack_sizes(3)).to be_a Pack
end
end
end

0 comments on commit 424c2d5

Please sign in to comment.