Skip to content

Commit 424c2d5

Browse files
committed
Create orderline
1 parent ffba141 commit 424c2d5

File tree

8 files changed

+110
-33
lines changed

8 files changed

+110
-33
lines changed

lib/handle_input.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,29 @@ def shop
3333
# retrieve another input
3434
$stdout.print "Select qty and items, example: 3 watermelon \n"
3535

36-
input = gets.chomp
36+
input = $stdin.gets.chomp
3737

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

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

43+
# TODO: Ensure the order_item is valid
44+
45+
# Create a order_line containing the new item
46+
# puts line[1]
47+
order_line = OrderLine.new(line[0].to_i, Item.new(line[1]))
48+
49+
# line_item = OrderLine.new(6, Item.new('watermelon'))
50+
order_line.optimal(order_line.order_item.name)
51+
4352
# create the order_line
44-
order_line = OrderLine.new(line[0], line[1])
53+
# order_line = OrderLine.new(line[0], line[1])
4554

4655
# return the order line
47-
$stdout.print "#{order_line.order_qty} #{order_line.order_item} \n"
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
59+
order_line
4860
end
4961
end

lib/item.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44

55
# Item class
66
class Item
7-
attr_reader :name
7+
attr_reader :name, :pack
88

99
def initialize(name)
1010
@name = name
11+
@pack = nil
12+
end
13+
14+
def watermelon
15+
threepack = Pack.new(3, Money.new(699, 'NZD'))
16+
fivepack = Pack.new(5, Money.new(899, 'NZD'))
17+
18+
[threepack, fivepack]
1119
end
1220
end

lib/order_line.rb

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
1+
require_relative 'item'
2+
13
# OrderLine class
24
class OrderLine
3-
attr_reader :order_qty, :order_item
5+
attr_reader :order_qty, :order_item, :order_packs
46

57
def initialize(order_qty, order_item)
68
@order_qty = order_qty
79
@order_item = order_item
10+
@order_packs = nil
11+
end
12+
13+
def optimal(product)
14+
# get item.pack_sizes
15+
pack = @order_item.watermelon if product == 'watermelon'
16+
17+
pack_qtys = []
18+
pack.each do |p|
19+
pack_qtys << p.qty
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|
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
37+
38+
whole_packs[p] = volume.to_int
39+
end
40+
41+
# sort pack in ascending order
42+
p_asc = pack_qtys.sort { |a, b| a <=> b }
43+
44+
p_asc.each do |p|
45+
break if left_over_qty.zero?
46+
47+
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
52+
end
53+
54+
next
55+
end
56+
57+
puts whole_packs
58+
whole_packs
859
end
960
end

lib/watermelon.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,17 @@ class Watermelon < Item
66

77
OPTIONS = [3, 5].freeze
88

9-
def initialize(name)
10-
super(name)
9+
def initialize
1110
@pack = nil
1211
end
1312

1413
# Pack size method
1514
# @Param qty [Int]
1615
# @Return @pack
17-
def pack_size(qty)
16+
def pack_sizes(qty)
1817
return unless OPTIONS.include?(qty)
1918

2019
return @pack = Pack.new(3, Money.new(699, 'NZD')) if qty == 3
2120
@pack = Pack.new(5, Money.new(899, 'NZD')) if qty == 5
2221
end
2322
end
24-
25-
# watermelon = Watermelon.new('watermelon')
26-
# puts watermelon
27-
28-
# size = watermelon.pack_size(3)
29-
30-
# puts size.qty
31-
# puts size.price

spec/handle_input_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@
1010
expect(instance.interpret('hello')).to be_nil
1111
end
1212
end
13+
14+
describe '#shop' do
15+
before do
16+
$stdin = StringIO.new('3 watermelon')
17+
end
18+
19+
it 'should return an instance of OrderLine' do
20+
instance = HandleInput.new
21+
22+
expect(instance.shop).to be_a OrderLine
23+
end
24+
end
1325
end

spec/item_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@
1111
expect(instance.name).to eq 'Watermelons'
1212
end
1313
end
14+
15+
describe '#watermelon' do
16+
it 'should return an Array of Packs' do
17+
instance = Item.new('Watermelons')
18+
19+
expect(instance.watermelon).to be_a Array
20+
end
21+
end
1422
end

spec/order_line_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
describe OrderLine do
66
describe '#initialize' do
77
it 'should have a order_qty attribute which is an Integer' do
8-
instance = OrderLine.new(3, 'watermelon')
8+
instance = OrderLine.new(3, Item.new('watermelon'))
99

1010
expect(instance.order_qty).to be_a Integer
1111
expect(instance.order_qty).to eq 3
1212
end
13+
end
1314

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

17-
expect(instance.order_item).to be_a Item
19+
expect(instance.optimal(instance.order_item.name)).to be_a Hash
1820
end
1921
end
2022
end

spec/watermelon_spec.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,24 @@
44

55
describe Watermelon do
66
describe '#initialize' do
7-
it 'should have a name attribute which is an String' do
8-
instance = Watermelon.new('watermelon')
9-
10-
expect(instance.name).to be_a String
11-
expect(instance.name).to eq 'watermelon'
12-
end
13-
147
it 'should have a pack attribute which is nil' do
15-
instance = Watermelon.new('watermelon')
8+
instance = Watermelon.new
169

1710
expect(instance.pack).to be_nil
1811
end
1912
end
2013

2114
describe '#pack_size' do
2215
it 'should return nil if selection is not present in OPTIONS array' do
23-
instance = Watermelon.new('watermelon')
16+
instance = Watermelon.new
2417

25-
expect(instance.pack_size(1)).to be_nil
18+
expect(instance.pack_sizes(1)).to be_nil
2619
end
2720

2821
it 'should return an instance of Pack if selection is present in OPTIONS array' do
29-
instance = Watermelon.new('watermelon')
22+
instance = Watermelon.new
3023

31-
expect(instance.pack_size(3)).to be_a Pack
24+
expect(instance.pack_sizes(3)).to be_a Pack
3225
end
3326
end
3427
end

0 commit comments

Comments
 (0)