Skip to content
Permalink
Browse files

List available items and pack sizes. Move shop methods out into their…

… own class
  • Loading branch information...
SelenaSmall committed Sep 3, 2017
1 parent 56b6587 commit 4b96f158a107d02dcc69d58fff9ce34d742f3f35
Showing with 145 additions and 104 deletions.
  1. +1 −1 app.rb
  2. +7 −50 lib/handle_input.rb
  3. +0 −6 lib/invoice.rb
  4. +24 −0 lib/list.rb
  5. +48 −0 lib/shop.rb
  6. +0 −39 spec/handle_input_spec.rb
  7. +0 −8 spec/invoice_spec.rb
  8. +13 −0 spec/list_spec.rb
  9. +52 −0 spec/shop_spec.rb
2 app.rb
@@ -7,7 +7,7 @@

# Read user input
loop do
$stdout.print "Main menu: LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"
$stdout.print "Main menu: LIST available products, SHOP (add items to your order), VIEW basket, EXIT \n"

input = gets.chomp

@@ -1,17 +1,21 @@
require_relative 'item'
require_relative 'invoice'
require_relative 'list'
require_relative 'shop'
require_relative 'order_line'

# HandleInput class
class HandleInput
attr_reader :order, :invoice
attr_reader :order, :invoice, :list, :shop

# Actions
ACTIONS = %w[LIST SHOP VIEW].freeze

def initialize(order)
@order = order
@invoice = Invoice.new
@list = List.new
@shop = Shop.new(order)
end

# Interpret method
@@ -20,57 +24,10 @@ def initialize(order)
def interpret(command)
return unless ACTIONS.detect { |a| a == command }

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

return shop_menu if command.match?('SHOP')
return shop.menu if command.match?('SHOP')

return invoice.print_order(order) if command.match?('VIEW')
end

private

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"

loop do
input = $stdin.gets.chomp

next if input.empty?

break if 'BACK'.match?(input)

shop(input)
next
end
end

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

# Order items
def shop(input)
unless input.match?(PATTERN)
puts "That's not a valid input"
return
end

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

exec(OrderLine.new(line[0].to_i, Item.new(line[1])))
end

# 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
end
@@ -1,11 +1,5 @@
# Invoice class
class Invoice
attr_reader :printorder

def initialize
@printorder = []
end

def print_order(order)
puts "Customer Invoice \n"
puts "Items rounded up to nearest available qty\n"
@@ -0,0 +1,24 @@
# List class
class List
def selection
puts "Products Available \n"

puts 'Watermelons'
Item.new('watermelons').packs('watermelons').each do |i|
puts "#{i.qty} pack @ #{i.price}"
end
puts "---------------\n"

puts 'Pineapples'
Item.new('pineapples').packs('pineapples').each do |i|
puts "#{i.qty} pack @ #{i.price}"
end
puts "---------------\n"

puts 'Rockmelons'
Item.new('rockmelons').packs('rockmelons').each do |i|
puts "#{i.qty} pack @ #{i.price}"
end
puts "---------------\n"
end
end
@@ -0,0 +1,48 @@
# Shop class
class Shop
attr_reader :order
# Order input pattern
PATTERN = /^\s*\d+\s*(watermelons||pineapples||rockmelons)$/

def initialize(order)
@order = order
end

def 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"

loop do
input = $stdin.gets.chomp

next if input.empty?

break if 'BACK'.match?(input)

choose_items(input)
next
end
end

def choose_items(input)
unless input.match?(PATTERN)
puts "That's not a valid input"
return
end

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

exec(OrderLine.new(line[0].to_i, Item.new(line[1])))
end

def exec(order_line)
order.add_item(
order_line,
order_line.present_line(
order_line.optimal(order_line.order_item.name)
)
)

order_line
end
end
@@ -41,43 +41,4 @@
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 watermelons')
end

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

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.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.send(:exec, OrderLine.new(10, Item.new('watermelons')))).to be_a OrderLine
end
end
end
@@ -3,14 +3,6 @@
require './lib/invoice'

describe Invoice do
describe '#initialize' do
it 'should have a printorder attribute which is an Array' do
instance = Invoice.new

expect(instance.printorder).to be_a Array
end
end

describe '#print_order' do
it 'should return nil' do
instance = Invoice.new
@@ -0,0 +1,13 @@
require 'rspec'
require 'spec_helper'
require './lib/list'

describe List do
describe '#selection' do
it 'should return nil' do
instance = List.new

expect(instance.selection).to be_nil
end
end
end
@@ -0,0 +1,52 @@
require 'rspec'
require 'spec_helper'
require './lib/shop'

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

expect(instance.order).to be_a Order
end
end

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

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

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

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

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

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

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

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

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

expect(instance.send(:exec, OrderLine.new(10, Item.new('watermelons')))).to be_a OrderLine
end
end
end

0 comments on commit 4b96f15

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