diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 7469f1e0..79555503 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,5 +1,46 @@ +require "pry" +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 + market_array = [] + farmar_data = CSV.read("./support/markets.csv") + farmar_data.each do |csv| + @m = Market.new(csv[0], csv[1], csv[2], csv[3], csv[4], csv[5], csv[6]) + market_array.push(@m) + end + return market_array + end + + def self.find(id) + farmar_data = Market.all + farmar_data.find do |data| + if data.id == id + return data + end + end + end + + def get_vendor + FarMar::Vendor.all.find_all do |vendor| + vendor.market_id == @id + end + end end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 8efc48d2..b07bfa02 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,5 +1,63 @@ +require "pry" +require "csv" + 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 + product_array =[] + product_data = CSV.read("./support/products.csv") + product_data.each do |csv| + @p = Product.new(csv[0], csv[1],csv[2]) + product_array.push(@p) + end + return product_array + end + + def self.find(id) + product_data = Product.all + product_data.find do |data| + if data.id == id + return data + end + end + end + + def get_vendor + FarMar::Vendor.all.find_all do |vendor| + vendor.id == @vendor_id + end + end + + def get_sales + FarMar::Sale.all.find_all do |sale| + sale.vendor_id == @id + end + end + + def number_of_sales + total = 0 + FarMar::Sale.all.each do |each_sale| + if each_sale.product_id == id + total = total + 1 + end + end + return total + end + + def self.by_vendor(vendor_id) + self.all.find_all do |product| + product.vendor_id == vendor_id + end + + end end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 10e3780e..b80b0ebf 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,4 +1,57 @@ +require "pry" +require "csv" 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 = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") + @vendor_id = vendor_id.to_i + @product_id = product_id.to_i + end + + def self.all + sale_array = [] + sale_data = CSV.read("./support/sales.csv") + sale_data.each do |csv| + @s = Sale.new(csv[0], csv[1], csv[2], csv[3], csv[4]) + sale_array.push(@s) + end + return sale_array + end + + def self.find(id) + sale_data = Sale.all + sale_data.find do |data| + if data.id == id + return data + end + end + end + def get_vendor + FarMar::Vendor.all.find_all do |vendor| + vendor.id == @vendor_id + end + end + + def get_product + FarMar::Product.all.each do |product| + if product.id == product_id + return product + end + end + end + + def self.between(beginning_time, end_time) + array = [] + self.all.find_all do |sale_times| + if sale_times.purchase_time >= beginning_time && sale_times.purchase_time <= end_time + array.push(sale_times) + end + end + return array + end end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index bfe19c33..9b8a61f4 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,5 +1,69 @@ +require "pry" +require "csv" + 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.to_i + @market_id = market_id.to_i + end + + def self.all + vendor_array = [] + vendor_data = CSV.read("./support/vendors.csv") + vendor_data.each do |csv| + @v = Vendor.new(csv[0], csv[1], csv[2], csv[3]) + vendor_array.push(@v) + end + return vendor_array + end + + def self.find(id) + vendor_data = Vendor.all + vendor_data.find do |data| + if data.id == id + return data + end + end + end + + def get_market + FarMar::Market.all.find do |market| + market.id == @market_id + end + end + + def get_products + FarMar::Product.all.find_all do |product| + product.vendor_id == @id + end + end + + def get_sales + FarMar::Sale.all.find_all do |sale| + sale.vendor_id == @id + end + end + + def revenue + total = 0 + get_sales.each do |sale| + total = total + sale.amount + end + return total + end + + def self.by_market(market_id) + FarMar::Vendor.all.find_all do |vendor| + vendor.market_id == market_id + + end + + end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 204b458c..fccbbf80 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Market do before :each do - @market = FarMar::Market.new + @market = FarMar::Market.new("id","name","city","county","state","zip","address") end describe "new market instance" do @@ -10,4 +10,25 @@ expect(@market).to be_an_instance_of FarMar::Market end end + + + + describe "return a collection of market instances described in CVS" do + it "returns all of the accounts" do + expect(FarMar::Market.all).to be_an_instance_of Array + end + end + + describe "return one account" do + it "returns one account" do + expect(FarMar::Market.find(1).id).to eq 1 + end + end + + describe ".get_vendor" do + it "returns all accounts with same market id" do + expect(@market.get_vendor).to be_an Array + end + end + end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 5cd77cc9..a86c267a 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,12 +2,45 @@ describe FarMar::Product do before :each do - @product = FarMar::Product.new + @product = FarMar::Product.new("1", "name", "1") end - describe "new product instance; .new" do + + describe "new product instance" do it "creats a new product instance" do expect(@product).to be_an_instance_of FarMar::Product end end + + + describe "return a collection of product instances described in CVS" do + it "returns all of the accounts" do + expect(FarMar::Product.all).to be_an_instance_of Array + end + end + + describe "return one account" do + it "returns one account" do + expect(FarMar::Product.find(1).id).to eq 1 + end + end + + describe "get_vendor" do + it "returns all account with the same vendor id" do + expect(@product.get_vendor).to be_an Array + end + end + + describe "number_of_sales" do + it "returns the number of the product has been sold" do + expect(@product.number_of_sales).to eq 7 + end + end + + describe "self.by_vendor" do + it "returns returns all of the products with the given vendor_id" do + expect(FarMar::Product.all.find_all) + end +end + end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index e1a7aa52..d926ff52 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("id", "amount", "purchase_time", "vendor_id", "product_id") end describe "new sale instance" do @@ -10,4 +10,28 @@ expect(@sale).to be_an_instance_of FarMar::Sale end end + describe "return a collection of sale instances described in CVS" do + it "returns all of the accounts" do + expect(FarMar::Sale.all).to be_an_instance_of Array + end + end + + describe "return one account" do + it "returns one account" do + expect(FarMar::Sale.find(1).id).to eq 1 + end + end + + describe "get_vendor" do + it "returns vendor instance ralated to sale" do + expect(@sale.get_vendor).to be_an Array + end + end + + describe "get_product" do + it "returns product instance related to sale" do + expect(@sale.get_product). to be_an Array + end + end + end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index fb7ee7b9..cb0a4d2e 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 + @vendor = FarMar::Vendor.new("1","Feil-Farrell","8","1") end describe "new vendor instance" do @@ -10,4 +10,49 @@ expect(@vendor).to be_an_instance_of FarMar::Vendor end end + + + describe "return a collection of market instances described in CVS" do + it "returns all of the accounts" do + expect(FarMar::Vendor.all).to be_an_instance_of Array + end + end + + describe "return one account" do + it "returns one account" do + expect(FarMar::Vendor.find(1).id).to eq 1 + end + end + + + describe ".get_market" do + it "returns all accounts with the same market id" do + expect(@vendor.get_market).to be_an_instance_of FarMar::Market + end + end + + + describe ".get_products" do + it "returns all accounts with the same vendor id" do + expect(@vendor.get_products).to be_an_instance_of Array + end + end + + describe ".get_sales" do + it "returns all accounts with the vendor id" do + expect(@vendor.get_sales).to be_an_instance_of Array + end + end + + describe "revenue" do + it "returns the amount of vendor sales in cents" do + expect(@vendor.revenue).to eq 38259 + end + + describe ".by_market" do + it "returns all of the vendors with the given market_id" do + expect(FarMar::Vendor.by_market(2)).to be_an_instance_of Array + end + end + end end diff --git a/support/products.csv b/support/products.csv index 92794f1c..2cd50214 100644 --- a/support/products.csv +++ b/support/products.csv @@ -23,7 +23,7 @@ 23,Calm Carrots,10 24,Fierce Beef,10 25,Helpless Bread,10 -26,Yummy Bread,10 +26,Yummy Bread,10 27,Broken Beets,10 28,Quiet Honey,11 29,Crazy Fish,11