Skip to content
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
54 changes: 54 additions & 0 deletions lib/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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

# returns a collection of `Customer` instances, representing all of the Customer described in the CSV file

#Iterating through the allcustomers array per customer, or element in that array
#Taking each customer and extracting id at index 0

#I want to iterate over each customers(an arrray) addresses(also an array)
#Create a hash
#I want to assign keys (labels) to the values (data) for their addresses

#Each customer object here will be an instance of that customers array
def self.all
allcustomers = CSV.read('data/customers.csv').map(&:to_a)
customers = []

allcustomers.each do |customer_data|
customers << Customer.new(customer_data[0].to_i, customer_data[1], customer_data[2..5])
end

customers.each do |customer|
address = {}
address[:street] = customer.address[0]
address[:city] = customer.address[1]
address[:state] = customer.address[2]
address[:zip] = customer.address[3]

customer.address = address
end
return customers
end

# - `self.find(id)` - returns an instance of `Customer` where the value of the id field in the CSV matches the passed parameter
def self.find(id)
customers = Customer.all

found = customers.find do |customer|
customer.id == id
end
return found
end

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

def initialize(id, products, customer, fulfillment_status = :pending)
@id = id
@products = products
@customer = customer
@fulfillment_status = fulfillment_status
shipping = [:pending, :paid, :processing, :shipped, :complete]

if !shipping.include? @fulfillment_status
raise ArgumentError
end
end

def total
prices = @products.values
total_without_tax = prices.sum
tax = total_without_tax * 0.075
total_sum = tax + total_without_tax
total_sum.round(2)
#Initialize variable outside of loop
#Iterate through each key(product) + value(price) in the products hash
# calculate the total cost of the order by:
# Summing up the products
# Adding a 7.5% tax
# Rounding the result to two decimal places
end

def add_product(name, price)

if @products.key?(name)
raise ArgumentError
end

@products[name] = price
end

def self.all
all_orders = []

CSV.read('data/orders.csv').each do |order|
id = order[0].to_i
products = {}
@item_price_array = order[1].split(";")

#parse through product by item/price
@item_price_array.each do |product|
split_product = product.split(":")
item = split_product[0]
price = split_product[1].to_f
products_hash = {item => price}
products.merge!(products_hash)
end

customer = Customer.find(order[2].to_i)
status = order[3].to_sym
all_orders << Order.new(id.to_i, products, customer, status)

end
return all_orders
end


def self.find(id)
self.all.each do |order|
if id == order.id
return order
end
end
return nil
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
39 changes: 33 additions & 6 deletions test/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,16 @@
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(Order.all).must_be_instance_of Array
end
end

it "Returns accurate information about the first order" do
Expand All @@ -141,21 +147,42 @@
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(35)
expect(last).must_be_kind_of Order
expect(last.id).must_equal 35
end

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