diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 5be343e9..937abcd1 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,4 +1,45 @@ +require 'csv' + module FarMar class Market + attr_accessor :id, :name, :address, :city, :county, :state, :zip + def initialize(id, name, address, city, county, state, zip) + @id = id.to_i + @name = name + @address = address + @city = city + @county = county + @state = state + @zip = zip + end + + def self.all + #returns a collection of Market instances, representing all of the markets described in the CSV + market_array = [] + read_file = CSV.read("./support/markets.csv") + read_file.each do |market| + list_of_markets = FarMar::Market.new(market[0], market[1], market[2], market[3], market[4], market[5], market[6]) + market_array.push(list_of_markets) + end + return market_array + end + + def self.find(id) + #returns an instance of Market where the value in the id field of the CSV file matches the passed parameter + self.all.find {|i| i.id == id} + end + + def vendors + #returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field + vendor_array = [] + vendor_list = FarMar::Vendor.all + vendor_list.find_all do |i| + if i.market_id == id + vendor_array.push(i) + end + end + return vendor_array + end + end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 67d6716d..fd49072e 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,4 +1,71 @@ module FarMar class Product + attr_accessor :id, :name, :vendor_id + def initialize(id, name, vendor_id) + @id = id.to_i + @name = name + @vendor_id = vendor_id.to_i + end + + def self.all + #returns a collection of product instances, representing all of the products described in the CSV + product_array = [] + product_file = CSV.read("./support/products.csv") + product_file.each do |product| + list_of_products = FarMar::Product.new(product[0], product[1], product[2]) + product_array.push(list_of_products) + end + return product_array + end + + def self.find(id) + #returns an instance of Product where the value in the id field of the CSV file matches the passed parameter + all.find {|i| i.id == id} + end + + def vendor + # returns the FarMar::Market instance that is associated with this vendor + # using the FarMar::Vendor market_id field + # vendor_array = [] + # vendor_list = FarMar::Product.all + # vendor_list.find_all do |i| + # if i.id == vendor_id + # vendor_array.push(i) + # end + # end + # return vendor_array + + vendor_list = FarMar::Vendor.all + vendor_list.find do |vendor| + vendor.id == vendor_id + end + end + + def sales + #sales - returns a collection + #of FarMar::Sale instances that are associated + #using the FarMar::Sale product_id field. + sales_array = [] + sales_list = FarMar::Sale.all + sales_list.find_all do |i| + if i.product_id == id + sales_array.push(i) + end + end + return sales_array + end + + def self.by_vendor(vendor_id) + #returns all of the products with the given vendor_id + products_sold_by_a_vendor_array = [] + product_list = FarMar::Product.all + product_list.find_all do |product| + if product.vendor_id == vendor_id + products_sold_by_a_vendor_array.push(product) + end + end + return products_sold_by_a_vendor_array + end + end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 10e3780e..b8bbc666 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,4 +1,67 @@ module FarMar class Sale + attr_accessor :id, :amount, :purchase_time, :vendor_id, :product_id + def initialize(id, amount, purchase_time, vendor_id, product_id) + @id = id.to_i + @amount = amount.to_i + @purchase_time = purchase_time.to_s + @vendor_id = vendor_id.to_i + @product_id = product_id.to_i + end + + def self.all + #returns a collection of sale instances, representing all of the sales described in the CSV + sale_array = [] + CSV.read("./support/sales.csv").each do |sale| + list_of_sales = Sale.new(sale[0], sale[1], sale[2], sale[3], sale[4]) + sale_array.push(list_of_sales) + end + return sale_array + end + + def self.find(id) + #self.find(id) - returns an instance of Sale where the value of the id field + #in the CSV matches the passed parameter. + all.find {|i| i.id == id} + end + + def vendor + # returns the FarMar::Market instance that is associated with this vendor + # using the FarMar::Vendor market_id field + vendor_list = FarMar::Vendor.all + vendor_list.find do |vendor| + vendor.id == vendor_id + end + end + + def product + #product - returns the FarMar::Product + # instance that is associated with this sale using the FarMar::Sale product_id field + product_list = FarMar::Product.all + product_list.find do |product| + product.id == product_id + end + end + + + def self.between(beginning_time, end_time) + #returns a collection of FarMar::Sale + #objects where the purchase time is between the two times given as arguments + sales_between_times_array = [] + list_of_sales = FarMar::Sale.all + list_of_sales.find_all do |sale| + if (beginning_time < sale.purchase_time) && (sale.purchase_time < end_time) + sales_between_times_array.push(sale) + end + end + return sales_between_times_array + end +# @begin = DateTime.strptime("2013-11-06 08:35:40 -0800", "%Y-%m-%d %H:%M:%S %z") +# @end = DateTime.strptime("2013-11-13 08:35:16 -0800", "%Y-%m-%d +# +# list_of_sales.each do |sale_object| +# sale_object.purchase_time > beginning_time && sale_object.purchase_time < end_time + + end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 43c213b6..b0934a3c 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,4 +1,81 @@ module FarMar class Vendor + attr_accessor :id, :name, :no_of_employees, :market_id + def initialize(id, name, no_of_employees, market_id) + @id = id.to_i + @name = name + @no_of_employees = no_of_employees + @market_id = market_id.to_i + end + + def self.all + #returns a collection of vendor instances, representing all of the vendors described in the CSV + vendor_array = [] + CSV.read("./support/vendors.csv").each do |vendor| + list_of_vendors = Vendor.new(vendor[0], vendor[1], vendor[2], vendor[3]) + vendor_array.push(list_of_vendors) + end + return vendor_array + end + + def self.find(id) + self.all.find {|i| i.id == id} + end + + def market + # returns the FarMar::Market instance that is associated with this vendor + # using the FarMar::Vendor market_id field + market_array = [] + market_list = FarMar::Market.all + market_list.find_all do |i| + if i.id == market_id + market_array.push(i) + end + end + return market_array + end + + def products + product_array = [] + product_list = FarMar::Product.all + product_list.find_all do |i| + if i.vendor_id == id + product_array.push(i) + end + end + return product_array + end + + def sales + sales_array = [] + sales_list = FarMar::Sale.all + sales_list.find_all do |i| + if i.vendor_id == id + sales_array.push(i) + end + end + return sales_array + end + + def revenue + total_amount = 0 + sales.each do |sale| + total_amount += sale.amount + end + return total_amount + end + + def self.by_market(market_id) + #returns all of the vendors with the given market_id + vendors_in_a_market_array = [] + vendor_list = FarMar::Vendor.all + vendor_list.find_all do |vendor| + if vendor.market_id == market_id + vendors_in_a_market_array.push(vendor) + end + end + return vendors_in_a_market_array + end + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 160df0eb..b8aa4e55 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 + @market = FarMar::Market.new("14", "Hartford Farmers Market", "1 Block North of Highway 60 on Rural Street","Hartford","Washington", "Wisconsin", "53027") + @market_2 = FarMar::Market.new("500", "Montefiore Medical Center Farmers Market_Thursday", "111 E. 210th Street", "Bronx", "Bronx", "New York","10467") end context "initializing" do @@ -10,4 +11,33 @@ expect(@market).to be_an_instance_of FarMar::Market end end + + context ".self.all" do + it "returns an array" do + expect(FarMar::Market.all().class).to be Array + end + it "returns instances of all lines in the csv" do + expect(FarMar::Market.all().length).to eq 500 + end + end + + context ".self.find(id)" do + it "returns the right instance" do + expect(FarMar::Market.find(14).name).to eq "Hartford Farmers Market" + end + + it "returns an instance with the same id as the value in the CSV file" do + expect(@market.id).to eq 14 + expect(@market_2.id).to eq 500 + end + end + + context "#vendors" do + it "returns a collection" do + expect(@market.vendors.class).to eq Array + end + it "is a collection of Vendor instances in Far::Mar" do + expect(@market.vendors.length).to eq 8 + end + end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 4e20d380..03963e8f 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,7 +2,8 @@ describe FarMar::Product do before :each do - @product = FarMar::Product.new + @product = FarMar::Product.new(12, "Gorgeous Fish", 6) + @product_2 = FarMar::Product.new(8193, "Cruel Beef", 2690) end context "initializing" do @@ -10,4 +11,46 @@ expect(@product).to be_an_instance_of FarMar::Product end end + + context ".self.all" do + it "returns an array" do + expect(FarMar::Product.all().class).to be Array + end + it "returns instances of all lines in the csv" do + expect(FarMar::Product.all().length).to eq 8193 + end + end + + context ".self.find(id)" do + it "returns an instance of Product" do + expect(FarMar::Product.find(12)).to be_an_instance_of FarMar::Product + end + + it "returns the right instance" do + expect(FarMar::Product.find(12).name).to eq "Gorgeous Fish" + end + it "returns an instance with the same id as the value in the CSV file" do + expect(@product.id).to eq 12 + expect(@product_2.id).to eq 8193 + end + end + + context "#vendor" do + it "returns an instance of Vendor" do + expect(@product.vendor).to be_an_instance_of FarMar::Vendor + end + end + + context "#sales" do + it "returns a collection of FarMar::Sale instances" do + expect(@product_2.sales.length).to eq 2 + end + end + + context ".self.by_vendor(vendor_id)" do + it "returns all of the products with a given vendor id" do + expect(FarMar::Product.by_vendor(6).length).to eq 3 + end + end + end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 90c45a92..32d97d24 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -2,7 +2,8 @@ describe FarMar::Sale do before :each do - @sale = FarMar::Sale.new + @sale = FarMar::Sale.new(14, 4978, "2013-11-10 01:51:24 -0800", 3, 4) + @sale_2 = FarMar::Sale.new(12001, 8923.0, "2013-11-12 02:03:31 -0800", 2690, 8192) end context "initializing" do @@ -10,4 +11,52 @@ expect(@sale).to be_an_instance_of FarMar::Sale end end + + context ".self.all" do + it "returns an array" do + expect(FarMar::Sale.all().class).to be Array + end + it "returns instances of all lines in the csv" do + expect(FarMar::Sale.all().length).to eq 12798 + end + end + + context ".self.find(id)" do + it "returns the right instance" do + expect(FarMar::Sale.find(14).amount).to eq 4978 + end + + it "returns an instance with the same id as the value in the CSV file" do + expect(@sale.id).to eq 14 + expect(@sale_2.id).to eq 12001 + end + end + + context "#product" do + it "returns an instance of product" do + expect(@sale.product).to be_an_instance_of FarMar::Product + end + + it "returns the right product" do + expect(@sale.product.id).to eq 4 + expect(@sale_2.product.id).to eq 8192 + end + end + + context "#vendor" do + it "returns an instance of a vendor" do + expect(@sale.vendor).to be_an_instance_of FarMar::Vendor + end + end + + context ".self.between(beginning_time, end_time)" do + it "returns an array of sales" do + expect(FarMar::Sale.between("2013-11-07 20:25:38 -0800", "2013-11-08 10:38:56 -0800").class).to eq Array + end + + it "returns the expected number of sales" do + expect(FarMar::Sale.between("2013-11-07 20:25:38 -0800", "2013-11-08 10:38:56 -0800").length).to eq 1090 + end + end + end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 804d90ab..a6c85910 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,12 +2,66 @@ describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new + @vendor = FarMar::Vendor.new(45, "Hyatt, Conroy and Ortiz", 5, 10) + @vendor_2 = FarMar::Vendor.new(2690, "Mann-Lueilwitz", 4, 500) end - context "initializing" do - it "returns a vendor object" do + context "#initializing" do + it "creates an instance of @vendor" do expect(@vendor).to be_an_instance_of FarMar::Vendor + expect(@vendor_2).to be_an_instance_of FarMar::Vendor end end + + context ".self.all" do + it "returns an array" do + expect(FarMar::Vendor.all().class).to be Array + end + it "returns instances of all lines in the csv" do + expect(FarMar::Vendor.all().length).to eq 2690 + end + end + + context ".self.find(id)" do + it "returns the right instance" do + expect(FarMar::Vendor.find(45).name).to eq "Hyatt, Conroy and Ortiz" + end + + it "returns an instance with the same id as the value in the CSV file" do + expect(@vendor.id).to eq 45 + expect(@vendor_2.id).to eq 2690 + end + end + + context "#market" do + it "returns an instance of FarMar::Market associated with the vendor" do + expect(@vendor.market.length).to eq 1 + expect(@vendor_2.market.length).to eq 1 + end + end + + context "#products" do + it "returns a collection of FarMar::Product product instances" do + expect(@vendor.products.class).to be Array + end + end + + context "#sales" do + it "returns a collection of FarMar::Sale sale instances" do + expect(@vendor.sales.class).to be Array + end + end + + context "#revenue" do + it "returns a sum of all the vendors sales" do + expect(@vendor.revenue).to eq 10150 + end + end + + context ".self.by_market(market_id)" do + it "returns all the vendors with the given market_id" do + expect(FarMar::Vendor.by_market(10).length).to eq 9 + end + end + end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f7dc6dfb..14061ed2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,3 +2,19 @@ SimpleCov.start require './lib/far_mar' + +# RSpec.configure do |config| +# config.after(:suite) do +# num_failed = RSpec.world +# .filtered_examples +# .values +# .flatten +# .count(&:exception) +# ​ +# if num_failed == 0 +# `say "You did it Amy! You're amazing!"` +# else +# `say "Keep going! Only #{num_failed} #{num_failed > 1 ? "tests" : "test"} to go!"` +# end +# end +# end