Skip to content

Commit

Permalink
Define Invoice object
Browse files Browse the repository at this point in the history
  • Loading branch information
SelenaSmall committed Sep 3, 2017
1 parent e7df573 commit f232c0b
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 33 deletions.
8 changes: 0 additions & 8 deletions lib/basket.rb

This file was deleted.

8 changes: 5 additions & 3 deletions lib/handle_input.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
require_relative 'item'
require_relative 'invoice'
require_relative 'order_line'

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

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

def initialize(order)
@order = order
@invoice = Invoice.new
end

# Interpret method
Expand Down Expand Up @@ -41,8 +43,8 @@ def interpret(command)
end
end

# show invoice total
puts order.find_order_total if command == 'VIEW'
# View Invoice
return invoice.print_order(order) if command == 'VIEW'
end

PATTERN = /^\s*\d+\s*(watermelon||pineapple||rockmelon)$/
Expand Down
21 changes: 21 additions & 0 deletions lib/invoice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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"
puts "qty\titem\t\t\tsub"
puts '-------------------------------------'
order.items.each do |item|
puts "#{item[0].order_qty} #{item[0].order_item.name} \t\t\t#{item[0].presenter_line_total}"
item[1].each { |i| puts "\t#{i}\n" }
end
puts '-------------------------------------'
puts "TOTAL \t\t\t\t#{order.find_order_total} \n\n"
end
end
2 changes: 1 addition & 1 deletion lib/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def add_item(order_item, breakdown)
def find_order_total
total = 0
@items.each do |item|
total += item[0].total_quantity_of_items
total += item[0].presenter_line_total
end

total
Expand Down
12 changes: 5 additions & 7 deletions lib/order_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@ def optimal(product)
total_packs[0]
end

def present_line(whole_packs)
def present_line(packs)
sub_total = []
sub_item = []
whole_packs.each do |k, v, i|
sub_item << "#{k}x #{v} packs @ #{i} "
packs.each do |k, v, i|
sub_total << k * i
sub_item << "#{k}x #{v} packs @ #{i}"
end

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

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

sub_item.each { |s| puts s }
sub_item.each { |s| s }
end

def total_quantity_of_items
def presenter_line_total
@line_total
end

Expand Down
13 changes: 0 additions & 13 deletions spec/basket_spec.rb

This file was deleted.

20 changes: 20 additions & 0 deletions spec/handle_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

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

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

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

describe '#shop' do
Expand All @@ -21,5 +27,19 @@

expect(instance.shop('10 watermelon')).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
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
end
end
end
21 changes: 21 additions & 0 deletions spec/invoice_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rspec'
require 'spec_helper'
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

expect(instance.print_order(Order.new)).to be_nil
end
end
end
47 changes: 46 additions & 1 deletion spec/order_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,32 @@
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'))

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

expect(instance.order_packs).to be_nil
end
end

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

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

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

describe '#present_line' do
Expand All @@ -27,4 +45,31 @@
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.present_line(instance.optimal(instance.order_item.name))

expect(instance.presenter_line_total).to be_a Money
end
end

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

expect(instance.send(:whole_packs, pack_qtys, [], @order_qty)).to be_a Array
end
end

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

expect(instance.send(:left_over_items, pack_qtys, [], 1)).to be_a Array
end
end
end

0 comments on commit f232c0b

Please sign in to comment.