diff --git a/lib/customer.rb b/lib/customer.rb index e69de29b..731bf482 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -0,0 +1,40 @@ +class Customer + + require "csv" + + attr_reader :id + attr_accessor :email, :address + + def initialize(id, email, address) + @id = id + @email = email + @address = address + end + + + def self.all + customers = CSV.read('data/customers.csv') + new_cust_array = [] + customers.each do |array| + id = array[0].to_i + email = array[1] + address = {:street => "#{array[2]}", :city => "#{array[3]}", :state => "#{array[4]}", :zip => "#{array[5]}"} + new_cust_array << Customer.new(id, email, address) + end + return new_cust_array + end + + + def self.find(id) + customer_array = self.all + customer_array.each do |object| + if object.id == id + return object + end + end + return nil + end + +end + + diff --git a/lib/order.rb b/lib/order.rb index e69de29b..61dba5cb 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -0,0 +1,74 @@ +class Order + + attr_reader :id + attr_accessor :customer, :products, :fulfillment_status + + def initialize(id, products, customer, fulfillment_status = :pending) + unless fulfillment_status == :pending || fulfillment_status == :paid || fulfillment_status == :processing || fulfillment_status == :shipped || fulfillment_status == :complete + raise ArgumentError, "Not a valid fulfillment status." + end + @id = id + @customer = customer + @products = products + @fulfillment_status = fulfillment_status + end + + + def total + bill = 0 + + @products.each do |food, price| + if @products == nil + bill = 0 + else + bill += price + end + end + bill_with_tax = bill * 1.075 + return bill_with_tax.round(2) + end + + + def add_product(product_name, price) + if @products.key?(product_name) == false + @products[product_name] = price + else + raise ArgumentError, "This item is already in your cart." + end + end + + + def self.all + orders = CSV.read("data/orders.csv") + new_order_array = [] + orders.each do |one_order_object| + id = one_order_object[0].to_i + + products = one_order_object[1].split(";") + product_hash = {} + products.each do |one_product| + key_and_value = one_product.split(":") + product_hash[key_and_value[0]] = key_and_value[1].to_f + end + products = product_hash + + customer = Customer.find(one_order_object[2].to_i) + fulfillment_status = one_order_object[3].to_sym + new_order_array << Order.new(id, products, customer, fulfillment_status) + end + return new_order_array + end + + + def self.find(id) + order_array = Order.all + + order_array.each do |object| + if object.id == id + return object + end + end + return nil + end + +end diff --git a/test/customer_test.rb b/test/customer_test.rb index 54889400..ca5cf7b1 100644 --- a/test/customer_test.rb +++ b/test/customer_test.rb @@ -2,6 +2,9 @@ require 'minitest/reporters' require 'minitest/skip_dsl' +require 'pry' + + require_relative '../lib/customer' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new @@ -15,17 +18,16 @@ state: "WA", zip: "98101" }.freeze - + describe "#initialize" do it "Takes an ID, email and address info" do cust = Customer.new(ID, EMAIL, ADDRESS) - expect(cust).must_respond_to :id expect(cust.id).must_equal ID - + expect(cust).must_respond_to :email expect(cust.email).must_equal EMAIL - + expect(cust).must_respond_to :address expect(cust.address).must_equal ADDRESS end @@ -33,24 +35,24 @@ 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 - + expect(c.id).must_be_kind_of Integer expect(c.email).must_be_kind_of String expect(c.address).must_be_kind_of Hash end end - + it "Returns accurate information about the first customer" do first = Customer.all.first - + expect(first.id).must_equal 1 expect(first.email).must_equal "leonard.rogahn@hagenes.org" expect(first.address[:street]).must_equal "71596 Eden Route" @@ -58,10 +60,10 @@ expect(first.address[:state]).must_equal "LA" expect(first.address[:zip]).must_equal "98872-9105" end - + it "Returns accurate information about the last customer" do last = Customer.all.last - + expect(last.id).must_equal 35 expect(last.email).must_equal "rogers_koelpin@oconnell.org" expect(last.address[:street]).must_equal '7513 Kaylee Summit' @@ -70,22 +72,22 @@ expect(last.address[:zip]).must_equal '64529-2614' end end - + describe "Customer.find" do it "Can find the first customer from the CSV" do first = Customer.find(1) - + expect(first).must_be_kind_of Customer expect(first.id).must_equal 1 end - + it "Can find the last customer from the CSV" do last = Customer.find(35) - + expect(last).must_be_kind_of Customer expect(last.id).must_equal 35 end - + it "Returns nil for a customer that doesn't exist" do expect(Customer.find(53145)).must_be_nil end diff --git a/test/order_test.rb b/test/order_test.rb index cdb2aec7..2b81c494 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -17,40 +17,40 @@ } Customer.new(123, "a@a.co", address) end - + describe "#initialize" 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) - + expect(order).must_respond_to :id expect(order.id).must_equal id - + expect(order).must_respond_to :products expect(order.products.length).must_equal 0 - + expect(order).must_respond_to :customer expect(order.customer).must_equal customer - + expect(order).must_respond_to :fulfillment_status expect(order.fulfillment_status).must_equal fulfillment_status end - + it "Accepts all legal statuses" do valid_statuses = %i[pending paid processing shipped complete] - + valid_statuses.each do |fulfillment_status| order = Order.new(1, {}, customer, fulfillment_status) expect(order.fulfillment_status).must_equal fulfillment_status end end - + it "Uses pending if no fulfillment_status is supplied" do order = Order.new(1, {}, customer) expect(order.fulfillment_status).must_equal :pending end - + it "Raises an ArgumentError for bogus statuses" do bogus_statuses = [3, :bogus, 'pending', nil] bogus_statuses.each do |fulfillment_status| @@ -60,53 +60,53 @@ end end end - + describe "#total" do it "Returns the total from the collection of products" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Order.new(1337, products, customer) - + expected_total = 5.36 - + expect(order.total).must_equal expected_total end - + it "Returns a total of zero if there are no products" do order = Order.new(1337, {}, customer) - + expect(order.total).must_equal 0 end end - + describe "#add_product" 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) - + order.add_product("salad", 4.25) expected_count = before_count + 1 expect(order.products.count).must_equal expected_count end - + it "Is added to the collection of products" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Order.new(1337, products, customer) - + order.add_product("sandwich", 4.25) expect(order.products.include?("sandwich")).must_equal true end - + it "Raises an ArgumentError if the product is already present" do products = { "banana" => 1.99, "cracker" => 3.00 } - + order = Order.new(1337, products, customer) before_total = order.total - + expect { order.add_product("banana", 4.25) }.must_raise ArgumentError - + # The list of products should not have been modified expect(order.total).must_equal before_total end @@ -114,12 +114,21 @@ 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.fulfillment_status).must_be_kind_of Symbol + expect(o.products).must_be_kind_of Hash + end end - + it "Returns accurate information about the first order" do id = 1 products = { @@ -129,9 +138,9 @@ } customer_id = 25 fulfillment_status = :complete - + order = Order.all.first - + # Check that all data was loaded as expected expect(order.id).must_equal id expect(order.products).must_equal products @@ -139,23 +148,34 @@ expect(order.customer.id).must_equal customer_id expect(order.fulfillment_status).must_equal fulfillment_status 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).must_be_kind_of Hash + 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! + 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(125)).must_equal nil end end end