Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaves - Mariya #44

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions lib/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'csv'

class Customer
attr_accessor :email, :address
attr_reader :id

def self.all
customer_list = []
CSV.read('data/customers.csv').each do |customer|
customer_object = Customer.new(customer[0].to_i, customer[1], {street: customer[2], city: customer[3], state: customer[4], zip: customer[5]})
customer_list << customer_object
end
return customer_list
end

def self.find(id)
Customer.all.find do |customer_object|
customer_object.id == id ? customer_object : nil
end
end

def initialize(id, email, address)
@id = id
@email = email
@address = address
end
end
93 changes: 93 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require_relative 'customer'
require 'csv'


class Order
attr_reader :id, :products
attr_accessor :customer, :fulfillment_status

def initialize(id, products, customer, fulfillment_status = :pending)
@id = id
@products = products
@customer = customer
@fulfillment_status = fulfillment_status

#raises Argument Error for invalid status
valid_statuses = %i[pending paid processing shipped complete]
if !(valid_statuses.include?(@fulfillment_status))
raise ArgumentError.new("Invalid status.")
end
end

def self.all
orders_list = []
#splits string at the ";" to seperate each product/cost pair
CSV.read('data/orders.csv').each do |order|
food_price_pairs = order[1].split(";")

products = {}
food_price_pairs.each do |food|
food_price = food.split(":") #splits the product & cost so that the price so that it can be added into a hash & the price can be converted into a float
products["#{food_price[0]}"] = food_price[1].to_f
end

order_object = Order.new(order[0].to_i, products, Customer.find(order[2].to_i), order[3].to_sym)
orders_list << order_object
end
return orders_list
end

def self.find(id)
Order.all.each do |order|
if order.id == (id)
return order
end
end
return nil
end

def self.find_by_customer(customer_id)
customer_orders = []
Order.all.each do |order|

Choose a reason for hiding this comment

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

This looks good! In case you want something shorter that does exactly what you implemented, you might want to check out: https://ruby-doc.org/core-2.6.1/Enumerable.html#method-i-find

if order.customer.id == customer_id
customer_orders << order
end
end

if customer_orders.length == 0
raise ArgumentError.new("Invalid customer ID.")
end
return customer_orders
end


def total
total = 0
@products.each do |product, amount|
total += amount
end

tax_amount = total * 0.075
total = total + tax_amount
return total.round(2)
end

def add_product(product_name, product_price)
if @products.has_key?(product_name)
raise ArgumentError.new("This product has already been added.")
else
@products[product_name] = product_price
end

return @products
end

def remove_products(product_name, product_price)
if @products.has_key?(product_name)
@products.delete(product_name)
else
raise ArgumentError.new("This product is not in our inventory.")
end
end

end
2 changes: 1 addition & 1 deletion test/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
end

# TODO: remove the 'x' in front of this block when you start wave 2
xdescribe "Customer Wave 2" do
describe "Customer Wave 2" do
describe "Customer.all" do
it "Returns an array of all customers" do
customers = Customer.all
Expand Down
94 changes: 84 additions & 10 deletions test/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,55 @@
end
end

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Order Wave 2" do
# test for optional remove_products
describe "#remove_products" do
it "Decreases the number of products" do
products = { "Eggs" => 84.23, "Watermelon" => 11.16, "Cherries" => 10.4 }
before_count = products.length
customer = Customer.find(7)
order = Order.new(7, products, customer)

order.remove_products("Eggs", 84.23)
after_count = before_count - 1
expect(order.products.include?("Eggs")).must_equal false

end

it "Raises an ArgumentError if the product is not present" do
products = { "Eggs" => 84.23, "Watermelon" => 11.16, "Cherries" => 10.4 }

customer = Customer.find(7)
order = Order.new(7, products, customer)

expect {
order.remove_products("banana", 4.25)
}.must_raise ArgumentError

# The list of products should not have been modified
expect(order.total).must_equal 113.72
end
end

describe "Order Wave 2" do
describe "Order.all" do
it "Returns an array of all orders" do
# TODO: Your test code here!
end
# get all instances of orders
orders = Order.all
# there should be 100 orders
expect(orders.length).must_equal 100
# confirm data types of attributes

orders.each do |order|
expect(order).must_be_kind_of Order
expect(order.id).must_be_kind_of Integer
expect(order.products).must_be_kind_of Hash
expect(order.customer).must_be_kind_of Customer
expect(order.fulfillment_status).must_be_kind_of Symbol
end


end

it "Returns accurate information about the first order" do
id = 1
products = {
Expand All @@ -141,21 +183,53 @@
end

it "Returns accurate information about the last order" do
# TODO: Your test code here!
# assign the last order object to value last_order
last_order = Order.all.last

# check that all data returned was matched to the last line (array) of the CSV file.
expect(last_order.id).must_equal 100
expect(last_order.products).must_equal ({"Amaranth" => 83.81, "Smoked Trout" => 70.6, "Cheddar" => 5.63})
expect(last_order.customer.id).must_equal 20
expect(last_order.fulfillment_status).must_equal :pending
end
end

describe "Order.find" do
it "Can find the first order from the CSV" do
# TODO: Your test code here!
#first is assigned the value of calling on Order.find
first = Order.find(1)

# check expected
expect(first.id).must_equal 1
expect(first.products).must_equal ({"Lobster" => 17.18, "Annatto seed" => 58.38, "Camomile"=> 83.21})
expect(first.customer.id).must_equal 25
expect(first.fulfillment_status).must_equal :complete
end

it "Can find the last order from the CSV" do
# TODO: Your test code here!
# last is assigned to the result of calling Order.find
last = Order.find(100)

# check expected values vs. actual values
expect(last.id).must_equal 100
expect(last.products).must_equal ({"Amaranth" => 83.81, "Smoked Trout" => 70.6, "Cheddar" => 5.63})
expect(last.customer.id).must_equal 20
expect(last.fulfillment_status).must_equal :pending
end

it "Returns nil for an order that doesn't exist" do
# TODO: Your test code here!

# optional enhancement
it "Can find the customer from the customer id" do
customer_orders = Order.find_by_customer(24)

expect((customer_orders.all? { |orders| orders.customer.id == 24 })).must_equal true
end


it "Returns nil for an order that doesn't exist" do
no_order = Order.find(101)

Choose a reason for hiding this comment

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

It might be nice to give a name to the value 101, like non_extant_order_id = 101


expect(no_order).must_equal nil
end
end
end