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 - Dominique Taylor #29

Open
wants to merge 2 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
3 changes: 2 additions & 1 deletion data/orders.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
id,products,customer_id,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
4,Aubergine:56.71;Brown rice vinegar:33.52;dried Chinese Broccoli:51.74,35,paid
5,Sour Dough Bread:35.11,21,shipped
6,Peaches:46.34,14,pending
7,Eggs:84.23;Watermelon:11.16;Cherries:10.4,24,complete
8,Tahini:74.52;Chickory:43.8;Cos lettuce:33.4,12,complete
8,Tahini:74.52;Chickoprory:43.8;Cos lettuce:33.4,12,complete
9,Iceberg lettuce:88.51;Rice paper:66.35;Amaranth:1.5;Walnut:65.26,14,paid
10,Jerusalem Artichoke:59.92,26,complete
11,Pinto Beans:68.42,4,pending
Expand Down
42 changes: 42 additions & 0 deletions lib/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'csv'

class Customer
attr_reader :id
attr_accessor :email, :address

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

def self.all
#this is a class method bc it calls a method on itself
customer_array = []

customer_data = CSV.read("data/customers.csv", headers: true).map(&:to_h)

customer_data.each do |info|
customer = Customer.new( info["id"].to_i, info["email"], {
street: info["street"],
city: info["city"],
state: info["state"],
zip: info["zip"]
})
customer_array << customer
end
return customer_array
end

def self.find(id)
Customer.all.each do |customer_instance|
return customer_instance if id == customer_instance.id
# when you're in a class method, you have to type
# "customer_instance.id" instead of just @id to get the
# integer. @id only has access to the class when it is inside
# an instance method. Self.find is a class method.
end
return nil
end
end

92 changes: 92 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require 'csv'
require 'pry'

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

def initialize(id, products, customer, fulfillment_status = :pending)
# initialize is a special instance method of .new
@id = id
@products = products
@customer = customer
@fulfillment_status = fulfillment_status

fulfillment_status_array = [:pending, :paid, :processing, :shipped, :complete]
# fulfillment_status must equal one of these options, but is set to
# :pending default.

if fulfillment_status_array.include?(fulfillment_status) == false
raise ArgumentError, "You must provide a valid fulfillment status
#{fulfillment_status_array.join(",")}"
end
end

def total
products_total_cost = @products.values.sum * 1.075
return ('%.2f' % (products_total_cost)).to_f
# Got the percentage by 7.5/100 and then
# adding 1 to 0.075
end

def add_product(product_name, price)
if @products.keys.include?(product_name) == false
@products[product_name] = price
elsif
raise ArgumentError, "You must provide a product with a different
name"
end
end

def self.all
orders_data = CSV.read("data/orders.csv", headers: true).map(&:to_h)

order_array = []

orders_data.each_with_index do |products, index|
product_variable = Order.parse_products(orders_data)
order = Order.new(products["id"].to_i,
product_variable[index], Customer.find(products["customer_id"].to_i), products["status"].to_sym)
order_array << order
end
return order_array
end

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

def self.parse_products(orders)

Choose a reason for hiding this comment

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

I like that you made a helper here!

products_array = []
product_hash = {}

orders.each do |products1|
product_hash = {}
products_split_by_semicolon = products1["products"].split(';')

products_split_by_colon = products_split_by_semicolon.map do |products2|
products2.split(':')
end

products_split_by_colon.each do |products3|
product_key = products3[0]
product_value = products3[1]
product_hash[product_key] = product_value.to_f
end
products_array.push(product_hash)
end
products_array
end
end









8 changes: 3 additions & 5 deletions test/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
end
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

expect(customers.length).must_equal 35
customers.each do |c|
expect(c).must_be_kind_of Customer
Expand All @@ -61,7 +60,6 @@

it "Returns accurate information about the last customer" do
last = Customer.all.last

expect(last.id).must_equal 35
expect(last.email).must_equal "[email protected]"
expect(last.address[:street]).must_equal '7513 Kaylee Summit'
Expand All @@ -79,7 +77,7 @@
expect(first.id).must_equal 1
end

it "Can find the last customer from the CSV" do
it "Can find the last customer from the CSV" do
last = Customer.find(35)

expect(last).must_be_kind_of Customer
Expand Down
33 changes: 24 additions & 9 deletions test/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,21 @@
end
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!
order = Order.all

expect(order.length).must_equal 100
order.each do |c|
expect(c).must_be_kind_of Order

expect(c.id).must_be_kind_of Integer
expect(c.products).must_be_kind_of Hash
expect(c.customer).must_be_kind_of Customer
expect(c.fulfillment_status).must_be_kind_of Symbol
end
end

it "Returns accurate information about the first order" do
Expand All @@ -141,21 +151,26 @@
end

it "Returns accurate information about the last order" do
# TODO: Your test code here!
last = Order.all.last
expect(last.id).must_equal 100
expect(last.products["Amaranth"]).must_equal 83.81
expect(last.products["Smoked Trout"]).must_equal 70.6
expect(last.products["Cheddar"]).must_equal 5.63
expect(last.customer).must_be_kind_of Customer
expect(last.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!
end
first = Order.find(1)

it "Can find the last order from the CSV" do
# TODO: Your test code here!
expect(first).must_be_kind_of Order
expect(first.id).must_equal 1
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
end