From d44907035d9930d4ed63466abc0429a8cef35627 Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Tue, 20 Oct 2015 09:15:54 -0700 Subject: [PATCH 01/14] Add self.all, self.find to all Classes and vendors method to Market class --- lib/far_mar/market.rb | 36 ++++++++++++++++++++++++++++++++++++ lib/far_mar/product.rb | 15 +++++++++++++++ lib/far_mar/sale.rb | 15 +++++++++++++++ lib/far_mar/vendor.rb | 23 +++++++++++++++++++++++ spec/far_mar/market_spec.rb | 24 +++++++++++++++++++++++- spec/far_mar/product_spec.rb | 16 ++++++++++++++++ spec/far_mar/sale_spec.rb | 15 +++++++++++++++ spec/far_mar/vendor_spec.rb | 16 +++++++++++++++- 8 files changed, 158 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 5be343e9..d6807136 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,4 +1,40 @@ module FarMar class Market + + attr_reader :market_id, :name, :address, :city, :county, :state, :zip + + def initialize(market_id, name, address, city, county, state, zip) + @market_id = market_id + @name = name + @address = address + @city = city + @county = county + @state = state + @zip = zip + end + + def self.all + markets_csv = CSV.read("./support/markets.csv") + market_instances = [] + markets_csv.each do |row| + market_instances.push(Market.new(row[0].to_i, row[1], row[2], row[3], row[4], row[5], row[6])) + end + return market_instances + end + def self.find(id) + self.all.find do |market| + market.market_id == id + end + end + def vendor + market_vendor_array = [] + vendor_array = FarMar::Vendor.all + vendor_array.each do |vendor| + if vendor.market_id == @market_id + market_vendor_array.push(vendor) + end + end + return market_vendor_array + end end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 67d6716d..2c5f969b 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,4 +1,19 @@ module FarMar class Product + + def self.all + products_csv = CSV.read("./support/products.csv") + product_instances = [] + products_csv.each do |row| + product_instances.push(Product.new(row[0].to_i, row[1], row[2].to_i)) + end + return product_instances + end + def self.find(id) + self.all.find do |product| + product.product_id == id + end + end + end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 10e3780e..372fec44 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,4 +1,19 @@ module FarMar class Sale + + def self.all + saless_csv = CSV.read("./support/sales.csv") + sale_instances = [] + sales_csv.each do |row| + sale_instances.push(Sale.new(row[0].to_i, row[1].to_i, row[2], row[3].to_i, row[4].to_i)) + end + return sale_instances + end + def self.find(id) + self.all.find do |sale| + sale.sale_id == id + end + end + end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 43c213b6..2aef28ff 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,4 +1,27 @@ module FarMar class Vendor + + attr_reader :vendor_id, :name, :employees, :market_id + + def initialize(vendor_id, name, employees, market_id) + @vendor_id = vendor_id + @name = name + @employees = employees + @market_id = market_id + end + + def self.all + vendor_csv = CSV.read("./support/vendors.csv") + vendor_instances = [] + vendor_csv.each do |row| + vendor_instances.push(Vendor.new(row[0].to_i, row[1], row[2], row[3].to_i)) + end + return vendor_instances + end + def self.find(id) + self.all.find do |vendor| + vendor.vendor_id == id + end + end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 092c46ca..cbfb3504 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -2,11 +2,33 @@ describe FarMar::Market do before :each do - @market = FarMar::Market.new + @market = FarMar::Market.new(1, "Meighan", "1234 N St", "Seattle", "WA", "King", "98103") end describe "#initialize" do it "creates a new instance" do expect(@market).to be_an_instance_of FarMar::Market end end + + describe "self.all" do + it "creates an array of markets" do + expect(FarMar::Market.all).to be_an Array + end + end + + describe "self.find" do + it "returns an instance of Market where the value of the id field in the CSV matches the passed parameter" do + result = FarMar::Market.find(3) + expect(result).to be_an Object + expect(result.market_id).to eq 3 + end + end + + describe "vendors" do + it "return a collection of vendor instances that are associated with market_id" do + market_vendor_array = @market.vendor + expect(market_vendor_array).to be_an Array + expect(market_vendor_array.length).to eq 6 + end + end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index cf4bbf1e..bdb0310d 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -9,4 +9,20 @@ expect(@product).to be_an_instance_of FarMar::Product end end + + describe "self.all" do + it "creates an array of products" do + expect(FarMar::Product.all).to be_an Array + end + end + + describe "self.find" do + it "returns an instance of Product where the value of the id field in the CSV matches the passed parameter" do + result = FarMar::Product.find(3) + expect(result).to be_an Object + expect(result.product_id).to eq 3 + end + end + + end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 38d647a2..0e4b95f2 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -9,4 +9,19 @@ expect(@sale).to be_an_instance_of FarMar::Sale end end + + describe "self.all" do + it "creates an array of sales" do + expect(FarMar::Sale.all).to be_an Array + end + end + + describe "self.find" do + it "returns an instance of Sale where the value of the id field in the CSV matches the passed parameter" do + result = FarMar::Sale.find(3) + expect(result).to be_an Object + expect(result.sale_id).to eq 3 + end + end + end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index a1987411..310fabb2 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,11 +2,25 @@ describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new + @vendor = FarMar::Vendor.new(1, "Joe", "10", "4") end describe "#initialize" do it "creates a new instance" do expect(@vendor).to be_an_instance_of FarMar::Vendor end end + + describe "self.all" do + it "creates an array of vendors" do + expect(FarMar::Vendor.all).to be_an Array + end + end + + describe "self.find" do + it "returns an instance of Vendor where the value of the id field in the CSV matches the passed parameter" do + result = FarMar::Vendor.find(1) + expect(result).to be_an Object + expect(result.vendor_id).to eq 1 + end + end end From ec0d81fcff8a4b91f4ddcf86b6091b6e05787648 Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Tue, 20 Oct 2015 16:04:48 -0700 Subject: [PATCH 02/14] Add market method to Vendor Class and related tests, plus edits to other files --- lib/far_mar/market.rb | 2 +- lib/far_mar/product.rb | 9 +++++++++ lib/far_mar/sale.rb | 13 ++++++++++++- lib/far_mar/vendor.rb | 9 +++++++++ spec/far_mar/market_spec.rb | 2 +- spec/far_mar/product_spec.rb | 2 +- spec/far_mar/sale_spec.rb | 2 +- spec/far_mar/vendor_spec.rb | 10 +++++++++- 8 files changed, 43 insertions(+), 6 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index d6807136..e4b7b51b 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -26,7 +26,7 @@ def self.find(id) market.market_id == id end end - def vendor + def vendors market_vendor_array = [] vendor_array = FarMar::Vendor.all vendor_array.each do |vendor| diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 2c5f969b..a31196f2 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,6 +1,15 @@ module FarMar class Product + attr_reader :product_id, :name, :vendor_id + + def initialize(product_id, name, vendor_id) + @product_id = product_id + @name = name + @vendor_id = vendor_id + end + + def self.all products_csv = CSV.read("./support/products.csv") product_instances = [] diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 372fec44..79095b3a 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,8 +1,19 @@ module FarMar class Sale + attr_reader :sale_id, :amount, :purchase_time, :vendor_id, :product_id + + def initialize(sale_id, amount, purchae_time, vendor_id, product_id) + @sale_id = sale_id + @amount = amount + @purchae_time = purchae_time + @vendor_id = vendor_id + @product_id = product_id + end + + def self.all - saless_csv = CSV.read("./support/sales.csv") + sales_csv = CSV.read("./support/sales.csv") sale_instances = [] sales_csv.each do |row| sale_instances.push(Sale.new(row[0].to_i, row[1].to_i, row[2], row[3].to_i, row[4].to_i)) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 2aef28ff..ae3b966c 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -23,5 +23,14 @@ def self.find(id) vendor.vendor_id == id end end + + def market + vendor_market_array = FarMar::Market.all + vendor_market_array.each do |row| + if row.market_id == @market_id + return row + end + end + end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index cbfb3504..ff0affc8 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -26,7 +26,7 @@ describe "vendors" do it "return a collection of vendor instances that are associated with market_id" do - market_vendor_array = @market.vendor + market_vendor_array = @market.vendors expect(market_vendor_array).to be_an Array expect(market_vendor_array.length).to eq 6 end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index bdb0310d..00ac9fac 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Product do before :each do - @product = FarMar::Product.new + @product = FarMar::Product.new(5, "apple", 2) end describe "#initialize" do it "creates a new instance" do diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 0e4b95f2..9dd06ad4 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Sale do before :each do - @sale = FarMar::Sale.new + @sale = FarMar::Sale.new(10, "150", "12:00pm", 2, 4) end describe "#initialize" do it "creates a new instance" do diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 310fabb2..990b4561 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new(1, "Joe", "10", "4") + @vendor = FarMar::Vendor.new(1, "Joe", "10", 4) end describe "#initialize" do it "creates a new instance" do @@ -23,4 +23,12 @@ expect(result.vendor_id).to eq 1 end end + + describe "market" do + it "returns the market instance that is associated with the vendor" do + vendor_market = @vendor.market + expect(vendor_market).to be_an Object + expect(vendor_market.market_id).to eq 4 + end + end end From 4baa975c4fa0dcdb9f25bffdc9367e37b850c4c0 Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Tue, 20 Oct 2015 16:56:58 -0700 Subject: [PATCH 03/14] Add products method to Vendor class --- lib/far_mar/vendor.rb | 11 +++++++++++ spec/far_mar/vendor_spec.rb | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index ae3b966c..3c631a02 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -32,5 +32,16 @@ def market end end end + + def products + vendor_products_array = [] + product_array = FarMar::Product.all + product_array.each do |product| + if product.vendor_id == @vendor_id + vendor_products_array.push(product) + end + end + return vendor_products_array + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 990b4561..6fb36328 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new(1, "Joe", "10", 4) + @vendor = FarMar::Vendor.new(2, "Joe", "10", 4) end describe "#initialize" do it "creates a new instance" do @@ -31,4 +31,12 @@ expect(vendor_market.market_id).to eq 4 end end + + describe "products" do + it "return a collection of vendor instances that are associated with market_id" do + vendor_product_array = @vendor.products + expect(vendor_product_array).to be_an Array + expect(vendor_product_array.length).to eq 2 + end + end end From 1365b46f12386a867579517b7ca50871f88ff71b Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Wed, 21 Oct 2015 14:37:57 -0700 Subject: [PATCH 04/14] Add all methods to Vendor class and created tests for them --- lib/far_mar/sale.rb | 4 ++-- lib/far_mar/vendor.rb | 24 ++++++++++++++++++++++++ spec/far_mar/vendor_spec.rb | 32 ++++++++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 79095b3a..7abdb148 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -3,10 +3,10 @@ class Sale attr_reader :sale_id, :amount, :purchase_time, :vendor_id, :product_id - def initialize(sale_id, amount, purchae_time, vendor_id, product_id) + def initialize(sale_id, amount, purchase_time, vendor_id, product_id) @sale_id = sale_id @amount = amount - @purchae_time = purchae_time + @purchase_time = purchase_time @vendor_id = vendor_id @product_id = product_id end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 3c631a02..106be9bf 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -43,5 +43,29 @@ def products end return vendor_products_array end + + def sales + vendor_sales_array = [] + sale_array = FarMar::Sale.all + sale_array.each do |sale| + if sale.vendor_id == @vendor_id + vendor_sales_array.push(sale) + end + end + return vendor_sales_array + end + + def revenue + total_revenue = 0 + sales.each do |sale| + total_revenue += sale.amount + end + return total_revenue + end + + def self.by_market(market_id) + market = FarMar::Market.find(market_id) + return market.vendors + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 6fb36328..6f88a23d 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new(2, "Joe", "10", 4) + @vendor = FarMar::Vendor.new(1, "Joe", "10", 4) end describe "#initialize" do it "creates a new instance" do @@ -17,7 +17,7 @@ end describe "self.find" do - it "returns an instance of Vendor where the value of the id field in the CSV matches the passed parameter" do + it "returns vendor instance where the value of the id the passed parameter" do result = FarMar::Vendor.find(1) expect(result).to be_an Object expect(result.vendor_id).to eq 1 @@ -33,10 +33,34 @@ end describe "products" do - it "return a collection of vendor instances that are associated with market_id" do + it "returns a collection of product instances that are associated with vendor_id" do vendor_product_array = @vendor.products expect(vendor_product_array).to be_an Array - expect(vendor_product_array.length).to eq 2 + expect(vendor_product_array.length).to eq 1 + end + end + + describe "sales" do + it "return a collection of sale instances that are associated with vendor_id" do + vendor_sale_array = @vendor.sales + expect(vendor_sale_array).to be_an Array + expect(vendor_sale_array.length).to eq 7 + end + end + + describe "revenue" do + it "returns the sum of all of the vendor's sales" do + total_sales = @vendor.revenue + expect(total_sales).to eq 38259 + end + end + + describe "self.by_market(market_id)" do + it "returns all of the vendors with the given market_id" do + vendors_at_market = FarMar::Vendor.by_market(4) + expect(vendors_at_market).to be_an Array + expect(vendors_at_market.length).to eq 4 + expect(vendors_at_market[0].vendor_id).to eq 13 end end end From dcef780cc4a0d1ff47988ea9d92a7122f78bc885 Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Wed, 21 Oct 2015 14:56:09 -0700 Subject: [PATCH 05/14] Add vendor method to Product class plus tests --- lib/far_mar/product.rb | 9 +++++++++ spec/far_mar/market_spec.rb | 2 +- spec/far_mar/product_spec.rb | 10 +++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index a31196f2..39b486c2 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -24,5 +24,14 @@ def self.find(id) end end + def vendor + product_vendor_array = FarMar::Vendor.all + product_vendor_array.each do |row| + if row.vendor_id == @vendor_id + return row + end + end + end + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index ff0affc8..d20e80ac 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -17,7 +17,7 @@ end describe "self.find" do - it "returns an instance of Market where the value of the id field in the CSV matches the passed parameter" do + it "returns market instance where the value of the id the passed parameter" do result = FarMar::Market.find(3) expect(result).to be_an Object expect(result.market_id).to eq 3 diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 00ac9fac..cacb99d7 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -17,12 +17,20 @@ end describe "self.find" do - it "returns an instance of Product where the value of the id field in the CSV matches the passed parameter" do + it "returns product instance where the value of the id the passed parameter" do result = FarMar::Product.find(3) expect(result).to be_an Object expect(result.product_id).to eq 3 end end + describe "vendor" do + it "returns the vendor instance that is associated with the product" do + product_vendor = @product.vendor + expect(product_vendor).to be_an Object + expect(product_vendor.vendor_id).to eq 2 + end + end + end From 4f3ac57229751edc306a24a8f3fc0003e6cff695 Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Wed, 21 Oct 2015 15:24:53 -0700 Subject: [PATCH 06/14] Add sales method to Product class plus tests --- lib/far_mar/product.rb | 11 +++++++++++ spec/far_mar/product_spec.rb | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 39b486c2..e8b99f69 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -33,5 +33,16 @@ def vendor end end + def sales + product_sales_array = [] + sale_array = FarMar::Sale.all + sale_array.each do |sale| + if sale.product_id == @product_id + product_sales_array.push(sale) + end + end + return product_sales_array + end + end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index cacb99d7..4a276ed1 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -32,5 +32,13 @@ end end + describe "sales" do + it "returns a collection of sale instances that are associated with product_id" do + product_sale_array = @product.sales + expect(product_sale_array).to be_an Array + expect(product_sale_array.length).to eq 2 + end + end + end From 3cbba117c3eb0f5f72e4e00787d7cf2860c79dd1 Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Wed, 21 Oct 2015 15:36:27 -0700 Subject: [PATCH 07/14] Add number_of_sales method to Product class --- lib/far_mar/product.rb | 3 +++ spec/far_mar/product_spec.rb | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index e8b99f69..4321c72e 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -44,5 +44,8 @@ def sales return product_sales_array end + def number_of_sales + return sales.length + end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 4a276ed1..b261461e 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -40,5 +40,12 @@ end end + describe "number_of_sales" do + it "returns the number of times a product has been sold" do + total_times_sold = @product.number_of_sales + expect(total_times_sold).to eq 2 + end + end + end From 3fb697e47fc375cf40479f8ab44846c605702960 Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Wed, 21 Oct 2015 15:47:24 -0700 Subject: [PATCH 08/14] Add self.by_vendor(vendor_id) to Product class plus tests --- lib/far_mar/product.rb | 6 ++++++ spec/far_mar/product_spec.rb | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 4321c72e..5b72f483 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -47,5 +47,11 @@ def sales def number_of_sales return sales.length end + + def self.by_vendor(vendor_id) + vendor = FarMar::Vendor.find(vendor_id) + return vendor.products + end + end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index b261461e..6edc9be9 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -47,5 +47,12 @@ end end - + describe "self.by_vendor(vendor_id)" do + it "returns all of the products with the given vendor_id" do + products_by_vendor = FarMar::Product.by_vendor(2) + expect(products_by_vendor).to be_an Array + expect(products_by_vendor.length).to eq 2 + expect(products_by_vendor[0].product_id).to eq 2 + end + end end From 4d10fe03a64dfd80e083c061c7ac2cf4065eb3eb Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Wed, 21 Oct 2015 15:59:22 -0700 Subject: [PATCH 09/14] Add product method to Sale class plus tests --- lib/far_mar/sale.rb | 17 +++++++++++++++++ spec/far_mar/sale_spec.rb | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 7abdb148..b2e74e62 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -26,5 +26,22 @@ def self.find(id) end end + def vendor + sale_vendor_array = FarMar::Vendor.all + sale_vendor_array.each do |row| + if row.vendor_id == @vendor_id + return row + end + end + end + + def product + sale_product_array = FarMar::Product.all + sale_product_array.each do |row| + if row.product_id == @product_id + return row + end + end + end end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 9dd06ad4..a9e2348b 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -24,4 +24,19 @@ end end + describe "vendor" do + it "returns the vendor instance that is associated with the sale" do + sale_vendor = @sale.vendor + expect(sale_vendor).to be_an Object + expect(sale_vendor.vendor_id).to eq 2 + end + end + + describe "product" do + it "returns the product instance that is associated with the sale" do + sale_product = @sale.product + expect(sale_product).to be_an Object + expect(sale_product.product_id).to eq 4 + end + end end From d89ef53178ef9aac8514c55c91ad311d7bb2944e Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Thu, 22 Oct 2015 11:03:18 -0700 Subject: [PATCH 10/14] Add self.between(beginning_time, end_time) method to Sale class plus tests --- lib/far_mar/sale.rb | 10 +++++++++- spec/far_mar/sale_spec.rb | 21 +++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index b2e74e62..9325c703 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -6,7 +6,7 @@ class Sale def initialize(sale_id, amount, purchase_time, vendor_id, product_id) @sale_id = sale_id @amount = amount - @purchase_time = purchase_time + @purchase_time = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") @vendor_id = vendor_id @product_id = product_id end @@ -43,5 +43,13 @@ def product end end end + + def self.between(beginning_time, end_time) + sales_by_time = [] + FarMar::Sale.all.find_all do |sale_object| + sale_object.purchase_time >= beginning_time && sale_object.purchase_time <= end_time + sales_by_time.push(sale_object) + end + end end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index a9e2348b..d983711a 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Sale do before :each do - @sale = FarMar::Sale.new(10, "150", "12:00pm", 2, 4) + @sale = FarMar::Sale.new(10, "150", "2013-11-07 04:34:56 -0800", 2, 4) end describe "#initialize" do it "creates a new instance" do @@ -17,7 +17,7 @@ end describe "self.find" do - it "returns an instance of Sale where the value of the id field in the CSV matches the passed parameter" do + it "returns sale instance where the value of the id the passed parameter" do result = FarMar::Sale.find(3) expect(result).to be_an Object expect(result.sale_id).to eq 3 @@ -39,4 +39,21 @@ expect(sale_product.product_id).to eq 4 end end + + describe "self.between(beginning_time, end_time)" do + before :each do + @beginning_time = DateTime.strptime("2013-11-07 04:34:56 -0800", "%Y-%m-%d %H:%M:%S %z") + @end_time = DateTime.strptime("2013-11-10 01:51:24 -0800", "%Y-%m-%d %H:%M:%S %z") + @begin = DateTime.strptime("2013-11-07 05:00:24 -0800", "%Y-%m-%d %H:%M:%S %z") + @end = DateTime.strptime("2013-11-09 10:00:24 -0800", "%Y-%m-%d %H:%M:%S %z") + @id = 10 + end + it "returns a collection of sales between two specific times" do + sales_by_time = FarMar::Sale.between(@beginning_time, @end_time) + expect(FarMar::Sale.find(10).purchase_time).to be_between @beginning_time, @end_time + expect(@end).to be_between @beginning_time, @end_time + expect(sales_by_time).to be_an Array + expect(sales_by_time[0]).to be_an Object + end + end end From 6b3a1db40fd815c102ee0414076756d00fdaf53e Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Thu, 22 Oct 2015 11:39:45 -0700 Subject: [PATCH 11/14] Refactoring of sale_spec and add products method to Market class plus tests --- lib/far_mar/market.rb | 8 ++++++++ spec/far_mar/market_spec.rb | 10 +++++++++- spec/far_mar/sale_spec.rb | 3 --- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index e4b7b51b..0d2e3021 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -36,5 +36,13 @@ def vendors end return market_vendor_array end + def products + market_products = [] + vendor_array = self.vendors + vendor_array.each do |vendor| + market_products += vendor.products + end + return market_products + end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index d20e80ac..9c2a4577 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -25,10 +25,18 @@ end describe "vendors" do - it "return a collection of vendor instances that are associated with market_id" do + it "returns a collection of vendor instances that are associated with market_id" do market_vendor_array = @market.vendors expect(market_vendor_array).to be_an Array expect(market_vendor_array.length).to eq 6 end end + + describe "products" do + it "returns a collection of product instances via the Vendor class" do + market_product_array = @market.products + expect(market_product_array).to be_an Array + expect(market_product_array[0]).to be_an_instance_of FarMar::Product + end + end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index d983711a..ec8441e8 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -44,14 +44,11 @@ before :each do @beginning_time = DateTime.strptime("2013-11-07 04:34:56 -0800", "%Y-%m-%d %H:%M:%S %z") @end_time = DateTime.strptime("2013-11-10 01:51:24 -0800", "%Y-%m-%d %H:%M:%S %z") - @begin = DateTime.strptime("2013-11-07 05:00:24 -0800", "%Y-%m-%d %H:%M:%S %z") - @end = DateTime.strptime("2013-11-09 10:00:24 -0800", "%Y-%m-%d %H:%M:%S %z") @id = 10 end it "returns a collection of sales between two specific times" do sales_by_time = FarMar::Sale.between(@beginning_time, @end_time) expect(FarMar::Sale.find(10).purchase_time).to be_between @beginning_time, @end_time - expect(@end).to be_between @beginning_time, @end_time expect(sales_by_time).to be_an Array expect(sales_by_time[0]).to be_an Object end From 5cb2a8ad4b9746571bd00afcf2c9484c49733e49 Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Thu, 22 Oct 2015 17:21:22 -0700 Subject: [PATCH 12/14] Add products method and self.search(search_team) methods to Market class plus tests --- lib/far_mar.rb | 1 + lib/far_mar/market.rb | 36 +++++++++++++++++++++++++++++++----- lib/far_mar/product.rb | 12 +++++++----- lib/far_mar/sale.rb | 12 +++++++----- lib/far_mar/vendor.rb | 12 +++++++----- spec/far_mar/market_spec.rb | 29 +++++++++++++++++++++++++++++ spec/far_mar/sale_spec.rb | 1 + 7 files changed, 83 insertions(+), 20 deletions(-) diff --git a/lib/far_mar.rb b/lib/far_mar.rb index db54a95a..0e045c26 100644 --- a/lib/far_mar.rb +++ b/lib/far_mar.rb @@ -4,3 +4,4 @@ require './lib/far_mar/vendor' require './lib/far_mar/product' require './lib/far_mar/sale' +require 'pry' diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 0d2e3021..b71515bc 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,3 +1,4 @@ + module FarMar class Market @@ -14,12 +15,14 @@ def initialize(market_id, name, address, city, county, state, zip) end def self.all - markets_csv = CSV.read("./support/markets.csv") - market_instances = [] - markets_csv.each do |row| - market_instances.push(Market.new(row[0].to_i, row[1], row[2], row[3], row[4], row[5], row[6])) + @@market_instances ||= [] + if @@market_instances == [] + markets_csv = CSV.read("./support/markets.csv") + markets_csv.each do |row| + @@market_instances.push(Market.new(row[0].to_i, row[1], row[2], row[3], row[4], row[5], row[6])) + end end - return market_instances + return @@market_instances end def self.find(id) self.all.find do |market| @@ -44,5 +47,28 @@ def products end return market_products end + def self.search(search_term) + search_term = search_term.downcase + name_array = [] + name_search = FarMar::Market.all.map do |market| + {market.name.downcase => market} + end + name_search2 = FarMar::Vendor.all.map do |vendor| + {vendor.name.downcase => FarMar::Market.find(vendor.market_id)} + end + (name_search + name_search2).each do |hash| + if hash.keys[0].include?(search_term) + name_array += hash.values + end + end + return name_array.uniq + end + + def preferred_vendor + vendor_sales = [] + sales_per_vendor = FarMar::Vendor.all.map do |vendor| + vendor.revenue + end + end end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 5b72f483..d3a2692a 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -11,12 +11,14 @@ def initialize(product_id, name, vendor_id) def self.all - products_csv = CSV.read("./support/products.csv") - product_instances = [] - products_csv.each do |row| - product_instances.push(Product.new(row[0].to_i, row[1], row[2].to_i)) + @@product_instances ||= [] #@@product = @@product || [] + if @@product_instances == [] + products_csv = CSV.read("./support/products.csv") + products_csv.each do |row| + @@product_instances.push(Product.new(row[0].to_i, row[1], row[2].to_i)) + end end - return product_instances + return @@product_instances end def self.find(id) self.all.find do |product| diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 9325c703..d26c7ee2 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -13,12 +13,14 @@ def initialize(sale_id, amount, purchase_time, vendor_id, product_id) def self.all - sales_csv = CSV.read("./support/sales.csv") - sale_instances = [] - sales_csv.each do |row| - sale_instances.push(Sale.new(row[0].to_i, row[1].to_i, row[2], row[3].to_i, row[4].to_i)) + @@sale_instances ||= [] + if @@sale_instances == [] + sales_csv = CSV.read("./support/sales.csv") + sales_csv.each do |row| + @@sale_instances.push(Sale.new(row[0].to_i, row[1].to_i, row[2], row[3].to_i, row[4].to_i)) + end end - return sale_instances + return @@sale_instances end def self.find(id) self.all.find do |sale| diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 106be9bf..c4440409 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -11,12 +11,14 @@ def initialize(vendor_id, name, employees, market_id) end def self.all - vendor_csv = CSV.read("./support/vendors.csv") - vendor_instances = [] - vendor_csv.each do |row| - vendor_instances.push(Vendor.new(row[0].to_i, row[1], row[2], row[3].to_i)) + @@vendor_instances ||= [] + if @@vendor_instances == [] + vendor_csv = CSV.read("./support/vendors.csv") + vendor_csv.each do |row| + @@vendor_instances.push(Vendor.new(row[0].to_i, row[1], row[2], row[3].to_i)) + end end - return vendor_instances + return @@vendor_instances end def self.find(id) self.all.find do |vendor| diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 9c2a4577..d1593612 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -39,4 +39,33 @@ expect(market_product_array[0]).to be_an_instance_of FarMar::Product end end + + describe "self.search(search_term)" do + describe "does the search part of the method" do + it "searches market names" do + expect(FarMar::Market.find(4).name).to be_a String + expect(FarMar::Market.find(4).name).to include("Preston") + end + it "searches vendor names" do + expect(FarMar::Vendor.find(2).name).to be_a String + expect(FarMar::Vendor.find(2).name).to include("Hamill") + end + end + describe "creates a collection of market instances that contain search_term" do + it "creates a collection of markets from market name search" do + expect(FarMar::Market.search("People").length).to eq 2 + expect(FarMar::Market.search("People")[0].market_id).to eq 1 + end + it "creates a collection of markets from vendor name search" do + expect(FarMar::Market.search("Donnelly").length).to eq 17 + expect(FarMar::Market.search("donnelly")[0].market_id).to eq 3 + end + end + + describe "preferred_vendor" do + it "returns the vendor with the highest revenue" do + expect(FarMar::Market..preferred_vendor).to be_an Object + end + end + end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index ec8441e8..5fa063db 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -51,6 +51,7 @@ expect(FarMar::Sale.find(10).purchase_time).to be_between @beginning_time, @end_time expect(sales_by_time).to be_an Array expect(sales_by_time[0]).to be_an Object + expect(sales_by_time).to include(FarMar::Sale.find(10)) end end end From 7d3afca52fe3d6ceb443697687fb976a85e9fe1e Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Fri, 23 Oct 2015 15:20:46 -0700 Subject: [PATCH 13/14] Add random spec test to spec_helper, add preferred_vendor(date) to Market class --- lib/far_mar.rb | 2 +- lib/far_mar/market.rb | 11 ++++++----- lib/far_mar/vendor.rb | 5 ++++- spec/far_mar/market_spec.rb | 14 ++++++++++++-- spec/spec_helper.rb | 4 ++++ 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/far_mar.rb b/lib/far_mar.rb index 0e045c26..659b93f4 100644 --- a/lib/far_mar.rb +++ b/lib/far_mar.rb @@ -1,7 +1,7 @@ +require 'pry' require 'csv' require 'time' require './lib/far_mar/market' require './lib/far_mar/vendor' require './lib/far_mar/product' require './lib/far_mar/sale' -require 'pry' diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index b71515bc..26ae3ca8 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -64,11 +64,12 @@ def self.search(search_term) return name_array.uniq end - def preferred_vendor - vendor_sales = [] - sales_per_vendor = FarMar::Vendor.all.map do |vendor| - vendor.revenue - end + def preferred_vendor(date = nil) + market_vendors = self.vendors + top_vendor = market_vendors.max_by do |vendor| + vendor.revenue(date) + end + return top_vendor end end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index c4440409..09dab969 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -57,10 +57,13 @@ def sales return vendor_sales_array end - def revenue + def revenue(date = nil) + date = DateTime.strptime(date, "%Y-%m-%d") if date != nil total_revenue = 0 sales.each do |sale| + if sale.purchase_time.to_date == date || date == nil total_revenue += sale.amount + end end return total_revenue end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index d1593612..f3b057c1 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -2,7 +2,8 @@ describe FarMar::Market do before :each do - @market = FarMar::Market.new(1, "Meighan", "1234 N St", "Seattle", "WA", "King", "98103") + @market = FarMar::Market.new(1, "Meighan's Ranch", "1234 N St", "Seattle", "WA", "King", "98103") + @market_1 = FarMar::Market.new(1,"People's Co-op Farmers Market","30th and Burnside","Portland","Multnomah","Oregon","97202") end describe "#initialize" do it "creates a new instance" do @@ -64,7 +65,16 @@ describe "preferred_vendor" do it "returns the vendor with the highest revenue" do - expect(FarMar::Market..preferred_vendor).to be_an Object + expect(@market_1.preferred_vendor).to be_an_instance_of FarMar::Vendor + expect(@market_1.preferred_vendor.vendor_id).to eq 5 + expect(@market_1.preferred_vendor.revenue).to eq 61749 + end + end + + describe "preferred_vendor(date)" do + it "returns the vendor with the highest revenue for the given date" do + @date = DateTime.strptime("2013-11-07", "%Y-%m-%d") + expect(@market_1.preferred_vendor("2013-11-07")).to be_an_instance_of FarMar::Vendor end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 42436260..b5c43b74 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,3 +2,7 @@ SimpleCov.start require "./lib/far_mar" + +RSpec.configure do |config| + config.order = 'random' +end From 740d436ef71fe0a11137c50a94ed590e746ba118 Mon Sep 17 00:00:00 2001 From: Meighan Rasley Date: Fri, 23 Oct 2015 15:36:48 -0700 Subject: [PATCH 14/14] Add worst vendor method to Market class --- lib/far_mar/market.rb | 9 +++++++++ spec/far_mar/market_spec.rb | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 26ae3ca8..89ff4892 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -71,5 +71,14 @@ def preferred_vendor(date = nil) end return top_vendor end + + def worst_vendor(date = nil) + market_vendors = self.vendors + last_vendor = market_vendors.min_by do |vendor| + vendor.revenue(date) + end + return last_vendor + end + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index f3b057c1..26213cc0 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -77,5 +77,13 @@ expect(@market_1.preferred_vendor("2013-11-07")).to be_an_instance_of FarMar::Vendor end end + + describe "worst_vendor" do + it "returns the vendor with the lowest revenue" do + expect(@market_1.worst_vendor).to be_an_instance_of FarMar::Vendor + expect(@market_1.worst_vendor.vendor_id).to eq 6 + expect(@market_1.worst_vendor.revenue).to eq 2977 + end + end end end