diff --git a/lib/customer.rb b/lib/customer.rb index e69de29b..bea07585 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -0,0 +1,42 @@ +require 'csv' + +class Customer + def initialize(id,email,address) + @id = id + @email = email + @address = address + end + + attr_reader :id + attr_accessor :email, :address + + def self.all + customers = [] + CSV.open("data/customers.csv").each do |line| + id = line[0].to_i + email = line[1].to_s + address = { + street: line[2], + city: line[3], + state: line[4], + zip: line[5] + } + + customer = Customer.new(id,email,address) + customers << customer + end + + return customers + end + + def self.find(id) + customers = Customer.all + customers.each { |customer| return customer if customer.id == id } + return nil + end +end + + + + + diff --git a/lib/order.rb b/lib/order.rb index e69de29b..a96c42db 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -0,0 +1,75 @@ +require 'CSV' +require_relative 'customer' + +class Order + def initialize(id, products,customer,fulfillment_status = :pending) + @id = id + @products = products + @customer = customer + + case fulfillment_status + when :pending, :paid, :processing, :shipped, :complete + @fulfillment_status = fulfillment_status + else + raise ArgumentError.new "Invalid fufillmemt status entered" + end + end + + attr_reader :id, :products, :customer, :fulfillment_status + + def total + sum = @products.sum {|product,cost| cost} + total = (sum * 1.075).round(2) + return total + end + + def add_product(product_name, price) + if @products.key?(product_name) + raise ArgumentError.new "Added product already exists" + else + @products[product_name] = price + end + return @products + end + + def remove_product(product_name) + if @products.key?(product_name) + @products.delete(product_name) + else + raise ArgumentError.new "Deleted product doesn't exist" + end + return @products + end + + def self.formatproducts(products) + producthash = {} + productslist = products.split(';') + productslist.each do |product| + item = product.split(':') + productkey = item[0] + productcost = item[1].to_f + producthash[productkey] = productcost + end + return producthash + end + + def self.all + orders = [] + CSV.open("data/orders.csv").each do |line| + id = line[0].to_i + customer = Customer.find(line[-2].to_i) + fulfillment_status = line[-1].to_sym + products = Order.formatproducts(line[1]) + + order = Order.new(id, products,customer,fulfillment_status) + orders << order + end + return orders + end + + def self.find(order_id) + orders = Order.all + orders.each { |order| return order if order.id == order_id } + return nil + end +end diff --git a/lib/test.rb b/lib/test.rb new file mode 100644 index 00000000..2b8532f8 --- /dev/null +++ b/lib/test.rb @@ -0,0 +1,17 @@ +require_relative 'customer' + +def formatproducts(products) + producthash = {} + productslist = products.split(';') + productslist.each do |product| + item = product.split(':') + productkey = item[0] + productcost = item[1].to_f + producthash[productkey] = productcost + end + return producthash + end + +products = formatproducts('Lobster:17.18;Annatto seed:58.38;Camomile:83.21') + +print products \ No newline at end of file diff --git a/test/customer_test.rb b/test/customer_test.rb index 54889400..f0cb7906 100644 --- a/test/customer_test.rb +++ b/test/customer_test.rb @@ -6,7 +6,7 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -describe "Customer Wave 1" do +xdescribe "Customer Wave 1" do ID = 123 EMAIL = "a@a.co" ADDRESS = { @@ -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..957b9c53 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -111,13 +111,54 @@ expect(order.total).must_equal before_total end end + + describe '#remove_product' do + it "decreases the number of products" do + products = { "banana" => 1.99, "cracker" => 3.00 } + before_count = products.count + order = Order.new(1337, products, customer) + + result = order.remove_product("banana") + expected_count = before_count - 1 + expect(result.count).must_equal expected_count + end + + it "Is removed from the collection of products" do + products = { "banana" => 1.99, "cracker" => 3.00 } + order = Order.new(1337, products, customer) + + thing = order.remove_product("banana") + expect(order.products.include?("banana")).must_equal false + end + + it "Raises an ArgumentError if the product does not already exist" do + products = { "banana" => 1.99, "cracker" => 3.00 } + + order = Order.new(1337, products, customer) + before_total = order.total + + expect { + order.remove_product("apple") + }.must_raise ArgumentError + + expect(order.total).must_equal before_total + end + end 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 @@ -132,7 +173,6 @@ order = Order.all.first - # 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 @@ -141,21 +181,44 @@ 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 + + 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! + first = Order.find(100) + + expect(first).must_be_kind_of Order + expect(first.id).must_equal 100 end it "Returns nil for an order that doesn't exist" do - # TODO: Your test code here! + first = Order.find(10000) + + expect(first).must_equal nil end end end