Skip to content
Permalink
Browse files

Add additional item packs. Refactor shop_menu into it's own method

  • Loading branch information...
SelenaSmall committed Sep 3, 2017
1 parent f232c0b commit 56b658717a5ee41287142733e5cd7e73c1f24e81
Showing with 133 additions and 44 deletions.
  1. +1 −1 app.rb
  2. +18 −18 lib/handle_input.rb
  3. +25 −6 lib/item.rb
  4. +42 −4 spec/handle_input_spec.rb
  5. +37 −5 spec/item_spec.rb
  6. +9 −9 spec/order_line_spec.rb
  7. +1 −1 spec/order_spec.rb
2 app.rb
@@ -7,7 +7,7 @@

# Read user input
loop do
$stdout.print "Would you like to LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"
$stdout.print "Main menu: LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"

input = gets.chomp

@@ -20,34 +20,34 @@ def initialize(order)
def interpret(command)
return unless ACTIONS.detect { |a| a == command }

if command == 'LIST'
if command.match?('LIST')
# list all availble items and package sizes
end

# return shop if command == 'SHOP'
if command == 'SHOP'
$stdout.print "Select qty and items, example: 3 watermelon \n"
return shop_menu if command.match?('SHOP')

loop do
input = $stdin.gets.chomp
return invoice.print_order(order) if command.match?('VIEW')
end

next if input.empty?
private

unless 'BACK'.match?(input)
shop(input)
next
end
def shop_menu
$stdout.print "Type BACK at any time to return to the main menu. \nAdd qty and items to backet, example input: 3 watermelons \n"

$stdout.print "Returning to main menu \n"
break
end
end
loop do
input = $stdin.gets.chomp

next if input.empty?

# View Invoice
return invoice.print_order(order) if command == 'VIEW'
break if 'BACK'.match?(input)

shop(input)
next
end
end

PATTERN = /^\s*\d+\s*(watermelon||pineapple||rockmelon)$/
# Order input pattern
PATTERN = /^\s*\d+\s*(watermelons||pineapples||rockmelons)$/

# Order items
def shop(input)
@@ -4,19 +4,38 @@

# Item class
class Item
# Products
PRODUCTS = %w[watermelons pineapples rockmelons].freeze

attr_reader :name, :pack

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

def packs(fruit)
if fruit == 'watermelon'
threepack = Pack.new(3, Money.new(699, 'NZD'))
fivepack = Pack.new(5, Money.new(899, 'NZD'))
def packs(product)
return unless PRODUCTS.detect { |a| a == product }

send(product)
end

private

def watermelons
[Pack.new(3, Money.new(699, 'NZD')),
Pack.new(5, Money.new(899, 'NZD'))]
end

def pineapples
[Pack.new(2, Money.new(995, 'NZD')),
Pack.new(5, Money.new(1695, 'NZD')),
Pack.new(8, Money.new(2495, 'NZD'))]
end

[threepack, fivepack]
end
def rockmelons
[Pack.new(3, Money.new(595, 'NZD')),
Pack.new(5, Money.new(995, 'NZD')),
Pack.new(9, Money.new(1699, 'NZD'))]
end
end
@@ -3,6 +3,20 @@
require './lib/handle_input'

describe HandleInput do
describe '#initialize' do
it 'should have an order attribute which is an instance of Order' do
instance = HandleInput.new(Order.new)

expect(instance.order).to be_a Order
end

it 'should have an invoice attribute which is an instance of Invoice' do
instance = HandleInput.new(Order.new)

expect(instance.invoice).to be_a Invoice
end
end

describe '#interpret' do
it 'should return nil unless command matches a value in ACTIONS array' do
instance = HandleInput.new(Order.new)
@@ -15,31 +29,55 @@

expect(instance.interpret('VIEW')).to be_nil
end

before do
$stdin = StringIO.new('3 watermelons')
$stdin = StringIO.new('BACK')
end

it 'should return nil if command matches SHOP' do
instance = HandleInput.new(Order.new)

expect(instance.interpret('SHOP')).to be_nil
end
end

describe '#shop_menu' do
before do
$stdin = StringIO.new('3 watermelons')
$stdin = StringIO.new('BACK')
end

it 'should return nil' do
instance = HandleInput.new(Order.new)

expect(instance.send(:shop_menu)).to be_nil
end
end

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

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

