Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bc61dc0
ran first test successfully with Dee holding my hand through it.
AngelaPoland Feb 15, 2018
185900f
added some personal comments
AngelaPoland Feb 15, 2018
99176e6
Worked on add product method - got new product into @product hash
AngelaPoland Feb 15, 2018
b3fada8
passed test for Returns false if the product is already present but w…
AngelaPoland Feb 15, 2018
164a95c
got all tests to work, attempted to do the optional but didn't get th…
AngelaPoland Feb 15, 2018
d50ee19
small change - started attemping the optional wave 1 remove product m…
AngelaPoland Feb 15, 2018
4ad66a6
Started Wave 2, created class method called self.all and was finally …
AngelaPoland Feb 16, 2018
82d6765
figured out first test in wave 2
AngelaPoland Feb 16, 2018
07b1144
I was totally wrong with thinking I was done with the all method. Jus…
AngelaPoland Feb 16, 2018
bc33cae
Slight fix on all method. BUT IT WORKS
AngelaPoland Feb 17, 2018
0d8bb7f
trying to figure out find method. tests are giving me errors but not …
AngelaPoland Feb 17, 2018
ef0b0cb
Still playing with find class method. not sure which direction to go
AngelaPoland Feb 17, 2018
f4ef834
this find method might be right? I've been through like 4 different v…
AngelaPoland Feb 17, 2018
0bf2422
Working on tests for find method. Still not sure I get how tests are …
AngelaPoland Feb 17, 2018
1e4bf2e
I FINISHED WAVE 2 AND THE WAVE 2 TESTS FINALLLLLYYYYYYYYYYY.
AngelaPoland Feb 18, 2018
187feeb
Started OnlineOrder class.
AngelaPoland Feb 19, 2018
00bedba
I think I finished the find_by_customer method
AngelaPoland Feb 19, 2018
898adcd
I'm having a really hard time with the tests. Besides the ones I also…
AngelaPoland Feb 20, 2018
4d2ec9d
small change
AngelaPoland Feb 20, 2018
e49bddc
fixed my .all method, line 35 fix for parameter definitions.
AngelaPoland Feb 20, 2018
07171c1
small changes with first test in online specs
AngelaPoland Feb 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions lib/online_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# online order
require 'awesome_print'
require 'csv'
require_relative "order.rb"
require 'pry'

#example

module Grocery

class OnlineOrder < Order

attr_reader :id, :products, :customer_id, :fulfillment_status

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can inherit the helper methods for id and products and just have:

attr_reader :customer_id, :fulfillment_status


def initialize(id, products, customer_id, fulfillment_status)
@id = id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have:

super(id, products)

here to initialize @id and @products

@products = products
@customer_id = customer_id
@fulfillment_status = fulfillment_status.to_sym

# tip from Katie: 16 and 22 are the customer id's that exsit but do not have orders
end

def self.all

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done!

array_of_orders = []
CSV.open("support/online_orders.csv", 'r').each do |line|
customer_id = line[2]
fulfillment_status = line[3].to_sym
product_list = line[1].split(';')
product_hash = {}
product_list.each do |item|
product = item.split(':')
product_hash[product[0]] = product[1]
end
array_of_orders << Grocery::OnlineOrder.new(line[0].to_i, product_hash, customer_id, fulfillment_status)
end
array_of_orders
end

# (shouldn't need to rewrite out a new self.find method since it inherits from Order class and should work the same.)

def self.find_by_customer(customer_id)

OnlineOrder.all.each do |single_order|
order_list = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably have order_list = [] outside the loop.

if single_order[2] == customer_id
order_list << single_order
end
return order_list
end
end

def total

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

shipping_fee = 10.0
return super + shipping_fee
end

def add_product(product_name, product_price)
if @status == :pending || @status == :paid
return false if @products.has_key?(product_name)
@products[product_name] = product_price
return true
else
raise ArgumentError.new("Status is neither pending nor paid")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done, but you could use the superclass' add_product method

if @status == :pending || @status == :paid
  super(product_name, product_price)
else
  raise ArgumentError.new("Status is neither pending nor paid")
end

end
end

end # end of OnlineOrder Class




end # end of Module Class
80 changes: 76 additions & 4 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,90 @@
require 'awesome_print'
require 'csv'



# manually choose the data from the first line of the CSV file and ensure you can create a new instance of your Order using that data

module Grocery


class Order
attr_reader :id, :products
# id: Integer, represents unique identifier
# input_products: hash that represents products with keys of product name and values of price (Float)

def initialize(id, products)
@id = id
@products = products
end

# "../support/orders.csv " #change path back to this when opening this file in ruby instead of through rake


def self.all
array_of_orders = []
CSV.open("support/orders.csv", 'r').each do |line|
# for each line I need to take the index[0] and make it a key of a hash and the value of that hash is then another hash (which is index[1] of the array) with key: product name , value: product price
# first step is turning two elements of an array into a hash. second step is taking the value of that hash and turning it into another hash

product_list = line[1].split(';')
product_hash = {}
product_list.each do |item|
product = item.split(':')
product_hash[product[0]] = product[1]
end
array_of_orders << Grocery::Order.new(line[0].to_i, product_hash)
end
array_of_orders
end

def self.find(id)
# returns an instance of Order where the value of the id field in the CSV matches the passed parameter.
#to call this Class method: Grocery::Order.find(id)

all_orders = Grocery::Order.all

order = nil

all_orders.each do |single_order|

if single_order.id == id
order = single_order
end
end
return order
end



def total
# TODO: implement total
subtotal = @products.values.sum
sum = subtotal + (subtotal * 0.075).round(2)
return sum
end

