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..58fae8a6 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -0,0 +1,43 @@ +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 + + def self.all(file_name = 'data/customers.csv') + customers = [] + clients = CSV.read(file_name, headers: true).map(&:to_h) + clients.each do |client| + new_customer = Customer.new(client['id'].to_i, client['email'], { + street: client['street'], + city: client['city'], + state: client['state'], + zip: client['zip'] + }) + customers << new_customer + end + return customers + end + + def self.find(id) + if id.class != Integer + raise ArgumentError.new("Invalid id, the id must be an Integer") + end + + Customer.all.each do |customer| + if customer.id == id + return customer + end + end + return nil + #binding.pry + end + +end diff --git a/lib/order.rb b/lib/order.rb index e69de29b..72860c06 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -0,0 +1,62 @@ +class Order + attr_reader :id + attr_accessor :products, :customer, :fulfillment_status + + def initialize(id, products, customer, fulfillment_status = :pending) + puts fulfillment_status + if fulfillment_status != :pending && fulfillment_status != :paid && fulfillment_status != :processing && fulfillment_status != :shipped && fulfillment_status != :complete + raise ArgumentError.new("This is not a valid fulfillment_status") + end + @id = id + @products = products + @customer = customer + @fulfillment_status = fulfillment_status + end + + def total + products_subtotal = 0 + @products.each do |product,value| + products_subtotal += value + end + tax = products_subtotal * 0.075 + products_total = products_subtotal + tax + return products_total.round(2) + end + + def add_product(product_name, price) + if @products.key?(product_name) + raise ArgumentError.new("This product has been already added.") + else + @products[product_name] = price + end + end + + def self.all(file_name = 'data/orders.csv') + all_orders = [] + orders = CSV.read(file_name, headers: true).map(&:to_h) + # create a hash of products + orders.each do |order| + array_of_products = order['products'].split(';').map do |product| + product.split(':') + end + products = Hash[array_of_products.map {|product, cost| [product, cost.to_f]}] + customer = Customer.find(order['customer'].to_i) + new_order = Order.new(order['id'].to_i, products, customer, order['fulfillment_status'].to_sym) + all_orders << new_order + end + return all_orders + end + + def self.find(id) + if id.class != Integer + raise ArgumentError.new("Invalid id, the id must be an Integer") + end + + Order.all.each do |order| + if order.id == id + return order + end + end + return nil + end +end \ No newline at end of file diff --git a/test/customer_test.rb b/test/customer_test.rb index 54889400..796288d9 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 @@ -90,4 +90,15 @@ expect(Customer.find(53145)).must_be_nil end end + + # My test + it 'raises an ArgumentError if a string is provided' do + expect { + Customer.find("25") + }.must_raise ArgumentError + end + + + + end diff --git a/test/order_test.rb b/test/order_test.rb index cdb2aec7..4d295866 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -114,7 +114,7 @@ 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! @@ -141,21 +141,48 @@ 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(100) + expect(last).must_be_kind_of Order + 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(53145)).must_be_nil end end + + # My test + it 'raises an ArgumentError if a string is provided' do + expect { + Order.find("25") + }.must_raise ArgumentError + end end