diff --git a/data/customers.csv b/data/customers.csv index 90580a89..68ce78d0 100644 --- a/data/customers.csv +++ b/data/customers.csv @@ -1,3 +1,4 @@ +id,email,street,city,state,zip 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 diff --git a/data/orders.csv b/data/orders.csv index 597a5ce1..96309d2e 100644 --- a/data/orders.csv +++ b/data/orders.csv @@ -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 diff --git a/lib/customer.rb b/lib/customer.rb index e69de29b..731f45ca 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -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) + customers = [] + + csv.each do |customer| + id = customer["id"].to_i + email = customer["email"] + address = { + 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 diff --git a/lib/order.rb b/lib/order.rb index e69de29b..57fd7cc6 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -0,0 +1,64 @@ +require_relative 'customer' +require 'csv' +require 'awesome_print' + +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 + + 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 + diff --git a/test/customer_test.rb b/test/customer_test.rb index 54889400..85b985e4 100644 --- a/test/customer_test.rb +++ b/test/customer_test.rb @@ -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 diff --git a/test/order_test.rb b/test/order_test.rb index cdb2aec7..cb8b6426 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -1,7 +1,7 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' - +require 'pry' require_relative '../lib/customer' require_relative '../lib/order' @@ -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) @@ -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) @@ -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) @@ -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 @@ -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