def add_product(product_name, product_price)
# TODO: implement add_product

return false if @products.has_key?(product_name)
@products[product_name] = product_price
return true
end #end of add_product method


# • Add a remove_product method to the Order class which will take in one parameter, a product name, and remove the product from the collection
# It should return true if the item was successfully remove and false if it was not

def remove_product(product_name)
#p @products
if @products.key?(product_name)
@products.delete(product_name)
return true
else
return false
end
end
end
end

end #end of Order class


end #end of module
130 changes: 78 additions & 52 deletions specs/online_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,114 @@
require 'minitest/skip_dsl'

# TODO: uncomment the next line once you start wave 3
# require_relative '../lib/online_order'
require_relative '../lib/online_order'
# You may also need to require other classes here

# Because an OnlineOrder is a kind of Order, and we've
# already tested a bunch of functionality on Order,
# we effectively get all that testing for free! Here we'll
# only test things that are different.

xdescribe "OnlineOrder" do
describe "OnlineOrder" do
describe "#initialize" do
it "Is a kind of Order" do
# Check that an OnlineOrder is in fact a kind of Order
all_orders = Grocery::OnlineOrder.all

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the description in the it block you should have a test that makes sure that an OnlineOrder is a subclass of Order.

So:

sample_order = Grocery::OnlineOrder.new(1, {seeds: 58}, 3, :paid)
sample_order.must_be_instance_of Grocery::Order

This should work because an OnlineOrder is an Order (inheritance).


# Instatiate your OnlineOrder here
# online_order =
# online_order.must_be_kind_of Grocery::Order
end
all_orders.class.must_equal Array

it "Can access Customer object" do
# TODO: Your test code here!
end
all_orders.each do |order|
order.must_be_instance_of Grocery::OnlineOrder
end

it "Can access the online order status" do
# TODO: Your test code here!
all_orders.count.must_equal 100
end
end

describe "#total" do
it "Adds a shipping fee" do
# TODO: Your test code here!
end
xit "Can access Customer object" do
# TODO: I did not create a Customer class.
end

it "Can access the online order status" do

all_orders = Grocery::OnlineOrder.all

all_orders[0][3].must_be_kind_of Symbol

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a 2D array, instead it's an array of OnlineOrders. So this should be:

all_orders[0].fulfillment_status.must_be_kind_of Symbol


it "Doesn't add a shipping fee if there are no products" do
# TODO: Your test code here!
end
end
end

describe "#add_product" do
it "Does not permit action for processing, shipped or completed statuses" do
# TODO: Your test code here!
end
xdescribe "#total" do
xit "Adds a shipping fee" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing tests for total

# TODO: Your test code here!
end

it "Permits action for pending and paid satuses" do
# TODO: Your test code here!
end
xit "Doesn't add a shipping fee if there are no products" do
# TODO: Your test code here!
end
end

describe "OnlineOrder.all" do
it "Returns an array of all online orders" do
# TODO: Your test code here!
end
xdescribe "#add_product" do
xit "Does not permit action for processing, shipped or completed statuses" do
# TODO: Your test code here!
end

it "Returns accurate information about the first online order" do
# TODO: Your test code here!
end
xit "Permits action for pending and paid satuses" do
# TODO: Your test code here!
end
end

it "Returns accurate information about the last online order" do
# TODO: Your test code here!
xdescribe "OnlineOrder.all" do
it "Returns an array of all online orders" do
all_orders = Grocery::OnlineOrder.all
# all_orders.must_be_kind_of Array
all_orders.class.must_equal Array

all_orders.each do |order|
order.must_be_instance_of Grocery::OnlineOrder
end

all_orders.count.must_equal 100
end

describe "OnlineOrder.find" do
it "Will find an online order from the CSV" do
# TODO: Your test code here!
end

it "Raises an error for an online order that doesn't exist" do
# TODO: Your test code here!
end
it "Returns accurate information about the first online order" do
all_orders = Grocery::OnlineOrder.all

all_orders[0].id.must_equal 1

all_orders[0].products.must_be_kind_of Hash

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also check if the products contains "Annatto seed" at 58.38 or something similar

end

it "Returns accurate information about the last online order" do
all_orders = Grocery::OnlineOrder.all

all_orders[-1].id.must_equal 100
all_orders[-1].products.must_be_kind_of Hash

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would also want to check that it has a correct product, customer_id and fulfillment status.


end

end

xdescribe "OnlineOrder.find" do
xit "Will find an online order from the CSV" do
# TODO: Your test code here!
end

describe "OnlineOrder.find_by_customer" do
it "Returns an array of online orders for a specific customer ID" do
# TODO: Your test code here!
end
xit "Raises an error for an online order that doesn't exist" do
# TODO: Your test code here!
end
end

it "Raises an error if the customer does not exist" do
# TODO: Your test code here!
end
xdescribe "OnlineOrder.find_by_customer" do
xit "Returns an array of online orders for a specific customer ID" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return an array, so:

Grocery::OnlineOrder.find(1).must_be_instance_of Array
Grocery::OnlineOrder.find(1).each do |element|
  element.must_be_instance_of Grocery::OnlineOrder
end

Grocery::OnlineOrder.find(1).must_be_instance_of Grocery::OnlineOrder
end

it "Returns an empty array if the customer has no orders" do
# TODO: Your test code here!
end
xit "Raises an error if the customer does not exist" do
# TODO: Your test code here!
end

xit "Returns an empty array if the customer has no orders" do
Grocery::OnlineOrder.find_by_customer(22).must_be_empty
end
end
Loading