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

Daniela Sanchez - Leaves #28

Open
wants to merge 4 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 'awesome_print'
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
#CSV is an array of Hashes were each hash is a customer.
csv = CSV.read('/Users/dnsanche/ada/week3/grocery-store/data/customers.csv', 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.

you can't submit code that has the absolute path to github, because I don't have a folder called 'dnsanche'! Use the relative path moving forward.

customers = []

csv.each do |customer|
id = customer["id"].to_i
email = customer["email"]
address = {

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 do this if you couldn't add headers to the '.csv' file.

street: customer["street"],
city: customer["city"],
state: customer["state"],
zip: customer["zip"],
}
customer = Customer.new(id,email,address)
customers << customer
end
return customers
end

def self.find(id)
if self.all[id-1] != nil
return self.all[id-1]
else
nil
end
end
end
64 changes: 64 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require_relative 'customer'
require 'csv'
require 'awesome_print'

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 you need attr_accessor for all of these?


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

if [:pending, :paid, :processing, :shipped, :complete].include?(fulfillment_status) == false
raise ArgumentError.new('Invalid fulfillment status.')
end
end

def add_product(product_name, product_price)
if @products.include?(product_name) == true
raise ArgumentError.new('Error. Product already exists in the list')
else
@products.store(product_name,product_price)
end
end

def total
total = 0
@products.each do |key, value|
price = value*(1.075)
total += price.round(2)
end
return total
end

def self.all
csv = CSV.read("/Users/dnsanche/ada/week3/grocery-store/data/orders.csv", headers: true).map(&:to_h)
orders = []
csv.each do |customer_order|
id = customer_order["id"].to_i
products = {}
customer_order["products"].split(";").each do |product|
name_and_price = product.split(":")
products.store(name_and_price[0], name_and_price[1].to_f)
end
customer_id = customer_order["customer"].to_i
customer = Customer.find(customer_id)
fulfillment_status = customer_order["fulfillment_status"].to_sym
orders << Order.new(id,products,customer,fulfillment_status)
end
return orders
end

def self.find(id)
if Order.all[id-1] != nil
return Order.all[id-1]
else
nil
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
47 changes: 38 additions & 9 deletions test/order_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require 'pry'
require_relative '../lib/customer'
require_relative '../lib/order'

Expand All @@ -19,7 +19,7 @@
end

describe "#initialize" do
it "Takes an ID, collection of products, customer, and fulfillment_status" do
it "Takes an ID, collection of products, customer, and fulfillment_status" do
id = 1337
fulfillment_status = :shipped
order = Order.new(id, {}, customer, fulfillment_status)
Expand Down Expand Up @@ -62,7 +62,7 @@
end

describe "#total" do
it "Returns the total from the collection of products" do
it "Returns the total from the collection of products" do
products = { "banana" => 1.99, "cracker" => 3.00 }
order = Order.new(1337, products, customer)

Expand All @@ -79,7 +79,7 @@
end

describe "#add_product" do
it "Increases the number of products" do
it "Increases the number of products" do
products = { "banana" => 1.99, "cracker" => 3.00 }
before_count = products.count
order = Order.new(1337, products, customer)
Expand Down Expand Up @@ -114,10 +114,19 @@
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!
orders = Order.all
expect(orders.length).must_equal 100

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

end

it "Returns accurate information about the first order" do
Expand All @@ -142,20 +151,40 @@

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

last_order = Order.all.last

# Check that all data was loaded as expected
expect(last_order.id).must_equal id
expect(last_order.products).must_equal products
expect(last_order.customer).must_be_kind_of Customer
expect(last_order.customer.id).must_equal customer_id
expect(last_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 = Customer.find(1)
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.id).must_equal 100
end

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