diff --git a/lib/far_mar.rb b/lib/far_mar.rb index db54a95a..659b93f4 100644 --- a/lib/far_mar.rb +++ b/lib/far_mar.rb @@ -1,3 +1,4 @@ +require 'pry' require 'csv' require 'time' require './lib/far_mar/market' diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 5be343e9..89ff4892 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,4 +1,84 @@ + 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 + @@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 + end + def self.find(id) + self.all.find do |market| + market.market_id == id + end + end + def vendors + 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 + def products + market_products = [] + vendor_array = self.vendors + vendor_array.each do |vendor| + market_products += vendor.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(date = nil) + market_vendors = self.vendors + top_vendor = market_vendors.max_by do |vendor| + vendor.revenue(date) + 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/lib/far_mar/product.rb b/lib/far_mar/product.rb index 67d6716d..d3a2692a 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,4 +1,59 @@ 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 + @@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 + end + def self.find(id) + self.all.find do |product| + product.product_id == 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 + + 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 + + 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/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 10e3780e..d26c7ee2 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,4 +1,57 @@ module FarMar class Sale + + attr_reader :sale_id, :amount, :purchase_time, :vendor_id, :product_id + + def initialize(sale_id, amount, purchase_time, vendor_id, product_id) + @sale_id = sale_id + @amount = amount + @purchase_time = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") + @vendor_id = vendor_id + @product_id = product_id + end + + + def self.all + @@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 + end + def self.find(id) + self.all.find do |sale| + sale.sale_id == 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 + + 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/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 43c213b6..09dab969 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,4 +1,76 @@ 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_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 + end + def self.find(id) + self.all.find do |vendor| + 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 + + 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 + + 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(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 + + def self.by_market(market_id) + market = FarMar::Market.find(market_id) + return market.vendors + end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 092c46ca..26213cc0 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -2,11 +2,88 @@ describe FarMar::Market do before :each do - @market = FarMar::Market.new + @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 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 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 + end + end + + describe "vendors" 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 + + 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(@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 + + 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 diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index cf4bbf1e..6edc9be9 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,11 +2,57 @@ 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 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 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 + + 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 + + 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 + + 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 diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 38d647a2..5fa063db 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -2,11 +2,56 @@ describe FarMar::Sale do before :each do - @sale = FarMar::Sale.new + @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 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 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 + 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 + + 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") + @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(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 diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index a1987411..6f88a23d 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,11 +2,65 @@ 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 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 + 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 + + describe "products" 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 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 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