Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add orderline to order and calculate order total
  • Loading branch information
SelenaSmall committed Sep 2, 2017
1 parent 120a507 commit ddd08f5
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 24 deletions.
7 changes: 4 additions & 3 deletions app.rb
@@ -1,13 +1,14 @@
#!/usr/bin/ruby

require_relative './lib/order'
require_relative './lib/handle_input'

command = HandleInput.new

$stdout.print "Would you like to LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"
command = HandleInput.new(Order.new)

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

input = gets.chomp

next if input.empty?
Expand Down
45 changes: 37 additions & 8 deletions lib/handle_input.rb
@@ -1,11 +1,18 @@
require_relative 'item'
# require_relative 'order'
require_relative 'order_line'

# HandleInput class
class HandleInput
attr_reader :order

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

def initialize(order)
@order = order
end

# Interpret method
# @param command [String]
# @return
Expand All @@ -18,7 +25,26 @@ def interpret(command)
# list all availble items and package sizes
end

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

loop do
input = $stdin.gets.chomp

next if input.empty?

unless 'BACK'.match?(input)
shop(input)
next
end

$stdout.print "Returning to main menu \n"
break
end
end

# loop { shop if command == 'SHOP' }

if command == 'VIEW'
# show invoice
Expand All @@ -29,13 +55,8 @@ def interpret(command)
end
end

def shop
# retrieve another input
$stdout.print "Select qty and items, example: 3 watermelon \n"

input = $stdin.gets.chomp

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

# break it down into qty, item
line = input.split(/\W+/)
Expand All @@ -51,6 +72,14 @@ def shop
# Present order_line for invoice
order_line.present_line(product)

# order = Order.new

add_to_order = @order.add_item(order_line)

# puts add_to_order

puts order.find_order_total

order_line
end
end
18 changes: 15 additions & 3 deletions lib/order.rb
@@ -1,9 +1,21 @@
# Order class
class Order
attr_reader :items, :basket
attr_reader :items

def initialize(basket)
def initialize
@items = []
@basket = basket
end

def add_item(order_item)
@items.push(order_item)
end

def find_order_total
total = 0
@items.each do |item|
total += item.total_quantity_of_items
end

total
end
end
9 changes: 7 additions & 2 deletions lib/order_line.rb
Expand Up @@ -71,12 +71,17 @@ def present_line(whole_packs)
sub_total << k * i
end

total = sub_total.inject(0) { |sum, x| sum + x }
@line_total = sub_total.inject(0) { |sum, x| sum + x }

puts "#{@order_qty} #{@order_item.name} #{total}"
puts "#{@order_qty} #{@order_item.name} #{@line_total}"

sub_item.each do |s|
puts s
end
end

def total_quantity_of_items
# @order_qty * @line_total
@line_total
end
end
6 changes: 3 additions & 3 deletions spec/handle_input_spec.rb
Expand Up @@ -5,7 +5,7 @@
describe HandleInput do
describe '#interpret' do
it 'should return nil unless command matches a value in ACTIONS array' do
instance = HandleInput.new
instance = HandleInput.new(Order.new)

expect(instance.interpret('hello')).to be_nil
end
Expand All @@ -17,9 +17,9 @@
end

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

expect(instance.shop).to be_a OrderLine
expect(instance.shop('10 watermelon')).to be_a OrderLine
end
end
end
8 changes: 8 additions & 0 deletions spec/item_spec.rb
Expand Up @@ -19,4 +19,12 @@
expect(instance.packs('watermelon')).to be_a Array
end
end

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

expect(instance.packs('hello')).to be_nil
end
end
end
21 changes: 16 additions & 5 deletions spec/order_spec.rb
Expand Up @@ -4,17 +4,28 @@

describe Order do
describe '#initialize' do
it 'should have a items attribute which is an empty Array' do
instance = Order.new(Basket.new)
it 'should have an items attribute which is an empty Array' do
instance = Order.new

expect(instance.items).to be_a Array
expect(instance.items).to be_empty
end
end

describe '#add_item' do
it 'should return an Array of items' do
instance = Order.new
orderline = OrderLine.new(10, Item.new('watermelon'))

expect(instance.add_item(orderline)).to be_a Array
end
end

it 'should have a basket attribute which is an instance of Basket' do
instance = Order.new(Basket.new)
describe '#find_order_total' do
it 'should return 0 if there are no items in the items array' do
instance = Order.new

expect(instance.basket).to be_a Basket
expect(instance.find_order_total).to eq 0
end
end
end

0 comments on commit ddd08f5

Please sign in to comment.