expect(instance.shop('10 watermelon')).to be_a OrderLine
expect(instance.send(:shop, '10 watermelons')).to be_a OrderLine
end

it 'should return nil if input does not match PATTERN' do
instance = HandleInput.new(Order.new)

expect(instance.shop('fruit')).to be_nil
expect(instance.send(:shop, 'fruit')).to be_nil
end
end

describe '#exec' do
it 'should return an instance of OrderLine' do
instance = HandleInput.new(Order.new)

expect(instance.exec(OrderLine.new(10, Item.new('watermelon')))).to be_a OrderLine
expect(instance.send(:exec, OrderLine.new(10, Item.new('watermelons')))).to be_a OrderLine
end
end
end
@@ -5,26 +5,58 @@
describe Item do
describe '#initialize' do
it 'should have a name attribute which is a String' do
instance = Item.new('Watermelons')
instance = Item.new('watermelons')

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

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

expect(instance.pack).to be_nil
end
end

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

expect(instance.packs('watermelon')).to be_a Array
expect(instance.packs('watermelons')).to be_a Array
end
end

describe '#packs' do
it 'should nil unless fruit is valid' do
instance = Item.new('Watermelons')
instance = Item.new('watermelons')

expect(instance.packs('hello')).to be_nil
end

it 'should return packs available for the fruit which is an Array' do
instance = Item.new('watermelons')

expect(instance.packs('watermelons')).to be_a Array
end
end

describe '#watermelons' do
it 'should an Array' do
instance = Item.new('watermelons')

expect(instance.send(:watermelons)).to be_a Array
end

it 'should an Array' do
instance = Item.new('pineapples')

expect(instance.send(:pineapples)).to be_a Array
end

it 'should an Array' do
instance = Item.new('rockmelons')

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

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

it 'should have a order_item attribute which is an instance of Item' do
instance = OrderLine.new(3, Item.new('watermelon'))
instance = OrderLine.new(3, Item.new('watermelons'))

expect(instance.order_item).to be_a Item
end

it 'should have a order_packs attribute which is nil' do
instance = OrderLine.new(3, Item.new('watermelon'))
instance = OrderLine.new(3, Item.new('watermelons'))

expect(instance.order_packs).to be_nil
end
end

describe '#optimal' do
it 'should return total_packs[0] which is an Array' do
instance = OrderLine.new(12, Item.new('watermelon'))
instance = OrderLine.new(12, Item.new('watermelons'))

expect(instance.optimal(instance.order_item.name)).to be_a Array
end

it 'should return whole_packs[0] which is an Array' do
instance = OrderLine.new(10, Item.new('watermelon'))
instance = OrderLine.new(10, Item.new('watermelons'))

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'))
instance = OrderLine.new(12, Item.new('watermelons'))

expect(instance.present_line(instance.optimal(instance.order_item.name))).to be_a Array
end
end

describe '#presenter_line_total' do
it 'should return line_total which is an instance of Money' do
instance = OrderLine.new(12, Item.new('watermelon'))
instance = OrderLine.new(12, Item.new('watermelons'))
instance.present_line(instance.optimal(instance.order_item.name))

expect(instance.presenter_line_total).to be_a Money
@@ -57,7 +57,7 @@

describe '#whole_packs' do
it 'should return an Array' do
instance = OrderLine.new(12, Item.new('watermelon'))
instance = OrderLine.new(12, Item.new('watermelons'))
pack_qtys = [[3, 6.99], [5, 8.99]]

expect(instance.send(:whole_packs, pack_qtys, [], @order_qty)).to be_a Array
@@ -66,7 +66,7 @@

describe '#left_over_items' do
it 'should return an Array' do
instance = OrderLine.new(12, Item.new('watermelon'))
instance = OrderLine.new(12, Item.new('watermelons'))
pack_qtys = [[3, 6.99], [5, 8.99]]

expect(instance.send(:left_over_items, pack_qtys, [], 1)).to be_a Array
@@ -15,7 +15,7 @@
describe '#add_item' do
it 'should return an Array of items' do
instance = Order.new
order_line = OrderLine.new(10, Item.new('watermelon'))
order_line = OrderLine.new(10, Item.new('watermelons'))
breakdown = order_line.present_line(order_line.optimal(order_line.order_item.name))

expect(instance.add_item(order_line, breakdown)).to be_a Array

0 comments on commit 56b6587

Please sign in to comment.
You can’t perform that action at this time.