From bf4900409580464231cd523c051b43556a70ba97 Mon Sep 17 00:00:00 2001 From: Nadine Date: Mon, 14 Mar 2016 12:49:35 -0700 Subject: [PATCH 01/11] Baseline done --- .gitignore | 36 ++++++++++++++++++++++++++++++++++++ .ruby-gemset | 1 + .ruby-version | 1 + Rakefile | 10 ++++++++++ far_mar.rb | 8 ++++++++ lib/markets.rb | 3 +++ lib/products.rb | 3 +++ lib/sales.rb | 3 +++ lib/vendors.rb | 3 +++ specs/sales_spec.rb | 9 +++++++++ specs/spec_helper.rb | 11 +++++++++++ 11 files changed, 88 insertions(+) create mode 100644 .gitignore create mode 100644 .ruby-gemset create mode 100644 .ruby-version create mode 100644 Rakefile create mode 100644 far_mar.rb create mode 100644 lib/markets.rb create mode 100644 lib/products.rb create mode 100644 lib/sales.rb create mode 100644 lib/vendors.rb create mode 100644 specs/sales_spec.rb create mode 100644 specs/spec_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..38ba96f0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/test/tmp/ +/test/version_tmp/ +/tmp/ +/.DS_Store + +## Specific to RubyMotion: +.dat* +.repl_history +build/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalisation: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 00000000..73f861e5 --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +farmar diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..276cbf9e --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.3.0 diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..f45f7fcf --- /dev/null +++ b/Rakefile @@ -0,0 +1,10 @@ +require "rake/testtask" + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList["specs/*_spec.rb"] + +end + +task default: :test \ No newline at end of file diff --git a/far_mar.rb b/far_mar.rb new file mode 100644 index 00000000..eee969a5 --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,8 @@ +require 'CSV' + +module FarMar + require_relative "./lib/markets.rb" + require_relative "./lib/products.rb" + require_relative "./lib/sales.rb" + require_relative "./lib/vendors.rb" +end \ No newline at end of file diff --git a/lib/markets.rb b/lib/markets.rb new file mode 100644 index 00000000..de541752 --- /dev/null +++ b/lib/markets.rb @@ -0,0 +1,3 @@ +class Markets + +end \ No newline at end of file diff --git a/lib/products.rb b/lib/products.rb new file mode 100644 index 00000000..8571130b --- /dev/null +++ b/lib/products.rb @@ -0,0 +1,3 @@ +class Products + +end \ No newline at end of file diff --git a/lib/sales.rb b/lib/sales.rb new file mode 100644 index 00000000..7869512c --- /dev/null +++ b/lib/sales.rb @@ -0,0 +1,3 @@ +class Sales + +end \ No newline at end of file diff --git a/lib/vendors.rb b/lib/vendors.rb new file mode 100644 index 00000000..625b8bd7 --- /dev/null +++ b/lib/vendors.rb @@ -0,0 +1,3 @@ +class Vendors + +end \ No newline at end of file diff --git a/specs/sales_spec.rb b/specs/sales_spec.rb new file mode 100644 index 00000000..e82ecfd0 --- /dev/null +++ b/specs/sales_spec.rb @@ -0,0 +1,9 @@ +require_relative "./spec_helper" +require_relative "../far_mar" + + +describe Sales do + it "it is an object we have acccess to" do + Sales.wont_be_nil + end +end \ No newline at end of file diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..3ccb41dd --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,11 @@ +require "simplecov" +SimpleCov.start + +require "minitest" +require "minitest/spec" +require "minitest/autorun" +require "minitest/reporters" + +#give us some really pretty output +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + From 8748768cf60cca2c2bf54f8e65a8f3aefedf9106 Mon Sep 17 00:00:00 2001 From: Nadine Date: Mon, 14 Mar 2016 13:02:53 -0700 Subject: [PATCH 02/11] Fixed baseline --- lib/markets.rb | 2 +- lib/products.rb | 2 +- lib/sales.rb | 2 +- lib/vendors.rb | 2 +- specs/markets_spec.rb | 9 +++++++++ specs/products_spec.rb | 9 +++++++++ specs/sales_spec.rb | 4 ++-- specs/vendors_spec.rb | 9 +++++++++ 8 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 specs/markets_spec.rb create mode 100644 specs/products_spec.rb create mode 100644 specs/vendors_spec.rb diff --git a/lib/markets.rb b/lib/markets.rb index de541752..30fff0fe 100644 --- a/lib/markets.rb +++ b/lib/markets.rb @@ -1,3 +1,3 @@ -class Markets +class FarMar::Markets end \ No newline at end of file diff --git a/lib/products.rb b/lib/products.rb index 8571130b..96bbf0ce 100644 --- a/lib/products.rb +++ b/lib/products.rb @@ -1,3 +1,3 @@ -class Products +class FarMar::Products end \ No newline at end of file diff --git a/lib/sales.rb b/lib/sales.rb index 7869512c..a1ebe4ff 100644 --- a/lib/sales.rb +++ b/lib/sales.rb @@ -1,3 +1,3 @@ -class Sales +class FarMar::Sales end \ No newline at end of file diff --git a/lib/vendors.rb b/lib/vendors.rb index 625b8bd7..77aac7ce 100644 --- a/lib/vendors.rb +++ b/lib/vendors.rb @@ -1,3 +1,3 @@ -class Vendors +class FarMar::Vendors end \ No newline at end of file diff --git a/specs/markets_spec.rb b/specs/markets_spec.rb new file mode 100644 index 00000000..2a752c2a --- /dev/null +++ b/specs/markets_spec.rb @@ -0,0 +1,9 @@ +require_relative "./spec_helper" +require_relative "../far_mar" + + +describe FarMar::Markets do + it "it is an object we have acccess to" do + FarMar::Markets.wont_be_nil + end +end \ No newline at end of file diff --git a/specs/products_spec.rb b/specs/products_spec.rb new file mode 100644 index 00000000..87a33869 --- /dev/null +++ b/specs/products_spec.rb @@ -0,0 +1,9 @@ +require_relative "./spec_helper" +require_relative "../far_mar" + + +describe FarMar::Products do + it "it is an object we have acccess to" do + FarMar::Products.wont_be_nil + end +end \ No newline at end of file diff --git a/specs/sales_spec.rb b/specs/sales_spec.rb index e82ecfd0..ff718ad8 100644 --- a/specs/sales_spec.rb +++ b/specs/sales_spec.rb @@ -2,8 +2,8 @@ require_relative "../far_mar" -describe Sales do +describe FarMar::Sales do it "it is an object we have acccess to" do - Sales.wont_be_nil + FarMar::Sales.wont_be_nil end end \ No newline at end of file diff --git a/specs/vendors_spec.rb b/specs/vendors_spec.rb new file mode 100644 index 00000000..b2490a29 --- /dev/null +++ b/specs/vendors_spec.rb @@ -0,0 +1,9 @@ +require_relative "./spec_helper" +require_relative "../far_mar" + + +describe FarMar::Vendors do + it "it is an object we have acccess to" do + FarMar::Vendors.wont_be_nil + end +end \ No newline at end of file From edc19001e0d4787728f99bebbee047228f9520a0 Mon Sep 17 00:00:00 2001 From: Nadine Date: Mon, 14 Mar 2016 13:40:29 -0700 Subject: [PATCH 03/11] changes class names in singular --- lib/markets.rb | 2 +- lib/products.rb | 2 +- lib/sales.rb | 2 +- lib/vendors.rb | 2 +- specs/markets_spec.rb | 4 ++-- specs/products_spec.rb | 4 ++-- specs/sales_spec.rb | 4 ++-- specs/vendors_spec.rb | 4 ++-- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/markets.rb b/lib/markets.rb index 30fff0fe..b3f8e2fa 100644 --- a/lib/markets.rb +++ b/lib/markets.rb @@ -1,3 +1,3 @@ -class FarMar::Markets +class FarMar::Market end \ No newline at end of file diff --git a/lib/products.rb b/lib/products.rb index 96bbf0ce..6b7c91f5 100644 --- a/lib/products.rb +++ b/lib/products.rb @@ -1,3 +1,3 @@ -class FarMar::Products +class FarMar::Product end \ No newline at end of file diff --git a/lib/sales.rb b/lib/sales.rb index a1ebe4ff..ae8c1fa2 100644 --- a/lib/sales.rb +++ b/lib/sales.rb @@ -1,3 +1,3 @@ -class FarMar::Sales +class FarMar::Sale end \ No newline at end of file diff --git a/lib/vendors.rb b/lib/vendors.rb index 77aac7ce..4638e11a 100644 --- a/lib/vendors.rb +++ b/lib/vendors.rb @@ -1,3 +1,3 @@ -class FarMar::Vendors +class FarMar::Vendor end \ No newline at end of file diff --git a/specs/markets_spec.rb b/specs/markets_spec.rb index 2a752c2a..74a05ea3 100644 --- a/specs/markets_spec.rb +++ b/specs/markets_spec.rb @@ -2,8 +2,8 @@ require_relative "../far_mar" -describe FarMar::Markets do +describe FarMar::Market do it "it is an object we have acccess to" do - FarMar::Markets.wont_be_nil + FarMar::Market.wont_be_nil end end \ No newline at end of file diff --git a/specs/products_spec.rb b/specs/products_spec.rb index 87a33869..e05049b3 100644 --- a/specs/products_spec.rb +++ b/specs/products_spec.rb @@ -2,8 +2,8 @@ require_relative "../far_mar" -describe FarMar::Products do +describe FarMar::Product do it "it is an object we have acccess to" do - FarMar::Products.wont_be_nil + FarMar::Product.wont_be_nil end end \ No newline at end of file diff --git a/specs/sales_spec.rb b/specs/sales_spec.rb index ff718ad8..6d870f4b 100644 --- a/specs/sales_spec.rb +++ b/specs/sales_spec.rb @@ -2,8 +2,8 @@ require_relative "../far_mar" -describe FarMar::Sales do +describe FarMar::Sale do it "it is an object we have acccess to" do - FarMar::Sales.wont_be_nil + FarMar::Sale.wont_be_nil end end \ No newline at end of file diff --git a/specs/vendors_spec.rb b/specs/vendors_spec.rb index b2490a29..df64c910 100644 --- a/specs/vendors_spec.rb +++ b/specs/vendors_spec.rb @@ -2,8 +2,8 @@ require_relative "../far_mar" -describe FarMar::Vendors do +describe FarMar::Vendor do it "it is an object we have acccess to" do - FarMar::Vendors.wont_be_nil + FarMar::Vendor.wont_be_nil end end \ No newline at end of file From 6654843cca2e5c6ee26711b3847ac9f14486f463 Mon Sep 17 00:00:00 2001 From: Nadine Date: Tue, 15 Mar 2016 15:09:23 -0700 Subject: [PATCH 04/11] Added self and all method to all files including the tests. --- lib/markets.rb | 62 ++++++++++++++++++++++++++++++++++++++++ lib/products.rb | 53 ++++++++++++++++++++++++++++++++++ lib/sales.rb | 56 ++++++++++++++++++++++++++++++++++++ lib/vendors.rb | 65 ++++++++++++++++++++++++++++++++++++++++++ specs/markets_spec.rb | 40 ++++++++++++++++++++++++++ specs/products_spec.rb | 35 +++++++++++++++++++++++ specs/sales_spec.rb | 37 ++++++++++++++++++++++++ specs/vendors_spec.rb | 36 +++++++++++++++++++++++ 8 files changed, 384 insertions(+) diff --git a/lib/markets.rb b/lib/markets.rb index b3f8e2fa..aa695449 100644 --- a/lib/markets.rb +++ b/lib/markets.rb @@ -1,3 +1,65 @@ + class FarMar::Market + attr_accessor :ID, :name, :address, :city, :county, :state, :zip + + MARKET_DATA = "./support/markets.csv" + + def initialize(market_hash) + @ID = market_hash[:ID].to_i + @name = market_hash[:name] + @address = market_hash[:address] + @city = market_hash[:city] + @county = market_hash[:county] + @state = market_hash[:state] + @zip = market_hash[:zip] + + end + #returns a collection of instances, representing all of + #the objects described in the CSV + def self.all + all = [] + CSV.foreach(MARKET_DATA, "r") do |line| + market = FarMar::Market.new(ID: line[0].to_i, name: line[1], + address: line[2], city: line[3], county: line[4], + zip: line[6]) + all << market + end + + return all + end + + #returns an instance of the object where the value of the + # id field in the CSV matches the passed parameter. + def self.find(id) + find = [] + CSV.foreach(MARKET_DATA, "r") do |line| + if line[0] == id.to_s + find =[line[0].to_i, line[1], line[2], line[3], + line[4], line[5], line[6]] + return find + break + end + end + return nil + end + + #returns a collection of FarMar::Vendor instances that + #are associated with the market by the market_id field. + def vendor + vendors = [] + vendors << FarMar::Vendor.all.select{|key,value| key == "market_id" && value == @id} + + end + + + + + + + + + + + end \ No newline at end of file diff --git a/lib/products.rb b/lib/products.rb index 6b7c91f5..953b27b2 100644 --- a/lib/products.rb +++ b/lib/products.rb @@ -1,3 +1,56 @@ class FarMar::Product + PRODUCT_DATA = "./support/products.csv" + def initialize(product_hash) + @ID = product_hash[:ID].to_i + @name = product_hash[:name] + @vendor_id = product_hash[:vendor_id].to_i + + end + #returns a collection of instances, representing all of + #the objects described in the CSV + def self.all + all = [] + CSV.foreach(PRODUCT_DATA, "r") do |line| + product = FarMar::Product.new(ID: line[0].to_i, name: line[1], + vendor_id: line[2].to_i) + all << product + end + return all + end + + #returns an instance of the object where the value of the + # id field in the CSV matches the passed parameter. + def self.find(id) + find = [] + CSV.foreach(PRODUCT_DATA, "r") do |line| + if line[0] == id.to_s + find =[line[0].to_i, line[1], line[2].to_i] + return find + break + end + end + return nil + end + + def vendor + + end + + #returns a collection of FarMar::Sale instances that are + #associated using the FarMar::Sale product_id field. + def sales + + end + + #returns the number of times this product has been sold. + def number_of_sales + + end + + #returns all of the products with the given vendor_id + def self.by_vendor(vendor_id) + + end + end \ No newline at end of file diff --git a/lib/sales.rb b/lib/sales.rb index ae8c1fa2..ea26e853 100644 --- a/lib/sales.rb +++ b/lib/sales.rb @@ -1,3 +1,59 @@ class FarMar::Sale + SALE_DATA = "./support/sales.csv" + + def initialize(sale_hash) + @ID = sale_hash[:ID].to_i + @amount = sale_hash[:amount].to_i + @purchase_time = sale_hash[:purchase_time] + @vendor_id = sale_hash[:vendor_id].to_i + @product_id = sale_hash[:product_id].to_i + + end + + #returns a collection of instances, representing all of + #the objects described in the CSV + def self.all + all = [] + CSV.foreach(SALE_DATA, "r") do |line| + sale = FarMar::Sale.new(ID: line[0].to_i, + amount: line[1].to_i, purchase_time: line[2], + vendor_id: line[3].to_i, product_id: line[4].to_i) + all << sale + end + return all + end + + #returns an instance of the object where the value of the + # id field in the CSV matches the passed parameter. + def self.find(id) + find = [] + CSV.foreach(SALE_DATA, "r") do |line| + if line[0] == id.to_s + find =[line[0].to_i, line[1].to_i, line[2].to_i, + line[3].to_i, line[4].to_i] + return find + break + end + end + return nil + end + + #returns the FarMar::Vendor instance that is associated + # with this sale using the FarMar::Sale vendor_id field + def vendor + + end + + #returns the FarMar::Product instance that is associated + #with this sale using the FarMar::Sale product_id field + def product + + end + + #returns a collection of FarMar::Sale objects where the + #purchase time is between the two times given as arguments + def self.between(beginning_time, end_time) + + end end \ No newline at end of file diff --git a/lib/vendors.rb b/lib/vendors.rb index 4638e11a..4997fd53 100644 --- a/lib/vendors.rb +++ b/lib/vendors.rb @@ -1,3 +1,68 @@ class FarMar::Vendor + VENDOR_DATA = "./support/vendors.csv" + + def initialize(vendor_hash) + @ID = vendor_hash[:ID].to_i + @name = vendor_hash[:name] + @no_of_employees = vendor_hash[:no_of_employees].to_i + @market_id = vendor_hash[:market_id].to_i + + end + #returns a collection of instances, representing all of + #the objects described in the CSV + def self.all + all = [] + CSV.foreach(VENDOR_DATA, "r") do |line| + vendor = FarMar::Vendor.new(ID: line[0].to_i, name: line[1], + no_of_employees: line[2].to_i, market_id: line[3].to_i) + all << vendor + end + return all + end + + #returns an instance of the object where the value of the + # id field in the CSV matches the passed parameter. + def self.find(id) + find = [] + CSV.foreach(VENDOR_DATA, "r") do |line| + if line[0] == id.to_s + find =[line[0].to_i, line[1], line[2].to_i, + line[3].to_i] + return find + break + end + end + return nil + end + + + #returns the FarMar::Market instance that is associated + #with this vendor using the FarMar::Vendor market_id field + def market + + end + + #returns a collection of FarMar::Product instances that + #are associated by the FarMar::Product vendor_id field. + def products + + end + + #returns a collection of FarMar::Sale instances that are + #associated by the vendor_id field. + def sales + + end + + #returns the the sum of all of the vendor's sales(in cents) + def revenue + + end + + #returns all of the vendors with the given market_id + def self.by_market(market_id) + + end + end \ No newline at end of file diff --git a/specs/markets_spec.rb b/specs/markets_spec.rb index 74a05ea3..ec396180 100644 --- a/specs/markets_spec.rb +++ b/specs/markets_spec.rb @@ -3,7 +3,47 @@ describe FarMar::Market do + it "it is an object we have acccess to" do FarMar::Market.wont_be_nil end + + describe "Market#find" do + let (:market15) {FarMar::Market.find(15)} + + it "checks that market15 is instance of an array" do + market15.must_be_instance_of(Array) + end + + it "returns nil for id numbers that don't exist" do + FarMar::Market.find(10_000).must_equal(nil) + end + + it "checks the values of each variable for market15" do + market15[0].must_equal(15) + market15[1].must_equal("Farmers Market in Denison") + market15[2].must_equal("1 block west of Main St.") + market15[3].must_equal("Denison") + market15[4].must_equal(nil) + market15[5].must_equal("Texas") + market15[6].must_equal("75090") + end + end + + describe "Market#all" do + let (:market) {FarMar::Market.all} + + it "market should be instance of hash" do + market.must_be_instance_of(Array) + end + + it "market should have 500 entries" do + market.count.must_equal(500) + end + + it "should return array full of instances" do + classes = market.map{|m| m.class} + classes.uniq.must_equal([FarMar::Market]) + end + end end \ No newline at end of file diff --git a/specs/products_spec.rb b/specs/products_spec.rb index e05049b3..4980c697 100644 --- a/specs/products_spec.rb +++ b/specs/products_spec.rb @@ -6,4 +6,39 @@ it "it is an object we have acccess to" do FarMar::Product.wont_be_nil end + + describe "Product#find" do + let (:product219) {FarMar::Product.find(219)} + + it "checks that product219 is instance of an Array" do + product219.must_be_instance_of(Array) + end + + it "returns nil for id numbers that don't exist" do + FarMar::Product.find(10_000).must_equal(nil) + end + + it "checks the values of each variable for product219" do + product219[0].must_equal(219) + product219[1].must_equal("Strange Pretzel") + product219[2].must_equal(69) + end + end + + describe "Product#all" do + let (:product) {FarMar::Product.all} + + it "Product should be instance of array" do + product.must_be_instance_of(Array) + end + + it "Product should have 8193 entries" do + product.count.must_equal(8193) + end + + it "should return array full of instances" do + classes = product.map{|m| m.class} + classes.uniq.must_equal([FarMar::Product]) + end + end end \ No newline at end of file diff --git a/specs/sales_spec.rb b/specs/sales_spec.rb index 6d870f4b..a6c6763c 100644 --- a/specs/sales_spec.rb +++ b/specs/sales_spec.rb @@ -6,4 +6,41 @@ it "it is an object we have acccess to" do FarMar::Sale.wont_be_nil end + + describe "Sale#find" do + let (:sale311) {FarMar::Sale.find(311)} + + it "checks that sale311 is instance of an Array" do + sale311.must_be_instance_of(Array) + end + + it "returns nil for id numbers that don't exist" do + FarMar::Sale.find(1000_000).must_equal(nil) + end + + it "checks the values of each variable for sale311" do + sale311[0].must_equal(311) + sale311[1].must_equal(3520) + #sale311[2].must_equal("2013-11-08 20:36:34 -0800") + sale311[3].must_equal(69) + sale311[4].must_equal(219) + end + end + + describe "sale#all" do + let (:sale) {FarMar::Sale.all} + + it "sale should be instance of array" do + sale.must_be_instance_of(Array) + end + + it "sale should have 12798 entries" do + sale.count.must_equal(12798) + end + + it "should return array full of instances" do + classes = sale.map{|m| m.class} + classes.uniq.must_equal([FarMar::Sale]) + end + end end \ No newline at end of file diff --git a/specs/vendors_spec.rb b/specs/vendors_spec.rb index df64c910..35eca775 100644 --- a/specs/vendors_spec.rb +++ b/specs/vendors_spec.rb @@ -6,4 +6,40 @@ it "it is an object we have acccess to" do FarMar::Vendor.wont_be_nil end + + describe "Vendor#find" do + let (:vendor69) {FarMar::Vendor.find(69)} + + it "checks that Vendor69 is instance of an array" do + vendor69.must_be_instance_of(Array) + end + + it "returns nil for id numbers that don't exist" do + FarMar::Vendor.find(10_000).must_equal(nil) + end + + it "checks the values of each variable for Vendor69" do + vendor69[0].must_equal(69) + vendor69[1].must_equal("Emard-Streich") + vendor69[2].must_equal(5) + vendor69[3].must_equal(15) + end + end + + describe "Vendor#all" do + let (:vendor) {FarMar::Vendor.all} + + it "Vendor should be instance of array" do + vendor.must_be_instance_of(Array) + end + + it "Vendor should have 2690 entries" do + vendor.count.must_equal(2690) + end + + it "should return array full of instances" do + classes = vendor.map{|m| m.class} + classes.uniq.must_equal([FarMar::Vendor]) + end + end end \ No newline at end of file From c90f79930909866bb18d9d4ac8c6fad585ccd459 Mon Sep 17 00:00:00 2001 From: Nadine Date: Wed, 16 Mar 2016 10:44:21 -0700 Subject: [PATCH 05/11] Finished all vendor, product, sale, market methods and related tests. --- lib/markets.rb | 26 ++++++-------------------- lib/products.rb | 14 ++++++++++---- lib/sales.rb | 13 +++++++++---- lib/vendors.rb | 18 +++++++++++++----- specs/markets_spec.rb | 17 +++++++++++++++-- specs/products_spec.rb | 14 ++++++++++++++ specs/sales_spec.rb | 15 +++++++++++++++ specs/vendors_spec.rb | 16 ++++++++++++++++ 8 files changed, 98 insertions(+), 35 deletions(-) diff --git a/lib/markets.rb b/lib/markets.rb index aa695449..38f9e300 100644 --- a/lib/markets.rb +++ b/lib/markets.rb @@ -1,11 +1,9 @@ - class FarMar::Market - attr_accessor :ID, :name, :address, :city, :county, :state, :zip - + attr_accessor :id, :name, :address, :city, :county, :state, :zip MARKET_DATA = "./support/markets.csv" def initialize(market_hash) - @ID = market_hash[:ID].to_i + @id = market_hash[:id].to_i @name = market_hash[:name] @address = market_hash[:address] @city = market_hash[:city] @@ -19,7 +17,7 @@ def initialize(market_hash) def self.all all = [] CSV.foreach(MARKET_DATA, "r") do |line| - market = FarMar::Market.new(ID: line[0].to_i, name: line[1], + market = FarMar::Market.new(id: line[0].to_i, name: line[1], address: line[2], city: line[3], county: line[4], zip: line[6]) all << market @@ -46,20 +44,8 @@ def self.find(id) #returns a collection of FarMar::Vendor instances that #are associated with the market by the market_id field. def vendor - vendors = [] - vendors << FarMar::Vendor.all.select{|key,value| key == "market_id" && value == @id} - + FarMar::Vendor.all.find_all do |vendor| + vendor.market_id == @id + end end - - - - - - - - - - - - end \ No newline at end of file diff --git a/lib/products.rb b/lib/products.rb index 953b27b2..16571b46 100644 --- a/lib/products.rb +++ b/lib/products.rb @@ -1,7 +1,9 @@ class FarMar::Product + attr_accessor :id, :name, :vendor_id PRODUCT_DATA = "./support/products.csv" + def initialize(product_hash) - @ID = product_hash[:ID].to_i + @id = product_hash[:id].to_i @name = product_hash[:name] @vendor_id = product_hash[:vendor_id].to_i @@ -11,7 +13,7 @@ def initialize(product_hash) def self.all all = [] CSV.foreach(PRODUCT_DATA, "r") do |line| - product = FarMar::Product.new(ID: line[0].to_i, name: line[1], + product = FarMar::Product.new(id: line[0].to_i, name: line[1], vendor_id: line[2].to_i) all << product end @@ -33,13 +35,17 @@ def self.find(id) end def vendor - + FarMar::Vendor.all.find do |vendor| + vendor.id == @vendor_id + end end #returns a collection of FarMar::Sale instances that are #associated using the FarMar::Sale product_id field. def sales - + FarMar::Sale.all.find_all do |sale| + sale.product_id == @id + end end #returns the number of times this product has been sold. diff --git a/lib/sales.rb b/lib/sales.rb index ea26e853..acc18457 100644 --- a/lib/sales.rb +++ b/lib/sales.rb @@ -1,8 +1,9 @@ class FarMar::Sale + attr_accessor :id, :amount, :purchase_time, :vendor_id, :product_id SALE_DATA = "./support/sales.csv" def initialize(sale_hash) - @ID = sale_hash[:ID].to_i + @id = sale_hash[:id].to_i @amount = sale_hash[:amount].to_i @purchase_time = sale_hash[:purchase_time] @vendor_id = sale_hash[:vendor_id].to_i @@ -15,7 +16,7 @@ def initialize(sale_hash) def self.all all = [] CSV.foreach(SALE_DATA, "r") do |line| - sale = FarMar::Sale.new(ID: line[0].to_i, + sale = FarMar::Sale.new(id: line[0].to_i, amount: line[1].to_i, purchase_time: line[2], vendor_id: line[3].to_i, product_id: line[4].to_i) all << sale @@ -41,13 +42,17 @@ def self.find(id) #returns the FarMar::Vendor instance that is associated # with this sale using the FarMar::Sale vendor_id field def vendor - + FarMar::Vendor.all.find do |vendor| + vendor.id == @vendor_id + end end #returns the FarMar::Product instance that is associated #with this sale using the FarMar::Sale product_id field def product - + FarMar::Product.all.find do |product| + product.id == @product_id + end end #returns a collection of FarMar::Sale objects where the diff --git a/lib/vendors.rb b/lib/vendors.rb index 4997fd53..575df8e8 100644 --- a/lib/vendors.rb +++ b/lib/vendors.rb @@ -1,8 +1,10 @@ class FarMar::Vendor + attr_accessor :id, :name, :no_of_employees, :market_id + VENDOR_DATA = "./support/vendors.csv" def initialize(vendor_hash) - @ID = vendor_hash[:ID].to_i + @id = vendor_hash[:id].to_i @name = vendor_hash[:name] @no_of_employees = vendor_hash[:no_of_employees].to_i @market_id = vendor_hash[:market_id].to_i @@ -13,7 +15,7 @@ def initialize(vendor_hash) def self.all all = [] CSV.foreach(VENDOR_DATA, "r") do |line| - vendor = FarMar::Vendor.new(ID: line[0].to_i, name: line[1], + vendor = FarMar::Vendor.new(id: line[0].to_i, name: line[1], no_of_employees: line[2].to_i, market_id: line[3].to_i) all << vendor end @@ -39,19 +41,25 @@ def self.find(id) #returns the FarMar::Market instance that is associated #with this vendor using the FarMar::Vendor market_id field def market - + FarMar::Market.all.find do |market| + market.id == @market_id + end end #returns a collection of FarMar::Product instances that #are associated by the FarMar::Product vendor_id field. def products - + FarMar::Product.all.find_all do |product| + product.vendor_id == @id + end end #returns a collection of FarMar::Sale instances that are #associated by the vendor_id field. def sales - + FarMar::Sale.all.find_all do |sale| + sale.vendor_id == @id + end end #returns the the sum of all of the vendor's sales(in cents) diff --git a/specs/markets_spec.rb b/specs/markets_spec.rb index ec396180..76bb6dd0 100644 --- a/specs/markets_spec.rb +++ b/specs/markets_spec.rb @@ -3,13 +3,13 @@ describe FarMar::Market do - + let (:market15) {FarMar::Market.find(15)} it "it is an object we have acccess to" do FarMar::Market.wont_be_nil end describe "Market#find" do - let (:market15) {FarMar::Market.find(15)} + it "checks that market15 is instance of an array" do market15.must_be_instance_of(Array) @@ -46,4 +46,17 @@ classes.uniq.must_equal([FarMar::Market]) end end + + describe "Market#vendor" do + market13 = FarMar::Market.new(id: 13, name: "Otsiningo Park Farmers' Market", + address: "1 Bevier St", city:"Binghamton",county: "Broome", + state: "New York", zip: "13905") + it "should return an array of vendors" do + market13.vendor.must_be_instance_of(Array) + end + + it "should return vendor count for a market" do + market13.vendor.count.must_equal(7) + end + end end \ No newline at end of file diff --git a/specs/products_spec.rb b/specs/products_spec.rb index 4980c697..a4b3304c 100644 --- a/specs/products_spec.rb +++ b/specs/products_spec.rb @@ -41,4 +41,18 @@ classes.uniq.must_equal([FarMar::Product]) end end + + describe "Product#vendor,sales" do + product171 = FarMar::Product.new(id: 171, + name: "Depressed Carrots", vendor_id: 54) + it "should return an array of vendors" do + product171.vendor.must_be_instance_of(FarMar::Vendor) + product171.sales.must_be_instance_of(Array) + end + + it "should return vendor count for a market" do + product171.sales.count.must_equal(3) + product171.vendor.id.must_equal(54) + end + end end \ No newline at end of file diff --git a/specs/sales_spec.rb b/specs/sales_spec.rb index a6c6763c..5bf53db5 100644 --- a/specs/sales_spec.rb +++ b/specs/sales_spec.rb @@ -43,4 +43,19 @@ classes.uniq.must_equal([FarMar::Sale]) end end + + describe "Sale#vendor,product" do + sale253 = FarMar::Sale.new(id: 253, amount: 3924, + purchase_time: "2013-11-10 17:44:52 -0800", + vendor_id: 54, product_id: 171) + it "should return an array of vendors" do + sale253.vendor.must_be_instance_of(FarMar::Vendor) + sale253.product.must_be_instance_of(FarMar::Product) + end + + it "should return a value from method" do + sale253.vendor.id.must_equal(54) + sale253.product.id.must_equal(171) + end + end end \ No newline at end of file diff --git a/specs/vendors_spec.rb b/specs/vendors_spec.rb index 35eca775..e40e89b2 100644 --- a/specs/vendors_spec.rb +++ b/specs/vendors_spec.rb @@ -42,4 +42,20 @@ classes.uniq.must_equal([FarMar::Vendor]) end end + + describe "Vendor#market,product,sales" do + vendor54 = FarMar::Vendor.new(id: 54, name: "Bayer Inc", + no_of_employees: 3, market_id:13) + it "should return the instance type of the methods" do + vendor54.market.must_be_instance_of(FarMar::Market) + vendor54.products.must_be_instance_of(Array) + vendor54.sales.must_be_instance_of(Array) + end + + it "should check values from vendor methods" do + vendor54.products.count.must_equal(5) + vendor54.sales.count.must_equal(8) + vendor54.market.id.must_equal(13) + end + end end \ No newline at end of file From e4b05936bed9b2c092a14c6837543a6a803304c3 Mon Sep 17 00:00:00 2001 From: Nadine Date: Wed, 16 Mar 2016 11:31:48 -0700 Subject: [PATCH 06/11] Added revenue and self.by_market methods for vendors file with accompanying tests. --- lib/vendors.rb | 21 ++++++++++++++++----- specs/vendors_spec.rb | 23 ++++++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/vendors.rb b/lib/vendors.rb index 575df8e8..f31e124e 100644 --- a/lib/vendors.rb +++ b/lib/vendors.rb @@ -64,13 +64,24 @@ def sales #returns the the sum of all of the vendor's sales(in cents) def revenue - + sum = 0 + FarMar::Sale.all.each do |sale| + if sale.vendor_id == @id + sum += sale.amount + end + end + return sum end #returns all of the vendors with the given market_id def self.by_market(market_id) - - end - - + vendors = [] + CSV.foreach(VENDOR_DATA, "r") do |line| + if line[3].to_i == market_id + vendors << FarMar::Vendor.new(id: line[0].to_i, name: line[1], + no_of_employees: line[2].to_i, market_id: line[3].to_i) + end + end + return vendors + end end \ No newline at end of file diff --git a/specs/vendors_spec.rb b/specs/vendors_spec.rb index e40e89b2..47f9d90c 100644 --- a/specs/vendors_spec.rb +++ b/specs/vendors_spec.rb @@ -3,6 +3,9 @@ describe FarMar::Vendor do + vendor54 = FarMar::Vendor.new(id: 54, name: "Bayer Inc", + no_of_employees: 3, market_id:13) + it "it is an object we have acccess to" do FarMar::Vendor.wont_be_nil end @@ -37,25 +40,35 @@ vendor.count.must_equal(2690) end - it "should return array full of instances" do + it "should return the class" do classes = vendor.map{|m| m.class} classes.uniq.must_equal([FarMar::Vendor]) end end - describe "Vendor#market,product,sales" do - vendor54 = FarMar::Vendor.new(id: 54, name: "Bayer Inc", - no_of_employees: 3, market_id:13) + describe "Vendor#market,product,sales" do it "should return the instance type of the methods" do vendor54.market.must_be_instance_of(FarMar::Market) vendor54.products.must_be_instance_of(Array) vendor54.sales.must_be_instance_of(Array) end - it "should check values from vendor methods" do + it "should return correct values from vendor methods" do vendor54.products.count.must_equal(5) vendor54.sales.count.must_equal(8) vendor54.market.id.must_equal(13) end end + + describe "Vendor#revenue" do + it "should return the revenue from a vendor" do + vendor54.revenue.must_equal(39552) + end + end + + describe "Vendor#by_market(market_id)" do + it "should return count of vendors for a market" do + FarMar::Vendor.by_market(2).count.must_equal(3) + end + end end \ No newline at end of file From 6659bd48cc6e385c99d1183c89c98d06c3bb2428 Mon Sep 17 00:00:00 2001 From: Nadine Date: Wed, 16 Mar 2016 16:44:32 -0700 Subject: [PATCH 07/11] Finished and tested all methods but self.between --- lib/products.rb | 17 +++++++++++++++-- lib/sales.rb | 16 +++++++++++++--- specs/products_spec.rb | 17 +++++++++++++++-- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/products.rb b/lib/products.rb index 16571b46..ecec69df 100644 --- a/lib/products.rb +++ b/lib/products.rb @@ -50,12 +50,25 @@ def sales #returns the number of times this product has been sold. def number_of_sales - + sum = 0 + FarMar::Sale.all.each do |sale| + if sale.product_id == @id + sum += 1 + end + end + return sum end #returns all of the products with the given vendor_id def self.by_vendor(vendor_id) - + products = [] + CSV.foreach(PRODUCT_DATA, "r") do |line| + if line[2].to_i == vendor_id + products << FarMar::Vendor.new(id: line[0].to_i, name: line[1], + vendor_id: line[2].to_i) + end + end + return products end diff --git a/lib/sales.rb b/lib/sales.rb index acc18457..520375d2 100644 --- a/lib/sales.rb +++ b/lib/sales.rb @@ -17,7 +17,7 @@ def self.all all = [] CSV.foreach(SALE_DATA, "r") do |line| sale = FarMar::Sale.new(id: line[0].to_i, - amount: line[1].to_i, purchase_time: line[2], + amount: line[1].to_i, purchase_time: DateTime.parse(line[2]), vendor_id: line[3].to_i, product_id: line[4].to_i) all << sale end @@ -57,8 +57,18 @@ def product #returns a collection of FarMar::Sale objects where the #purchase time is between the two times given as arguments - def self.between(beginning_time, end_time) - + def self.between((beginning_time, end_time) + sales = [] + beginning_time = DateTime.parse(beginning_time) + end_time = DateTime.parse(end_time) + FarMar::Sale.all.find_all do |sale| + if (sale.purchase_time >= beginning_time) && + (sale.purchase_time <= end_time) + sales << sale + end + end + return sales end + end \ No newline at end of file diff --git a/specs/products_spec.rb b/specs/products_spec.rb index a4b3304c..17781810 100644 --- a/specs/products_spec.rb +++ b/specs/products_spec.rb @@ -3,6 +3,9 @@ describe FarMar::Product do + product171 = FarMar::Product.new(id: 171, + name: "Depressed Carrots", vendor_id: 54) + it "it is an object we have acccess to" do FarMar::Product.wont_be_nil end @@ -43,8 +46,6 @@ end describe "Product#vendor,sales" do - product171 = FarMar::Product.new(id: 171, - name: "Depressed Carrots", vendor_id: 54) it "should return an array of vendors" do product171.vendor.must_be_instance_of(FarMar::Vendor) product171.sales.must_be_instance_of(Array) @@ -55,4 +56,16 @@ product171.vendor.id.must_equal(54) end end + + describe "Product#number_of_sales" do + it "should return the # of sales for a product" do + product171.number_of_sales.must_equal(3) + end + end + + describe "Product#by_vendor(vendor_id)" do + it "should return count of products for a vendor" do + FarMar::Product.by_vendor(5).count.must_equal(3) + end + end end \ No newline at end of file From 3aa0b5fb95cc95342ff78b1034dc91b6adee0a9a Mon Sep 17 00:00:00 2001 From: Nadine Date: Thu, 17 Mar 2016 13:42:51 -0700 Subject: [PATCH 08/11] Finished primary requirements for FarMar --- lib/markets.rb | 1 - lib/products.rb | 1 - lib/sales.rb | 17 +++++++---------- lib/vendors.rb | 1 - specs/sales_spec.rb | 6 ++++++ 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/markets.rb b/lib/markets.rb index 38f9e300..6d53b571 100644 --- a/lib/markets.rb +++ b/lib/markets.rb @@ -35,7 +35,6 @@ def self.find(id) find =[line[0].to_i, line[1], line[2], line[3], line[4], line[5], line[6]] return find - break end end return nil diff --git a/lib/products.rb b/lib/products.rb index ecec69df..91999f6b 100644 --- a/lib/products.rb +++ b/lib/products.rb @@ -28,7 +28,6 @@ def self.find(id) if line[0] == id.to_s find =[line[0].to_i, line[1], line[2].to_i] return find - break end end return nil diff --git a/lib/sales.rb b/lib/sales.rb index 520375d2..3e3c0ecb 100644 --- a/lib/sales.rb +++ b/lib/sales.rb @@ -33,7 +33,6 @@ def self.find(id) find =[line[0].to_i, line[1].to_i, line[2].to_i, line[3].to_i, line[4].to_i] return find - break end end return nil @@ -57,18 +56,16 @@ def product #returns a collection of FarMar::Sale objects where the #purchase time is between the two times given as arguments - def self.between((beginning_time, end_time) + def self.between(beginning_time, end_time) sales = [] beginning_time = DateTime.parse(beginning_time) end_time = DateTime.parse(end_time) - FarMar::Sale.all.find_all do |sale| - if (sale.purchase_time >= beginning_time) && - (sale.purchase_time <= end_time) - sales << sale - end + FarMar::Sale.all.find_all do |sale| + if (sale.purchase_time >= beginning_time) && + (sale.purchase_time <= end_time) + sales << sale end - return sales + end + return sales end - - end \ No newline at end of file diff --git a/lib/vendors.rb b/lib/vendors.rb index f31e124e..3eb6ccf3 100644 --- a/lib/vendors.rb +++ b/lib/vendors.rb @@ -31,7 +31,6 @@ def self.find(id) find =[line[0].to_i, line[1], line[2].to_i, line[3].to_i] return find - break end end return nil diff --git a/specs/sales_spec.rb b/specs/sales_spec.rb index 5bf53db5..2068d399 100644 --- a/specs/sales_spec.rb +++ b/specs/sales_spec.rb @@ -58,4 +58,10 @@ sale253.product.id.must_equal(171) end end + + describe "Sale#between" do + it "should return an array" do + FarMar::Sale.between("2013-11-06 14:19:49 -0800","2013-11-07 04:34:56 -0800").must_be_instance_of(Array) + end + end end \ No newline at end of file From 1715f3c9aa76152ad66a3198433d468adf019efe Mon Sep 17 00:00:00 2001 From: Nadine Date: Fri, 18 Mar 2016 11:04:36 -0700 Subject: [PATCH 09/11] Finished optional 1 methods self.search(search_term) and products --- lib/markets.rb | 37 ++++++++++++++++++++++++++++++++++++- lib/products.rb | 2 +- specs/markets_spec.rb | 37 +++++++++++++++++++++++++++++++------ specs/sales_spec.rb | 3 ++- 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/lib/markets.rb b/lib/markets.rb index 6d53b571..128278fc 100644 --- a/lib/markets.rb +++ b/lib/markets.rb @@ -42,9 +42,44 @@ def self.find(id) #returns a collection of FarMar::Vendor instances that #are associated with the market by the market_id field. - def vendor + def vendors FarMar::Vendor.all.find_all do |vendor| vendor.market_id == @id end end + + #returns a collection of FarMar::Product instances that + #are associated to the market through the FarMar::Vendor class + def products + products = [] + FarMar::Vendor.all.each do |vendor| + if vendor.market_id == @id + find_product = FarMar::Product.by_vendor(vendor.id) + find_product.each do |product| + products << product + end + end + end + return products + end + + #returns a collection of FarMar::Market instances where the + #market name or vendor name contain the search_term + def self.search(search_term) + markets = [] + FarMar::Vendor.all.each do |vendor| + if search_term == vendor.name + search_term = vendor.market_id + end + end + + FarMar::Market.all.each do |market| + if market.id == search_term + markets << market + elsif market.name == search_term + markets << market + end + end + return markets + end end \ No newline at end of file diff --git a/lib/products.rb b/lib/products.rb index 91999f6b..68e54390 100644 --- a/lib/products.rb +++ b/lib/products.rb @@ -63,7 +63,7 @@ def self.by_vendor(vendor_id) products = [] CSV.foreach(PRODUCT_DATA, "r") do |line| if line[2].to_i == vendor_id - products << FarMar::Vendor.new(id: line[0].to_i, name: line[1], + products << FarMar::Product.new(id: line[0].to_i, name: line[1], vendor_id: line[2].to_i) end end diff --git a/specs/markets_spec.rb b/specs/markets_spec.rb index 76bb6dd0..13fdb174 100644 --- a/specs/markets_spec.rb +++ b/specs/markets_spec.rb @@ -3,7 +3,14 @@ describe FarMar::Market do + market1 = FarMar::Market.new(id: 1, name: "People's Co-op Farmers Market", address: "30th and Burnside", + city: "Portland", county: "Multnomah", state: "Oregon",zip: "97202") + market13 = FarMar::Market.new(id: 13, name: "Otsiningo Park Farmers' Market", + address: "1 Bevier St", city:"Binghamton",county: "Broome", + state: "New York", zip: "13905") + let (:market15) {FarMar::Market.find(15)} + it "it is an object we have acccess to" do FarMar::Market.wont_be_nil end @@ -47,16 +54,34 @@ end end - describe "Market#vendor" do - market13 = FarMar::Market.new(id: 13, name: "Otsiningo Park Farmers' Market", - address: "1 Bevier St", city:"Binghamton",county: "Broome", - state: "New York", zip: "13905") + describe "Market#vendors" do it "should return an array of vendors" do - market13.vendor.must_be_instance_of(Array) + market13.vendors.must_be_instance_of(Array) end it "should return vendor count for a market" do - market13.vendor.count.must_equal(7) + market13.vendors.count.must_equal(7) + end + end + + describe "Market#products" do + it "should return an array" do + market1.products.must_be_instance_of(Array) + end + + it "should return the correct size of the array" do + market1.products.count.must_equal(13) + end + end + + describe "Market#search(search_term)" do + it "should return an array" do + FarMar::Market.search("Silverdale Farmers Market").must_be_instance_of(Array) + end + + it "should return correct size of array" do + FarMar::Market.search("Stracke Group").count.must_equal(1) + FarMar::Market.search("Silverdale Farmers Market").count.must_equal(1) end end end \ No newline at end of file diff --git a/specs/sales_spec.rb b/specs/sales_spec.rb index 2068d399..d26143f4 100644 --- a/specs/sales_spec.rb +++ b/specs/sales_spec.rb @@ -60,8 +60,9 @@ end describe "Sale#between" do + test_sale = FarMar::Sale.between("2013-11-06 14:19:49 -0800","2013-11-07 04:34:56 -0800") it "should return an array" do - FarMar::Sale.between("2013-11-06 14:19:49 -0800","2013-11-07 04:34:56 -0800").must_be_instance_of(Array) + test_sale.must_be_instance_of(Array) end end end \ No newline at end of file From a693a09199d9ed9f90e500d7b289e130625d082c Mon Sep 17 00:00:00 2001 From: Nadine Date: Fri, 18 Mar 2016 11:27:03 -0700 Subject: [PATCH 10/11] Fixed self.search(search_term) method --- lib/markets.rb | 28 +++++++++++++++++----------- specs/markets_spec.rb | 4 ++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/markets.rb b/lib/markets.rb index 128278fc..794803cc 100644 --- a/lib/markets.rb +++ b/lib/markets.rb @@ -66,20 +66,26 @@ def products #returns a collection of FarMar::Market instances where the #market name or vendor name contain the search_term def self.search(search_term) - markets = [] - FarMar::Vendor.all.each do |vendor| - if search_term == vendor.name - search_term = vendor.market_id + market_collection = [] + markets = FarMar::Market.all + vendors = FarMar::Vendor.all + search_term = search_term.downcase + + vendors.each do |vendor| + if vendor.name.downcase.include? search_term + markets.each do |market| + if vendor.market_id == market.id + market_collection << market + end + end end end - FarMar::Market.all.each do |market| - if market.id == search_term - markets << market - elsif market.name == search_term - markets << market - end + markets.each do |market| + if market.name.downcase.include? search_term + market_collection << market + end end - return markets + return market_collection end end \ No newline at end of file diff --git a/specs/markets_spec.rb b/specs/markets_spec.rb index 13fdb174..cd04d1a5 100644 --- a/specs/markets_spec.rb +++ b/specs/markets_spec.rb @@ -80,8 +80,8 @@ end it "should return correct size of array" do - FarMar::Market.search("Stracke Group").count.must_equal(1) - FarMar::Market.search("Silverdale Farmers Market").count.must_equal(1) + FarMar::Market.search("Von-").count.must_equal(2) + FarMar::Market.search("Top").count.must_equal(4) end end end \ No newline at end of file From dae3815f760360ee098eae617b585722ec9ba9da Mon Sep 17 00:00:00 2001 From: Nadine Date: Fri, 18 Mar 2016 14:41:34 -0700 Subject: [PATCH 11/11] Added prefered_vendor method for market file. --- lib/markets.rb | 23 +++++++++++++++++++++++ lib/products.rb | 1 - specs/markets_spec.rb | 12 ++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/markets.rb b/lib/markets.rb index 794803cc..5eb3e78f 100644 --- a/lib/markets.rb +++ b/lib/markets.rb @@ -88,4 +88,27 @@ def self.search(search_term) end return market_collection end + + #returns the vendor with the highest revenue + def prefered_vendor + best_vendor = [] + sum = 0 + max_sum = 0 + FarMar::Vendor.all.each do |vendor| + if vendor.market_id == @id + sum = 0 + FarMar::Sale.all.each do |sale| + if sale.vendor_id == vendor.id + sum += sale.amount + end + end + + if sum > max_sum + max_sum = sum + best_vendor = vendor + end + end + end + return best_vendor + end end \ No newline at end of file diff --git a/lib/products.rb b/lib/products.rb index 68e54390..e280e5cd 100644 --- a/lib/products.rb +++ b/lib/products.rb @@ -70,5 +70,4 @@ def self.by_vendor(vendor_id) return products end - end \ No newline at end of file diff --git a/specs/markets_spec.rb b/specs/markets_spec.rb index cd04d1a5..86b81e80 100644 --- a/specs/markets_spec.rb +++ b/specs/markets_spec.rb @@ -82,6 +82,18 @@ it "should return correct size of array" do FarMar::Market.search("Von-").count.must_equal(2) FarMar::Market.search("Top").count.must_equal(4) + FarMar::Market.search("Inc").count.must_equal(283) end end + + describe "Market#prefered_vendor" do + it "should return an instance of FarMar::Vendor" do + market1.prefered_vendor.must_be_instance_of(FarMar::Vendor) + end + + it "should return the correct vendor" do + market1.prefered_vendor.id.must_equal(5) + end + end + end \ No newline at end of file