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
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,zipcode
1,leonard.rogahn@hagenes.org,71596 Eden Route,Connellymouth,LA,98872-9105
2,ruben_nikolaus@kreiger.com,876 Kemmer Cove,East Luellatown,AL,21362
3,edison.mclaughlin@hyattjohns.co,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_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
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'

class Customer
attr_reader :id
attr_accessor :email, :address
def initialize(id, email, address)
@id = id
@email = email
@address = address
end

def self.all
data = CSV.open("data/customers.csv", headers:true).map(&:to_h)
customers = []
data.each do | data_hash |
id = data_hash["id"]
email = data_hash["email"]
address = {
"address_1" => data_hash["address_1"],
"city" => data_hash["city"],
"state" => data_hash["state"],
"zipcode" => data_hash["zipcode"]
}

customer = self.new(id, email, address)
customers << customer
end

return customers
end

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


end
98 changes: 98 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'csv'
require_relative "customer"

class Order
# attribute
attr_reader :id
attr_accessor :products, :customer, :fulfillment_status
def initialize(id, products, customer, fulfillment_status = :pending)

# product should be a data of hash
@id = id
@products = products
@customer = customer
@fulfillment_status = fulfillment_status

if fulfillment_status != :pending && fulfillment_status != :paid && fulfillment_status != :processing && fulfillment_status != :shipped && fulfillment_status != :complete

puts fulfillment_status
raise ArgumentError.new("fullfillment fulfillment_status should be either :pending, :paid, :shipped or :complete")
end
end

def total
total_cost = 0

if @products.empty?
return 0
end

values = @products.values
product_total = values.sum
total_cost = product_total + (0.075 * product_total)
total_cost = total_cost.round(2)

return total_cost
end

def add_product(name, price)
if @products[name]
raise ArgumentError.new("product already there")
end

@products[name] = price
end

def remove_product(name)
if !@products[name]
raise ArgumentError.new("product not found.")
end
@products.delete(name)
end

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

data.each do | data_hash |
puts data_hash
products_data = data_hash["products"].split(";")

products = self.get_products(products_data)
customer_id = data_hash["customer_id"]
customer = Customer.find(customer_id)
id = data_hash["id"].to_i
fulfillment_status = data_hash["status"].to_sym

order = self.new(id, products, customer, fulfillment_status)
orders << order
end

return orders
end

def self.get_products(products_data)

products = {}
products_data.each do | name_price |
product_name_price = name_price.split(":")
name = product_name_price[0]
price = product_name_price[1].to_f
products[name] = price
end

return products
end

def self.find(order_id)
order = Order.all.find{ |order| order.id == order_id }

# binding.pry
if order
return order
else
raise ArgumentError.new("Order not found with the given id of #{order_id}")
end
end

end