From e46f1d58699a70e97cefe8d623391d30f74c0b0a Mon Sep 17 00:00:00 2001 From: eaball35 Date: Wed, 21 Aug 2019 14:31:04 -0700 Subject: [PATCH 1/9] Completed wave 1 initialize & total method --- lib/customer.rb | 10 ++++++++++ lib/order.rb | 25 +++++++++++++++++++++++++ lib/test.rb | 5 +++++ test/customer_test.rb | 2 +- 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 lib/test.rb diff --git a/lib/customer.rb b/lib/customer.rb index e69de29b..f74cfc91 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -0,0 +1,10 @@ +class Customer + def initialize(id,email_address,delivery_address) + @id = id + @email_address = email_address + @delivery_address = delivery_address + end + + attr_reader :id + attr_accessor :email_address, :delivery_address +end \ No newline at end of file diff --git a/lib/order.rb b/lib/order.rb index e69de29b..19e63b80 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -0,0 +1,25 @@ +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 + + +end \ No newline at end of file diff --git a/lib/test.rb b/lib/test.rb new file mode 100644 index 00000000..3512d79a --- /dev/null +++ b/lib/test.rb @@ -0,0 +1,5 @@ +products = { "banana" => 1.99, "cracker" => 3.00 } + +sum = products.sum {|product,value| value} + +puts sum \ No newline at end of file diff --git a/test/customer_test.rb b/test/customer_test.rb index 54889400..052196f7 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 = { From bf2dcf0a6516bae04c872b2f11dccf2a31c214bc Mon Sep 17 00:00:00 2001 From: eaball35 Date: Wed, 21 Aug 2019 15:07:23 -0700 Subject: [PATCH 2/9] Finished wave 1 including optionals --- lib/order.rb | 17 +++++++++++++++++ lib/test.rb | 4 ++-- test/order_test.rb | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index 19e63b80..57bda897 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -21,5 +21,22 @@ def total 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 end \ No newline at end of file diff --git a/lib/test.rb b/lib/test.rb index 3512d79a..d63fe2e3 100644 --- a/lib/test.rb +++ b/lib/test.rb @@ -1,5 +1,5 @@ products = { "banana" => 1.99, "cracker" => 3.00 } -sum = products.sum {|product,value| value} +products.delete("banana") -puts sum \ No newline at end of file +puts products \ No newline at end of file diff --git a/test/order_test.rb b/test/order_test.rb index cdb2aec7..f42d9fac 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -111,8 +111,43 @@ 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.all" do From 98a062b20938717cf1e70b805ebf9e83096c54d1 Mon Sep 17 00:00:00 2001 From: eaball35 Date: Thu, 22 Aug 2019 15:14:30 -0700 Subject: [PATCH 3/9] Finished wave 2 part 1 --- lib/customer.rb | 35 ++++++++++++++++++++++++++++++----- test/customer_test.rb | 4 ++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index f74cfc91..78aadd65 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -1,10 +1,35 @@ +require 'csv' +require 'awesome_print' + class Customer - def initialize(id,email_address,delivery_address) + def initialize(id,email,address) @id = id - @email_address = email_address - @delivery_address = delivery_address + @email = email + @address = address end attr_reader :id - attr_accessor :email_address, :delivery_address -end \ No newline at end of file + 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 +end + + + + + diff --git a/test/customer_test.rb b/test/customer_test.rb index 052196f7..cfb0434e 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 @@ -71,7 +71,7 @@ end end - describe "Customer.find" do + xdescribe "Customer.find" do it "Can find the first customer from the CSV" do first = Customer.find(1) From db1d3815b788d85a887a949ea4350978cd9f392b Mon Sep 17 00:00:00 2001 From: eaball35 Date: Thu, 22 Aug 2019 15:23:03 -0700 Subject: [PATCH 4/9] Finished wave 2 customers --- lib/customer.rb | 54 +++++++++++++++++++++++++------------------ test/customer_test.rb | 2 +- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index 78aadd65..6ffb2830 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -2,31 +2,39 @@ require 'awesome_print' class Customer - def initialize(id,email,address) - @id = id - @email = email - @address = address + 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 - 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 + return customers + end + + def self.find(id) + customers =self.all + customers.each { |customer| return customer if customer.id == id } + return nil + end end diff --git a/test/customer_test.rb b/test/customer_test.rb index cfb0434e..f0cb7906 100644 --- a/test/customer_test.rb +++ b/test/customer_test.rb @@ -71,7 +71,7 @@ end end - xdescribe "Customer.find" do + describe "Customer.find" do it "Can find the first customer from the CSV" do first = Customer.find(1) From 5ab7880441bd9eb6eb1662a236eb362a90620e9c Mon Sep 17 00:00:00 2001 From: eaball35 Date: Thu, 22 Aug 2019 16:18:21 -0700 Subject: [PATCH 5/9] finished wave 3 order part --- lib/order.rb | 90 +++++++++++++++++++++++++++++++++------------------- lib/test.rb | 5 ++- 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index 57bda897..906f9f6d 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,42 +1,68 @@ +require 'CSV' + 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 + 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 - - attr_reader :id, :products, :customer, :fulfillment_status + 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 total - sum = @products.sum {|product,cost| cost} - total = (sum * 1.075).round(2) - return total + 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 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 + 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 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 + + def self.all + orders = [] + CSV.open("data/orders.csv").each do |line| + id = line[0].to_i + customer_id = Customer.find(line[-2].to_i) + fulfillment_status = line[-1].to_sym + + producthash = {} + productslist = line[1].split(';') + productslist.each do |product| + item = product.split(':') + productkey = item[0] + productcost = item[1].to_f + producthash[productkey] = productcost + end + + + order = Order.new(id, producthash,customer_id,fulfillment_status) + orders << order end + return orders + end +end + -end \ No newline at end of file diff --git a/lib/test.rb b/lib/test.rb index d63fe2e3..42a8b784 100644 --- a/lib/test.rb +++ b/lib/test.rb @@ -1,5 +1,4 @@ -products = { "banana" => 1.99, "cracker" => 3.00 } - -products.delete("banana") +line = [1 , Lobster:17.18 ; Annatto seed:58.38 ; Camomile:83.21 , 25 , complete] +products = line[1] puts products \ No newline at end of file From 67a0ff857462da65836a412bf7911afb756e137c Mon Sep 17 00:00:00 2001 From: eaball35 Date: Thu, 22 Aug 2019 16:31:14 -0700 Subject: [PATCH 6/9] finished wave 2 - moved product formatingin order to own helper method --- lib/order.rb | 26 ++++++++++++++------------ lib/test.rb | 19 ++++++++++++++++--- test/order_test.rb | 2 +- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index 906f9f6d..9f35c9ff 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -39,26 +39,28 @@ def remove_product(product_name) 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_id = Customer.find(line[-2].to_i) fulfillment_status = line[-1].to_sym - - producthash = {} - productslist = line[1].split(';') - productslist.each do |product| - item = product.split(':') - productkey = item[0] - productcost = item[1].to_f - producthash[productkey] = productcost - end + products = Order.formatproducts(line[1]) - - order = Order.new(id, producthash,customer_id,fulfillment_status) + order = Order.new(id, products,customer_id,fulfillment_status) orders << order end return orders diff --git a/lib/test.rb b/lib/test.rb index 42a8b784..2b8532f8 100644 --- a/lib/test.rb +++ b/lib/test.rb @@ -1,4 +1,17 @@ -line = [1 , Lobster:17.18 ; Annatto seed:58.38 ; Camomile:83.21 , 25 , complete] +require_relative 'customer' -products = line[1] -puts products \ No newline at end of file +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/order_test.rb b/test/order_test.rb index f42d9fac..0f8928f1 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -149,7 +149,7 @@ # 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! From 8fb1dcb7eb594042d899057fd47782ccb78875bb Mon Sep 17 00:00:00 2001 From: eaball35 Date: Thu, 22 Aug 2019 18:10:29 -0700 Subject: [PATCH 7/9] Completed tests for order --- lib/customer.rb | 2 +- lib/order.rb | 22 ++++++++++++++++++++-- test/order_test.rb | 44 ++++++++++++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index 6ffb2830..08caa39e 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -31,7 +31,7 @@ def self.all end def self.find(id) - customers =self.all + customers = Customer.all customers.each { |customer| return customer if customer.id == id } return nil end diff --git a/lib/order.rb b/lib/order.rb index 9f35c9ff..f3f93c0a 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -56,15 +56,33 @@ def self.all orders = [] CSV.open("data/orders.csv").each do |line| id = line[0].to_i - customer_id = Customer.find(line[-2].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_id,fulfillment_status) + 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 + + def self.find_by_customer(customer_id) + customer_orders = [] + orders = Order.all + orders.each do |order| + if orders.customer == customer_id + customer_orders << order + end + end + return customer_orders + end + end diff --git a/test/order_test.rb b/test/order_test.rb index 0f8928f1..957b9c53 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -147,12 +147,18 @@ end - -# TODO: change 'xdescribe' to 'describe' to run these tests 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 @@ -167,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 @@ -176,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 From 40e5353ac6685a6cac731ba06ad080284c2c8496 Mon Sep 17 00:00:00 2001 From: eaball35 Date: Thu, 22 Aug 2019 18:22:57 -0700 Subject: [PATCH 8/9] getting off ferry - no work on wave 3 yet --- lib/customer.rb | 4 ++-- lib/order.rb | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index 08caa39e..e57bfb18 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -19,8 +19,8 @@ def self.all address = { street: line[2], city: line[3], - state: line [4], - zip: line [5] + state: line[4], + zip: line[5] } customer = Customer.new(id,email,address) diff --git a/lib/order.rb b/lib/order.rb index f3f93c0a..0dc147d9 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -84,5 +84,3 @@ def self.find_by_customer(customer_id) end end - - From 78519f1263068ae16f01e51523c321c3138f6625 Mon Sep 17 00:00:00 2001 From: eaball35 Date: Sun, 25 Aug 2019 20:38:42 -0700 Subject: [PATCH 9/9] submission --- lib/customer.rb | 1 - lib/order.rb | 13 +------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index e57bfb18..bea07585 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -1,5 +1,4 @@ require 'csv' -require 'awesome_print' class Customer def initialize(id,email,address) diff --git a/lib/order.rb b/lib/order.rb index 0dc147d9..a96c42db 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,4 +1,5 @@ require 'CSV' +require_relative 'customer' class Order def initialize(id, products,customer,fulfillment_status = :pending) @@ -71,16 +72,4 @@ def self.find(order_id) orders.each { |order| return order if order.id == order_id } return nil end - - def self.find_by_customer(customer_id) - customer_orders = [] - orders = Order.all - orders.each do |order| - if orders.customer == customer_id - customer_orders << order - end - end - return customer_orders - end - end