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 - Georgina #27

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions data/customers.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
id,email,street,city,state,zip
1,[email protected],71596 Eden Route,Connellymouth,LA,98872-9105
2,[email protected],876 Kemmer Cove,East Luellatown,AL,21362
3,[email protected],96807 Cartwright Points,North Casper,MT,29547
Expand Down
1 change: 1 addition & 0 deletions data/orders.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
id,products,customer,fulfillment_status
1,Lobster:17.18;Annatto seed:58.38;Camomile:83.21,25,complete
2,Sun dried tomatoes:90.16;Mastic:52.69;Nori:63.09;Cabbage:5.35,10,paid
3,Vegetable spaghetti:37.83;Dates:90.88;WhiteFlour:3.24;Caraway Seed:54.29,5,processing
Expand Down
43 changes: 43 additions & 0 deletions lib/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'csv'
require 'pry'

class Customer
attr_reader :id
attr_accessor :email, :address

def initialize(id, email, address)
@id = id
@email = email
@address = address
end

def self.all(file_name = 'data/customers.csv')
customers = []
clients = CSV.read(file_name, headers: true).map(&:to_h)

Choose a reason for hiding this comment

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

It's worth thinking about how you would solve this problem if you couldn't edit the csv to have headers.

clients.each do |client|
new_customer = Customer.new(client['id'].to_i, client['email'], {
street: client['street'],
city: client['city'],
state: client['state'],
zip: client['zip']
})
customers << new_customer
end
return customers
end

def self.find(id)
if id.class != Integer
raise ArgumentError.new("Invalid id, the id must be an Integer")
end

Customer.all.each do |customer|
if customer.id == id
return customer
end
end
return nil
#binding.pry
end

end
62 changes: 62 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class Order
attr_reader :id
attr_accessor :products, :customer, :fulfillment_status

Choose a reason for hiding this comment

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

do all of these need to be accessors?


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

Choose a reason for hiding this comment

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

You need to take out puts statements before you submit! This is debugging code.

if fulfillment_status != :pending && fulfillment_status != :paid && fulfillment_status != :processing && fulfillment_status != :shipped && fulfillment_status != :complete
raise ArgumentError.new("This is not a valid fulfillment_status")
end
@id = id
@products = products
@customer = customer
@fulfillment_status = fulfillment_status
end

def total
products_subtotal = 0
@products.each do |product,value|
products_subtotal += value
end
tax = products_subtotal * 0.075
products_total = products_subtotal + tax
return products_total.round(2)
end

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

def self.all(file_name = 'data/orders.csv')
all_orders = []
orders = CSV.read(file_name, headers: true).map(&:to_h)
# create a hash of products
orders.each do |order|
array_of_products = order['products'].split(';').map do |product|
product.split(':')
end
products = Hash[array_of_products.map {|product, cost| [product, cost.to_f]}]
customer = Customer.find(order['customer'].to_i)
new_order = Order.new(order['id'].to_i, products, customer, order['fulfillment_status'].to_sym)
all_orders << new_order
end
return all_orders
end

def self.find(id)
if id.class != Integer
raise ArgumentError.new("Invalid id, the id must be an Integer")
end

Order.all.each do |order|
if order.id == id
return order
end
end
return nil
end
end
13 changes: 12 additions & 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 Expand Up @@ -90,4 +90,15 @@
expect(Customer.find(53145)).must_be_nil
end
end

# My test
it 'raises an ArgumentError if a string is provided' do
expect {
Customer.find("25")
}.must_raise ArgumentError
end




end
37 changes: 32 additions & 5 deletions test/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
end

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Order Wave 2" do
describe "Order Wave 2" do
describe "Order.all" do
it "Returns an array of all orders" do
# TODO: Your test code here!
Expand All @@ -141,21 +141,48 @@
end

it "Returns accurate information about the last order" do
# TODO: Your test code here!
id = 100
products = {
"Amaranth" => 83.81,
"Smoked Trout" => 70.6,
"Cheddar" => 5.63
}
customer_id = 20
fulfillment_status = :pending

order = Order.all.last

# Check that all data was loaded as expected
expect(order.id).must_equal id
expect(order.products).must_equal products
expect(order.customer).must_be_kind_of Customer
expect(order.customer.id).must_equal customer_id
expect(order.fulfillment_status).must_equal fulfillment_status
end
end

describe "Order.find" do
it "Can find the first order from the CSV" do
# TODO: Your test code here!
first = Order.find(1)
expect(first).must_be_kind_of Order
expect(first.id).must_equal 1
end

it "Can find the last order from the CSV" do
# TODO: Your test code here!
last = Order.find(100)
expect(last).must_be_kind_of Order
expect(last.id).must_equal 100
end

it "Returns nil for an order that doesn't exist" do
# TODO: Your test code here!
expect(Order.find(53145)).must_be_nil
end
end

# My test
it 'raises an ArgumentError if a string is provided' do
expect {
Order.find("25")
}.must_raise ArgumentError
end
end