From bc61dc0661126e3ad94bdfd7e7995bc3f86f30de Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Wed, 14 Feb 2018 16:18:07 -0800 Subject: [PATCH 01/21] ran first test successfully with Dee holding my hand through it. --- lib/order.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/order.rb b/lib/order.rb index c567a484..a71a215c 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,7 +1,11 @@ module Grocery + + class Order attr_reader :id, :products + # id: Integer, represents unique identifier + # input_products: hash that represents products with keys of product name and values of price (Float) def initialize(id, products) @id = id @products = products @@ -9,10 +13,17 @@ def initialize(id, products) def total # TODO: implement total + subtotal = @products.values.sum + sum = subtotal + (subtotal * 0.075).round(2) + return sum end def add_product(product_name, product_price) # TODO: implement add_product + end end + + + end From 185900fca9bdf94b01149aa2b21035a044988817 Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Wed, 14 Feb 2018 16:20:25 -0800 Subject: [PATCH 02/21] added some personal comments --- specs/order_spec.rb | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/specs/order_spec.rb b/specs/order_spec.rb index 86077dcf..b922cdec 100644 --- a/specs/order_spec.rb +++ b/specs/order_spec.rb @@ -38,16 +38,24 @@ describe "#add_product" do it "Increases the number of products" do + # Arrange products = { "banana" => 1.99, "cracker" => 3.00 } before_count = products.count order = Grocery::Order.new(1337, products) + returned_products = order.return_products + puts "These are the products on the instance variable in the order #{returned_products}" + + # Act order.add_product("salad", 4.25) + + # Assert expected_count = before_count + 1 + order.products.count.must_equal expected_count end - it "Is added to the collection of products" do + xit "Is added to the collection of products" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Grocery::Order.new(1337, products) @@ -55,7 +63,7 @@ order.products.include?("sandwich").must_equal true end - it "Returns false if the product is already present" do + xit "Returns false if the product is already present" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Grocery::Order.new(1337, products) @@ -68,7 +76,7 @@ before_total.must_equal after_total end - it "Returns true if the product is new" do + xit "Returns true if the product is new" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Grocery::Order.new(1337, products) @@ -81,29 +89,29 @@ # TODO: change 'xdescribe' to 'describe' to run these tests xdescribe "Order Wave 2" do describe "Order.all" do - it "Returns an array of all orders" do + xit "Returns an array of all orders" do # TODO: Your test code here! end - it "Returns accurate information about the first order" do + xit "Returns accurate information about the first order" do # TODO: Your test code here! end - it "Returns accurate information about the last order" do + xit "Returns accurate information about the last order" do # TODO: Your test code here! end end describe "Order.find" do - it "Can find the first order from the CSV" do + xit "Can find the first order from the CSV" do # TODO: Your test code here! end - it "Can find the last order from the CSV" do + xit "Can find the last order from the CSV" do # TODO: Your test code here! end - it "Raises an error for an order that doesn't exist" do + xit "Raises an error for an order that doesn't exist" do # TODO: Your test code here! end end From 99176e69d3f48be7121901a49f4e64e2b6ec7c3c Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Wed, 14 Feb 2018 17:26:47 -0800 Subject: [PATCH 03/21] Worked on add product method - got new product into @product hash --- lib/order.rb | 15 +++++++++++++++ specs/order_spec.rb | 5 ++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index a71a215c..d595522f 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,3 +1,5 @@ +require 'awesome_print' + module Grocery @@ -20,8 +22,21 @@ def total def add_product(product_name, product_price) # TODO: implement add_product + # before_count = @products.count + # expected_count = before_count + 1 + + + + + @products[product_name] = product_price + puts "products is: #{@products}" + + + + return end + end diff --git a/specs/order_spec.rb b/specs/order_spec.rb index b922cdec..9b03cacc 100644 --- a/specs/order_spec.rb +++ b/specs/order_spec.rb @@ -3,6 +3,8 @@ require 'minitest/skip_dsl' require_relative '../lib/order' +Minitest::Reporters.use! + describe "Order Wave 1" do describe "#initialize" do it "Takes an ID and collection of products" do @@ -43,9 +45,6 @@ before_count = products.count order = Grocery::Order.new(1337, products) - returned_products = order.return_products - puts "These are the products on the instance variable in the order #{returned_products}" - # Act order.add_product("salad", 4.25) From b3fada81c18f1bd4f57446a9b4cf0734be807f5c Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Wed, 14 Feb 2018 18:45:49 -0800 Subject: [PATCH 04/21] passed test for Returns false if the product is already present but when I put puts statments in the if/else loop its in...both are printing out and I don't know why. (even though the test technically passes in specs test --- lib/order.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index d595522f..1895066d 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -25,20 +25,20 @@ def add_product(product_name, product_price) # before_count = @products.count # expected_count = before_count + 1 + # @products[product_name] = product_price + # puts "product list is now: #{@products}" + if @products.has_key?(product_name) + return false + else + @products[product_name] = product_price + puts "product list is now: #{@products}" + end + end #end of add_product method - @products[product_name] = product_price + end #end of class - puts "products is: #{@products}" - - return - end - - end - - - -end +end #end of module From 164a95ce578010403c96a906fd4b15d65002a01d Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Wed, 14 Feb 2018 21:54:17 -0800 Subject: [PATCH 05/21] got all tests to work, attempted to do the optional but didn't get there/ran out of time to figure it out. --- lib/order.rb | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index 1895066d..e1edf761 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,5 +1,3 @@ -require 'awesome_print' - module Grocery @@ -14,27 +12,16 @@ def initialize(id, products) end def total - # TODO: implement total subtotal = @products.values.sum sum = subtotal + (subtotal * 0.075).round(2) return sum end def add_product(product_name, product_price) - # TODO: implement add_product - # before_count = @products.count - # expected_count = before_count + 1 - - # @products[product_name] = product_price - # puts "product list is now: #{@products}" - - if @products.has_key?(product_name) - return false - else - @products[product_name] = product_price - puts "product list is now: #{@products}" - end + return false if @products.has_key?(product_name) + @products[product_name] = product_price + return true end #end of add_product method end #end of class From d50ee19204cee80ef982ba2ac9641d89a935e993 Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Wed, 14 Feb 2018 21:58:18 -0800 Subject: [PATCH 06/21] small change - started attemping the optional wave 1 remove product method test. did not finish. --- specs/order_spec.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/specs/order_spec.rb b/specs/order_spec.rb index 9b03cacc..589fd761 100644 --- a/specs/order_spec.rb +++ b/specs/order_spec.rb @@ -54,7 +54,7 @@ order.products.count.must_equal expected_count end - xit "Is added to the collection of products" do + it "Is added to the collection of products" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Grocery::Order.new(1337, products) @@ -62,7 +62,7 @@ order.products.include?("sandwich").must_equal true end - xit "Returns false if the product is already present" do + it "Returns false if the product is already present" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Grocery::Order.new(1337, products) @@ -75,13 +75,27 @@ before_total.must_equal after_total end - xit "Returns true if the product is new" do + it "Returns true if the product is new" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Grocery::Order.new(1337, products) result = order.add_product("salad", 4.25) result.must_equal true end + + # describe "#remove_product" do + # it "decreases the number of products" do + # products = { "banana" => 1.99, "cracker" => 3.00 } + # before_count = products.count + # order = Grocery::Order.new(1337, products) + # + # order.remove_product("banana") + # expected_count = before_count - 1 + # order.products.count.must_equal expected_count + # end + + end + end end From 4ad66a6b25c0636a2a1222ce5d86b85eb1cbcaec Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Thu, 15 Feb 2018 18:16:54 -0800 Subject: [PATCH 07/21] Started Wave 2, created class method called self.all and was finally able to get csv file to get imported. --- lib/order.rb | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/order.rb b/lib/order.rb index e1edf761..aaf4b0b4 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,16 +1,35 @@ +require 'awesome_print' +require 'csv' + +# CSV.read("orders.csv") +# CSV.open("orders.csv", mode='r') + +# manually choose the data from the first line of the CSV file and ensure you can create a new instance of your Order using that data + module Grocery class Order attr_reader :id, :products - # id: Integer, represents unique identifier # input_products: hash that represents products with keys of product name and values of price (Float) + + def initialize(id, products) @id = id @products = products end + def self.all + array_of_orders = [] + CSV.open("support/orders.csv", 'r').each do |line| + array_of_orders << line + end + return array_of_orders + end + + + def total subtotal = @products.values.sum sum = subtotal + (subtotal * 0.075).round(2) @@ -24,8 +43,27 @@ def add_product(product_name, product_price) return true end #end of add_product method + + # • Add a remove_product method to the Order class which will take in one parameter, a product name, and remove the product from the collection + # It should return true if the item was successfully remove and false if it was not + + def remove_product(product_name) + p @products + if @products.key?(product_name) + @products.delete(product_name) + return true + else + return false + end + end + + + + end #end of class end #end of module + +# ap Grocery::Order.all From 82d6765dc18bdf10a98ba523816e36de19a83650 Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Thu, 15 Feb 2018 18:19:35 -0800 Subject: [PATCH 08/21] figured out first test in wave 2 --- specs/order_spec.rb | 66 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/specs/order_spec.rb b/specs/order_spec.rb index 589fd761..4b28f271 100644 --- a/specs/order_spec.rb +++ b/specs/order_spec.rb @@ -82,32 +82,74 @@ result = order.add_product("salad", 4.25) result.must_equal true end + end + + #Basically the opposite of #add_product + describe "#remove_product" do + it "Decreases the number of products" do + products = { "banana" => 1.99, "cracker" => 3.00 } + before_count = products.count + order = Grocery::Order.new(1337, products) + + # based only on the first argument or name of product + order.remove_product("banana") + # remove instead of add + expected_count = before_count - 1 + order.products.count.must_equal expected_count + end + + it "Is removed to the collection of products" do + products = { "banana" => 1.99, "cracker" => 3.00 } + order = Grocery::Order.new(1337, products) + + order.remove_product("sandwich") + order.products.include?("sandwich").wont_equal true + end + + it "Returns true if the product is removed" do + products = { "banana" => 1.99, "cracker" => 3.00 } - # describe "#remove_product" do - # it "decreases the number of products" do - # products = { "banana" => 1.99, "cracker" => 3.00 } - # before_count = products.count - # order = Grocery::Order.new(1337, products) - # - # order.remove_product("banana") - # expected_count = before_count - 1 - # order.products.count.must_equal expected_count - # end + order = Grocery::Order.new(1337, products) + before_total = order.total + result = order.remove_product("banana") + after_total = order.total + + # Returns true if the before total != after total when item is removed + result.must_equal true + before_total.wont_equal after_total end + it "Returns false if the product is new" do + products = { "banana" => 1.99, "cracker" => 3.00 } + order = Grocery::Order.new(1337, products) + + result = order.remove_product("salad") + result.must_equal false end + + end end # TODO: change 'xdescribe' to 'describe' to run these tests xdescribe "Order Wave 2" do describe "Order.all" do - xit "Returns an array of all orders" do - # TODO: Your test code here! + it "Returns an array of all orders" do + # wait don't I need to put an "arrange" statement here?? + # + # Grocery::Order.all.count + # + # Grocery::Order.all.count.must_equal 100 + + all_orders = Grocery::Order.all + all_orders.must_be_kind_of Array + end xit "Returns accurate information about the first order" do # TODO: Your test code here! + + end xit "Returns accurate information about the last order" do From 07b114459a57244cbffcfc029b6d82dd8e15a103 Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Fri, 16 Feb 2018 15:21:13 -0800 Subject: [PATCH 09/21] I was totally wrong with thinking I was done with the all method. Just updated the first wave 1 test for it though. --- specs/order_spec.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/specs/order_spec.rb b/specs/order_spec.rb index 4b28f271..6409b3a9 100644 --- a/specs/order_spec.rb +++ b/specs/order_spec.rb @@ -137,29 +137,41 @@ it "Returns an array of all orders" do # wait don't I need to put an "arrange" statement here?? # - # Grocery::Order.all.count - # - # Grocery::Order.all.count.must_equal 100 + + + all_orders = Grocery::Order.all all_orders.must_be_kind_of Array + # or all_orders.class.must_equal Array + + all_orders.each do |order| + order.must_be_kind_of Order + end + + all_orders.count.must_equal 100 + end xit "Returns accurate information about the first order" do - # TODO: Your test code here! end xit "Returns accurate information about the last order" do - # TODO: Your test code here! + end end describe "Order.find" do xit "Can find the first order from the CSV" do - # TODO: Your test code here! + + first_order = Grocery::Order.find + + first_order.must_equal the first index + + end xit "Can find the last order from the CSV" do From bc33caef8bda98be8199ba6ed6e98229460de069 Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Fri, 16 Feb 2018 16:51:36 -0800 Subject: [PATCH 10/21] Slight fix on all method. BUT IT WORKS --- lib/order.rb | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index aaf4b0b4..da1ed421 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -20,12 +20,34 @@ def initialize(id, products) @products = products end + # "../support/orders.csv " #change path back to this when opening this file in ruby instead of through rake + + # def self.all + # array_of_orders = [] + # CSV.open("support/orders.csv", 'r').each do |line| + # array_of_orders << line + # end + # return array_of_orders + # end + def self.all array_of_orders = [] CSV.open("support/orders.csv", 'r').each do |line| - array_of_orders << line - end - return array_of_orders + # for each line I need to take the index[0] and make it a key of a hash and the value of that hash is then another hash (which is index[1] of the array) with key: product name , value: product price + # first step is turning two elements of an array into a hash. second step is taking the value of that hash and turning it into another hash + line[0].to_i + product_list = line[1].split(';') + product_hash = {} + product_list.each do |item| + product = item.split(':') + product_hash[product[0]] = product[1] + end + array_of_orders << Grocery::Order.new(line[0], product_hash) + end + array_of_orders + end + + def self.find end @@ -66,4 +88,4 @@ def remove_product(product_name) end #end of module -# ap Grocery::Order.all +#ap Grocery::Order.all[0][1] From 0d8bb7f61b30956e3dc91c68b24cf31f53cc88f7 Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Sat, 17 Feb 2018 13:00:59 -0800 Subject: [PATCH 11/21] trying to figure out find method. tests are giving me errors but not failures. --- lib/order.rb | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index da1ed421..2a6761cc 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,9 +1,6 @@ require 'awesome_print' require 'csv' -# CSV.read("orders.csv") -# CSV.open("orders.csv", mode='r') - # manually choose the data from the first line of the CSV file and ensure you can create a new instance of your Order using that data module Grocery @@ -32,7 +29,7 @@ def initialize(id, products) def self.all array_of_orders = [] - CSV.open("support/orders.csv", 'r').each do |line| + CSV.open("../support/orders.csv", 'r').each do |line| # for each line I need to take the index[0] and make it a key of a hash and the value of that hash is then another hash (which is index[1] of the array) with key: product name , value: product price # first step is turning two elements of an array into a hash. second step is taking the value of that hash and turning it into another hash line[0].to_i @@ -42,12 +39,27 @@ def self.all product = item.split(':') product_hash[product[0]] = product[1] end - array_of_orders << Grocery::Order.new(line[0], product_hash) - end - array_of_orders + array_of_orders << Grocery::Order.new(line[0], product_hash) + end + array_of_orders end - def self.find + def self.find(id) + # returns an instance of Order where the value of the id field in the CSV matches the passed parameter. + #to call this Class method: Grocery::Order.find(id) + + all_orders = Grocery::Order.all + # + # single_order = all_orders[id.to_i - 1] + # + # return single_order + + if all_orders.include?(id) + return all_orders[id - 1] + else + return nil + end + end @@ -70,7 +82,7 @@ def add_product(product_name, product_price) # It should return true if the item was successfully remove and false if it was not def remove_product(product_name) - p @products + #p @products if @products.key?(product_name) @products.delete(product_name) return true @@ -79,13 +91,16 @@ def remove_product(product_name) end end + end #end of class - end #end of class +end #end of module +all_orders = Grocery::Order.all + +# puts "\n#{all_orders}\n\n" -end #end of module -#ap Grocery::Order.all[0][1] +puts all_orders.find("3") From ef0b0cb9aa51a1bbc1f6c54b1ce4252604612a8f Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Sat, 17 Feb 2018 13:16:06 -0800 Subject: [PATCH 12/21] Still playing with find class method. not sure which direction to go --- lib/order.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/order.rb b/lib/order.rb index 2a6761cc..56fc5125 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,6 +1,8 @@ require 'awesome_print' require 'csv' + + # manually choose the data from the first line of the CSV file and ensure you can create a new instance of your Order using that data module Grocery @@ -54,7 +56,7 @@ def self.find(id) # # return single_order - if all_orders.include?(id) + if all_orders.include?("id") return all_orders[id - 1] else return nil From f4ef834d686b33f045b18ff3a3d31e61228f7a7f Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Sat, 17 Feb 2018 13:41:17 -0800 Subject: [PATCH 13/21] this find method might be right? I've been through like 4 different versions. --- lib/order.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index 56fc5125..b4456c0e 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -55,11 +55,16 @@ def self.find(id) # single_order = all_orders[id.to_i - 1] # # return single_order - - if all_orders.include?("id") - return all_orders[id - 1] - else - return nil + order = nil + + all_orders.each do |order_array| + if order_array[id - 1] == id + order = all_orders[id -1][1] + # return all_orders[id -1][1] + # else + # return nil + end + return order end end From 0bf24226ad423efd932083d447822566166d4142 Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Sat, 17 Feb 2018 13:49:35 -0800 Subject: [PATCH 14/21] Working on tests for find method. Still not sure I get how tests are supposed to work. --- specs/order_spec.rb | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/specs/order_spec.rb b/specs/order_spec.rb index 6409b3a9..b5a7c1c2 100644 --- a/specs/order_spec.rb +++ b/specs/order_spec.rb @@ -132,14 +132,10 @@ 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 # wait don't I need to put an "arrange" statement here?? - # - - - all_orders = Grocery::Order.all all_orders.must_be_kind_of Array @@ -150,35 +146,53 @@ end all_orders.count.must_equal 100 + end + it "Returns accurate information about the first order" do - end + all_orders = Grocery::Order.all + + all_orders[0][0].must_equal 1 + all_orders[0][1].must_be_kind_of Hash - xit "Returns accurate information about the first order" do - end xit "Returns accurate information about the last order" do + all_orders = Grocery::Order.all + + all_orders[0][99].must_equal 100 + all_orders[0][1].must_be_kind_of Hash + end end describe "Order.find" do - xit "Can find the first order from the CSV" do + it "Can find the first order from the CSV" do + all_orders = Grocery::Order.all - first_order = Grocery::Order.find + all_orders.find[1].must_equal true - first_order.must_equal the first index + first_order.must_equal all_orders[0][0] end - xit "Can find the last order from the CSV" do + it "Can find the last order from the CSV" do # TODO: Your test code here! + all_orders = Grocery::Order.all + + last_order = all_orders.find[100] + + last_order.must_equal true + + last_order.must_equal all_orders[0][-1] + + end - xit "Raises an error for an order that doesn't exist" do + xit "Returns nil for an order that doesn't exist" do # TODO: Your test code here! end end From 1e4bf2e2d9c6924088f90ef88ba8cb88a8bd963e Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Sat, 17 Feb 2018 18:57:54 -0800 Subject: [PATCH 15/21] I FINISHED WAVE 2 AND THE WAVE 2 TESTS FINALLLLLYYYYYYYYYYY. --- lib/order.rb | 33 ++++++++++++--------------------- specs/order_spec.rb | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index b4456c0e..7ced13d5 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -31,17 +31,17 @@ def initialize(id, products) def self.all array_of_orders = [] - CSV.open("../support/orders.csv", 'r').each do |line| + CSV.open("support/orders.csv", 'r').each do |line| # for each line I need to take the index[0] and make it a key of a hash and the value of that hash is then another hash (which is index[1] of the array) with key: product name , value: product price # first step is turning two elements of an array into a hash. second step is taking the value of that hash and turning it into another hash - line[0].to_i + product_list = line[1].split(';') product_hash = {} product_list.each do |item| product = item.split(':') product_hash[product[0]] = product[1] end - array_of_orders << Grocery::Order.new(line[0], product_hash) + array_of_orders << Grocery::Order.new(line[0].to_i, product_hash) end array_of_orders end @@ -51,22 +51,16 @@ def self.find(id) #to call this Class method: Grocery::Order.find(id) all_orders = Grocery::Order.all - # - # single_order = all_orders[id.to_i - 1] - # - # return single_order + order = nil - all_orders.each do |order_array| - if order_array[id - 1] == id - order = all_orders[id -1][1] - # return all_orders[id -1][1] - # else - # return nil + all_orders.each do |single_order| + + if single_order.id == id + order = single_order end - return order end - + return order end @@ -104,10 +98,7 @@ def remove_product(product_name) end #end of module -all_orders = Grocery::Order.all - -# puts "\n#{all_orders}\n\n" - - +#all_orders = Grocery::Order.all -puts all_orders.find("3") +# # puts "\n#{all_orders}\n\n" +# puts all_orders.find(3) diff --git a/specs/order_spec.rb b/specs/order_spec.rb index b5a7c1c2..b515e0ba 100644 --- a/specs/order_spec.rb +++ b/specs/order_spec.rb @@ -1,6 +1,7 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' +require 'pry' require_relative '../lib/order' Minitest::Reporters.use! @@ -134,15 +135,18 @@ # 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 # wait don't I need to put an "arrange" statement here?? all_orders = Grocery::Order.all - all_orders.must_be_kind_of Array - # or all_orders.class.must_equal Array + # all_orders.must_be_kind_of Array + all_orders.class.must_equal Array all_orders.each do |order| - order.must_be_kind_of Order + order.must_be_instance_of Grocery::Order end all_orders.count.must_equal 100 @@ -151,49 +155,50 @@ it "Returns accurate information about the first order" do all_orders = Grocery::Order.all + # binding.pry + - all_orders[0][0].must_equal 1 - all_orders[0][1].must_be_kind_of Hash + all_orders[0].id.must_equal 1 + + all_orders[0].products.must_be_kind_of Hash end - xit "Returns accurate information about the last order" do + it "Returns accurate information about the last order" do all_orders = Grocery::Order.all - all_orders[0][99].must_equal 100 - all_orders[0][1].must_be_kind_of Hash + all_orders[-1].id.must_equal 100 + all_orders[-1].products.must_be_kind_of Hash end end describe "Order.find" do - it "Can find the first order from the CSV" do - all_orders = Grocery::Order.all - all_orders.find[1].must_equal true + before do + @order_class = Grocery::Order + end + - first_order.must_equal all_orders[0][0] + it "Can find the first order from the CSV" do + @order_class.find(1).must_be_instance_of Grocery::Order end it "Can find the last order from the CSV" do - # TODO: Your test code here! - all_orders = Grocery::Order.all - last_order = all_orders.find[100] + @order_class.find(100).must_be_instance_of Grocery::Order - last_order.must_equal true + end - last_order.must_equal all_orders[0][-1] + it "Returns nil for an order that doesn't exist" do + @order_class.find(101).must_be_nil - end - xit "Returns nil for an order that doesn't exist" do - # TODO: Your test code here! end end end From 187feeb0f0ee5fb85f96b5787f8b79339b24fcc6 Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Sun, 18 Feb 2018 16:16:38 -0800 Subject: [PATCH 16/21] Started OnlineOrder class. --- lib/online_order.rb | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lib/online_order.rb diff --git a/lib/online_order.rb b/lib/online_order.rb new file mode 100644 index 00000000..f2b51279 --- /dev/null +++ b/lib/online_order.rb @@ -0,0 +1,64 @@ +# online order + #example + +module Grocery + +class OnlineOrder < Order + + attr_reader :id, :products :customer_id :status + + def initialize(id, products) + @id = id + @products = products + @customer_id = customer_id + @fulfillment_status = status.to_sym + + # tip from Katie: 16 and 22 are the customer id's that exsit but do not have orders + + + end + + def self.all + array_of_orders = [] + CSV.open("support/online_orders.csv", 'r').each do |line| + + product_list = line[1].split(';') + product_hash = {} + product_list.each do |item| + product = item.split(':') + product_hash[product[0]] = product[1] + end + array_of_orders << Grocery::OnlineOrder.new(line[0].to_i, product_hash, customer_id, fulfillment_status) + end + array_of_orders + end + + def self.find(id) + + end + + def self.find_by_customer(customer_id) + + end + + def total + shipping_fee = 10.0 + return super + shipping_fee + end + + def add_product(product_name, product_price) + return false if @products.has_key?(product_name) + @products[product_name] = product_price + return true + end + + +# The add_product method should be updated to permit a new product to be added ONLY if the status is either pending or paid (no other statuses permitted) +# Otherwise, it should raise an ArgumentError (Google this!) + + + +end # end of OnlineOrder Class + + +end # end of Module Class From 00bedba67a77403a3e65d7df08b1e55941e41863 Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Sun, 18 Feb 2018 16:42:24 -0800 Subject: [PATCH 17/21] I think I finished the find_by_customer method --- lib/online_order.rb | 23 ++++++++++++++++++++--- lib/order.rb | 16 +--------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/online_order.rb b/lib/online_order.rb index f2b51279..a6ebee88 100644 --- a/lib/online_order.rb +++ b/lib/online_order.rb @@ -33,12 +33,29 @@ def self.all array_of_orders end - def self.find(id) - - end + # (shouldn't need to rewrite out a new find method since it inherits from Order class and should work the same.) + # def self.find(id) + # all_orders = Grocery::OnlineOrder.all + # order = nil + # + # all_orders.each do |single_order| + # + # if single_order.id == id + # order = single_order + # end + # end + # return order + # end def self.find_by_customer(customer_id) + all_orders = Grocery::OnlineOrder.all + all_orders.each do |single_order| + order_list = [] + if single_order[2] == customer_id + order_list << single_order + end + return order_list end def total diff --git a/lib/order.rb b/lib/order.rb index 7ced13d5..17751392 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -13,7 +13,6 @@ class Order # id: Integer, represents unique identifier # input_products: hash that represents products with keys of product name and values of price (Float) - def initialize(id, products) @id = id @products = products @@ -21,13 +20,6 @@ def initialize(id, products) # "../support/orders.csv " #change path back to this when opening this file in ruby instead of through rake - # def self.all - # array_of_orders = [] - # CSV.open("support/orders.csv", 'r').each do |line| - # array_of_orders << line - # end - # return array_of_orders - # end def self.all array_of_orders = [] @@ -92,13 +84,7 @@ def remove_product(product_name) end end - end #end of class - + end #end of Order class end #end of module - -#all_orders = Grocery::Order.all - -# # puts "\n#{all_orders}\n\n" -# puts all_orders.find(3) From 898adcdf8a39d332de187cf3b7cb5e930d40522b Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Mon, 19 Feb 2018 16:53:39 -0800 Subject: [PATCH 18/21] I'm having a really hard time with the tests. Besides the ones I also did in order specs that are easier to adjust for online-orders. --- specs/online_order_spec.rb | 63 +++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/specs/online_order_spec.rb b/specs/online_order_spec.rb index 41683558..241c3fac 100644 --- a/specs/online_order_spec.rb +++ b/specs/online_order_spec.rb @@ -3,7 +3,7 @@ require 'minitest/skip_dsl' # TODO: uncomment the next line once you start wave 3 -# require_relative '../lib/online_order' +require_relative '../lib/online_order' # You may also need to require other classes here # Because an OnlineOrder is a kind of Order, and we've @@ -14,23 +14,28 @@ xdescribe "OnlineOrder" do describe "#initialize" do it "Is a kind of Order" do - # Check that an OnlineOrder is in fact a kind of Order - # Instatiate your OnlineOrder here - # online_order = - # online_order.must_be_kind_of Grocery::Order + all_orders.class.must_equal Array + + all_orders.each do |order| + order.must_be_instance_of Grocery::Order + end + + all_orders.count.must_equal 100 + end end - it "Can access Customer object" do - # TODO: Your test code here! + xit "Can access Customer object" do + # TODO: I did not create a Customer class. end it "Can access the online order status" do - # TODO: Your test code here! + @online_order.status.must_be_instance_of Symbol + end end - describe "#total" do + xdescribe "#total" do it "Adds a shipping fee" do # TODO: Your test code here! end @@ -51,39 +56,55 @@ end describe "OnlineOrder.all" do - it "Returns an array of all online orders" do - # TODO: Your test code here! + xit "Returns an array of all online orders" do + all_orders = Grocery::OnlineOrder.all + # all_orders.must_be_kind_of Array + all_orders.class.must_equal Array + + all_orders.each do |order| + order.must_be_instance_of Grocery::OnlineOrder + end + + all_orders.count.must_equal 100 + end end - it "Returns accurate information about the first online order" do - # TODO: Your test code here! + xit "Returns accurate information about the first online order" do + all_orders = Grocery::OnlineOrder.all + + all_orders[0].id.must_equal 1 + + all_orders[0].products.must_be_kind_of Hash end - it "Returns accurate information about the last online order" do - # TODO: Your test code here! + xit "Returns accurate information about the last online order" do + all_orders = Grocery::OnlineOrder.all + + all_orders[-1].id.must_equal 100 + all_orders[-1].products.must_be_kind_of Hash end end - describe "OnlineOrder.find" do - it "Will find an online order from the CSV" do + xdescribe "OnlineOrder.find" do + xit "Will find an online order from the CSV" do # TODO: Your test code here! end - it "Raises an error for an online order that doesn't exist" do + xit "Raises an error for an online order that doesn't exist" do # TODO: Your test code here! end end describe "OnlineOrder.find_by_customer" do - it "Returns an array of online orders for a specific customer ID" do + xit "Returns an array of online orders for a specific customer ID" do # TODO: Your test code here! end - it "Raises an error if the customer does not exist" do + xit "Raises an error if the customer does not exist" do # TODO: Your test code here! end - it "Returns an empty array if the customer has no orders" do + xit "Returns an empty array if the customer has no orders" do # TODO: Your test code here! end end From 4d2ec9d5662f0c9200264f4f7a4837a733e6edcd Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Mon, 19 Feb 2018 16:55:03 -0800 Subject: [PATCH 19/21] small change --- lib/online_order.rb | 111 +++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 59 deletions(-) diff --git a/lib/online_order.rb b/lib/online_order.rb index a6ebee88..c8952f05 100644 --- a/lib/online_order.rb +++ b/lib/online_order.rb @@ -1,81 +1,74 @@ # online order - #example +require 'awesome_print' +require 'csv' +require_relative "order.rb" -module Grocery +#example -class OnlineOrder < Order +module Grocery - attr_reader :id, :products :customer_id :status + class OnlineOrder < Order - def initialize(id, products) - @id = id - @products = products - @customer_id = customer_id - @fulfillment_status = status.to_sym + attr_reader :id, :products :customer_id :status - # tip from Katie: 16 and 22 are the customer id's that exsit but do not have orders + def initialize(id, products, customer_id, fulfillment_status) + @id = id + @products = products + @customer_id = customer_id + @fulfillment_status = status.to_sym + # tip from Katie: 16 and 22 are the customer id's that exsit but do not have orders - end - def self.all - array_of_orders = [] - CSV.open("support/online_orders.csv", 'r').each do |line| + end - product_list = line[1].split(';') - product_hash = {} - product_list.each do |item| - product = item.split(':') - product_hash[product[0]] = product[1] + def self.all + array_of_orders = [] + CSV.open("support/online_orders.csv", 'r').each do |line| + + product_list = line[1].split(';') + product_hash = {} + product_list.each do |item| + product = item.split(':') + product_hash[product[0]] = product[1] + end + array_of_orders << Grocery::OnlineOrder.new(line[0].to_i, product_hash, customer_id, fulfillment_status) end - array_of_orders << Grocery::OnlineOrder.new(line[0].to_i, product_hash, customer_id, fulfillment_status) + array_of_orders end - array_of_orders - end - - # (shouldn't need to rewrite out a new find method since it inherits from Order class and should work the same.) - # def self.find(id) - # all_orders = Grocery::OnlineOrder.all - # order = nil - # - # all_orders.each do |single_order| - # - # if single_order.id == id - # order = single_order - # end - # end - # return order - # end - - def self.find_by_customer(customer_id) - all_orders = Grocery::OnlineOrder.all - - all_orders.each do |single_order| - order_list = [] - if single_order[2] == customer_id - order_list << single_order - end - return order_list - end - def total - shipping_fee = 10.0 - return super + shipping_fee - end + # (shouldn't need to rewrite out a new self.find method since it inherits from Order class and should work the same.) + + def self.find_by_customer(customer_id) + all_orders = Grocery::OnlineOrder.all - def add_product(product_name, product_price) - return false if @products.has_key?(product_name) - @products[product_name] = product_price - return true - end + all_orders.each do |single_order| + order_list = [] + if single_order[2] == customer_id + order_list << single_order + end + return order_list + end + end + def total + shipping_fee = 10.0 + return super + shipping_fee + end -# The add_product method should be updated to permit a new product to be added ONLY if the status is either pending or paid (no other statuses permitted) -# Otherwise, it should raise an ArgumentError (Google this!) + def add_product(product_name, product_price) + if @status == :pending || @status == :paid + return false if @products.has_key?(product_name) + @products[product_name] = product_price + return true + else + raise ArgumentError.new("Status is neither pending nor paid") + end + end + end # end of OnlineOrder Class -end # end of OnlineOrder Class end # end of Module Class From e49bddccdd5f41441ad3de0d7cb4109b0e6b571c Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Mon, 19 Feb 2018 23:38:04 -0800 Subject: [PATCH 20/21] fixed my .all method, line 35 fix for parameter definitions. --- lib/online_order.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/online_order.rb b/lib/online_order.rb index c8952f05..6bd7aad7 100644 --- a/lib/online_order.rb +++ b/lib/online_order.rb @@ -2,6 +2,7 @@ require 'awesome_print' require 'csv' require_relative "order.rb" +require 'pry' #example @@ -9,23 +10,22 @@ module Grocery class OnlineOrder < Order - attr_reader :id, :products :customer_id :status + attr_reader :id, :products, :customer_id, :fulfillment_status def initialize(id, products, customer_id, fulfillment_status) @id = id @products = products @customer_id = customer_id - @fulfillment_status = status.to_sym + @fulfillment_status = fulfillment_status.to_sym # tip from Katie: 16 and 22 are the customer id's that exsit but do not have orders - - end def self.all array_of_orders = [] CSV.open("support/online_orders.csv", 'r').each do |line| - + customer_id = line[2] + fulfillment_status = line[3].to_sym product_list = line[1].split(';') product_hash = {} product_list.each do |item| @@ -40,9 +40,8 @@ def self.all # (shouldn't need to rewrite out a new self.find method since it inherits from Order class and should work the same.) def self.find_by_customer(customer_id) - all_orders = Grocery::OnlineOrder.all - all_orders.each do |single_order| + OnlineOrder.all.each do |single_order| order_list = [] if single_order[2] == customer_id order_list << single_order From 07171c173a215584313479d037362e3fb8641d8a Mon Sep 17 00:00:00 2001 From: Angela Poland Date: Mon, 19 Feb 2018 23:39:55 -0800 Subject: [PATCH 21/21] small changes with first test in online specs --- specs/online_order_spec.rb | 127 +++++++++++++++++++------------------ specs/order_spec.rb | 4 +- 2 files changed, 67 insertions(+), 64 deletions(-) diff --git a/specs/online_order_spec.rb b/specs/online_order_spec.rb index 241c3fac..7aa57fec 100644 --- a/specs/online_order_spec.rb +++ b/specs/online_order_spec.rb @@ -11,101 +11,106 @@ # we effectively get all that testing for free! Here we'll # only test things that are different. -xdescribe "OnlineOrder" do +describe "OnlineOrder" do describe "#initialize" do it "Is a kind of Order" do + all_orders = Grocery::OnlineOrder.all all_orders.class.must_equal Array all_orders.each do |order| - order.must_be_instance_of Grocery::Order + order.must_be_instance_of Grocery::OnlineOrder end all_orders.count.must_equal 100 end - end + end - xit "Can access Customer object" do - # TODO: I did not create a Customer class. - end + xit "Can access Customer object" do + # TODO: I did not create a Customer class. + end - it "Can access the online order status" do - @online_order.status.must_be_instance_of Symbol + it "Can access the online order status" do + + all_orders = Grocery::OnlineOrder.all + + all_orders[0][3].must_be_kind_of Symbol - end end +end - xdescribe "#total" do - it "Adds a shipping fee" do - # TODO: Your test code here! - end +xdescribe "#total" do + xit "Adds a shipping fee" do + # TODO: Your test code here! + end - it "Doesn't add a shipping fee if there are no products" do - # TODO: Your test code here! - end + xit "Doesn't add a shipping fee if there are no products" do + # TODO: Your test code here! end +end - describe "#add_product" do - it "Does not permit action for processing, shipped or completed statuses" do - # TODO: Your test code here! - end +xdescribe "#add_product" do + xit "Does not permit action for processing, shipped or completed statuses" do + # TODO: Your test code here! + end - it "Permits action for pending and paid satuses" do - # TODO: Your test code here! + xit "Permits action for pending and paid satuses" do + # TODO: Your test code here! + end +end + +xdescribe "OnlineOrder.all" do + it "Returns an array of all online orders" do + all_orders = Grocery::OnlineOrder.all + # all_orders.must_be_kind_of Array + all_orders.class.must_equal Array + + all_orders.each do |order| + order.must_be_instance_of Grocery::OnlineOrder end + + all_orders.count.must_equal 100 end - describe "OnlineOrder.all" do - xit "Returns an array of all online orders" do - all_orders = Grocery::OnlineOrder.all - # all_orders.must_be_kind_of Array - all_orders.class.must_equal Array - all_orders.each do |order| - order.must_be_instance_of Grocery::OnlineOrder - end +it "Returns accurate information about the first online order" do + all_orders = Grocery::OnlineOrder.all - all_orders.count.must_equal 100 - end - end + all_orders[0].id.must_equal 1 - xit "Returns accurate information about the first online order" do - all_orders = Grocery::OnlineOrder.all + all_orders[0].products.must_be_kind_of Hash +end - all_orders[0].id.must_equal 1 +it "Returns accurate information about the last online order" do + all_orders = Grocery::OnlineOrder.all - all_orders[0].products.must_be_kind_of Hash - end + all_orders[-1].id.must_equal 100 + all_orders[-1].products.must_be_kind_of Hash - xit "Returns accurate information about the last online order" do - all_orders = Grocery::OnlineOrder.all +end - all_orders[-1].id.must_equal 100 - all_orders[-1].products.must_be_kind_of Hash - end - end +end - xdescribe "OnlineOrder.find" do - xit "Will find an online order from the CSV" do - # TODO: Your test code here! - end +xdescribe "OnlineOrder.find" do + xit "Will find an online order from the CSV" do + # TODO: Your test code here! + end - xit "Raises an error for an online order that doesn't exist" do - # TODO: Your test code here! - end + xit "Raises an error for an online order that doesn't exist" do + # TODO: Your test code here! end +end - describe "OnlineOrder.find_by_customer" do - xit "Returns an array of online orders for a specific customer ID" do - # TODO: Your test code here! - end +xdescribe "OnlineOrder.find_by_customer" do + xit "Returns an array of online orders for a specific customer ID" do + Grocery::OnlineOrder.find(1).must_be_instance_of Grocery::OnlineOrder + end - xit "Raises an error if the customer does not exist" do - # TODO: Your test code here! - end + xit "Raises an error if the customer does not exist" do + # TODO: Your test code here! + end - xit "Returns an empty array if the customer has no orders" do - # TODO: Your test code here! - end + xit "Returns an empty array if the customer has no orders" do + Grocery::OnlineOrder.find_by_customer(22).must_be_empty end end diff --git a/specs/order_spec.rb b/specs/order_spec.rb index b515e0ba..73b9ae3e 100644 --- a/specs/order_spec.rb +++ b/specs/order_spec.rb @@ -136,8 +136,6 @@ describe "Order Wave 2" do describe "Order.all" do - - it "Returns an array of all orders" do # wait don't I need to put an "arrange" statement here?? @@ -196,7 +194,7 @@ it "Returns nil for an order that doesn't exist" do - @order_class.find(101).must_be_nil + @order_class.find(500).must_be_nil end