Skip to content
Permalink
Browse files

Define Invoice object

  • Loading branch information...
SelenaSmall committed Sep 3, 2017
1 parent e7df573 commit f232c0bf3bcbbc054b6c69d2fe1b01bdd50a0d18
Showing with 119 additions and 33 deletions.
  1. +0 −8 lib/basket.rb
  2. +5 −3 lib/handle_input.rb
  3. +21 −0 lib/invoice.rb
  4. +1 −1 lib/order.rb
  5. +5 −7 lib/order_line.rb
  6. +0 −13 spec/basket_spec.rb
  7. +20 −0 spec/handle_input_spec.rb
  8. +21 −0 spec/invoice_spec.rb
  9. +46 −1 spec/order_line_spec.rb

This file was deleted.

@@ -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
@@ -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)$/
@@ -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
@@ -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
@@ -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

This file was deleted.

@@ -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
@@ -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
@@ -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
@@ -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
@@ -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.
You can’t perform that action at this time.