diff --git a/lib/far_mar.rb b/lib/far_mar.rb index 24119757..cb1d418e 100644 --- a/lib/far_mar.rb +++ b/lib/far_mar.rb @@ -1,6 +1,6 @@ require 'csv' require 'time' require './lib/far_mar/market' -require './lib/far_mar/vendor' +require_relative('far_mar/vendor.rb') require './lib/far_mar/sale' require './lib/far_mar/product' diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 638ff607..a90c0054 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,5 +1,51 @@ +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 + + # Returns an array of Market instances representing all of the markets + # Not sure I fully understand how this method works. + def self.all + csv_info = CSV.read("./support/markets.csv") + markets = [] + csv_info.each do |line| + markets.push(Market.new(line[0], line[1], line[2], line[3], line[4], line[5], line[6])) + end + return markets + end + + # Returns an instance of Market where the value of the id in the csv + # matches the passed parameter. + def self.find(market_id) + all.each do |market| + if market.id == market_id + return market # the instance of the market. + end + end + end + + # Given the ID of the market, RETURN vendors with the same market_id. + def vendors + vendor_array = [] + FarMar::Vendor.all.find_all do |v| + if v.market_id == id + vendor_array.push(v) + end + end + return vendor_array + end + end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 8efc48d2..e4298cbe 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,5 +1,76 @@ +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 + + # Returns a collection of product instances + # representing all of the products described in the CSV + def self.all + csv_info = CSV.read("./support/products.csv") + products = [] + csv_info.each do |line| + products.push(Product.new(line[0], line[1], line[2])) + end + return products + end + + # Returns an instance of product where the value of the id in the csv + # matches the passed parameter. + def self.find(product_id) + all.each do |product| + if product.id == product_id + return product # the instance of the vendor. + end + end + end + + # Returns the instance of the Vendor by the product vendor_id. + def vendor + FarMar::Vendor.all.each do |vend| + if vend.id == vendor_id + return vend + end + end + end + + # Returns array of sales where the product_id equals the Product ID + def sales + sales_array = [] + FarMar::Sale.all.find_all do |sales| + if sales.product_id == id + sales_array.push(sales) + end + end + end + + # Returns the number of times this product has been sold. + def number_of_sales + count = 0 + FarMar::Sale.all.each do |s| + if s.product_id == id + count += 1 + end + end + return count + end + + # Returns all of the products matching the vendor_id + def self.by_vendor(vend_id) + product_array = [] + self.all.find_all do |prod| + if prod.vendor_id == vend_id + product_array.push(prod) + end + end + return product_array + end end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index d68f9ded..98d6ea55 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,5 +1,69 @@ +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 + + # Returns a collection of sale instances + # representing all of the sales described in the CSV + def self.all + csv_info = CSV.read("./support/sales.csv") + sales = [] + csv_info.each do |line| + sales.push(Sale.new(line[0], line[1], line[2], line[3], line[4])) + end + return sales + ## for optimization, say, if @sales has stuff in it, don't run it again, just return it. + end + + # Returns an instance of sale where the value of the id in Sale + # matches the passed parameter. + def self.find(sale_id) + all.each do |sales| + if sales.id == sale_id + return sales # the instance of the sale. + end + end + end + + # Returns the instance of the vendor associated with the Sale vendor_id + def vendor + FarMar::Vendor.all.each do |v| + if v.id == vendor_id + return v + end + end + end + + # Returns the one product ID associated with this sale using the product_id + def product + FarMar::Product.all.each do |prod| + if prod.id == product_id + return prod + end + end + end + + # Returns an array of sale objects where the purchase time is between the + # two times given as arguments. + def self.between(beginning_time, end_time) + sales_array = [] + self.all.find_all do |sales| + if sales.purchase_time >= beginning_time && sales.purchase_time <= end_time + sales_array.push(sales) + end + end + return sales_array + end + end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index bfe19c33..9e45ac25 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,5 +1,92 @@ +require "csv" + module FarMar class Vendor + attr_accessor :id, :name, :employees, :market_id, :vendors + + def initialize(id, name, employees, market_id) + @id = id.to_i + @name = name + @employees = employees.to_i + @market_id = market_id.to_i + end + + # Returns an array of Vendor instances + # representing all of the vendors in the CSV + def self.all + csv_info = CSV.read("./support/vendors.csv") + vendors = [] + csv_info.each do |line| + vendors.push(Vendor.new(line[0], line[1], line[2], line[3])) + end + return vendors + end + + # Returns an instance of Vendor where the value of the id + # matches the passed parameter. + def self.find(vendor_id) + all.each do |vendor| + if vendor.id == vendor_id + return vendor # the instance of the vendor. + end + end + end + + # Checking the market_id (of the vendor), what market do I belong to? + def market + FarMar::Market.all.each do |market| + if market.id == market_id + return market + end + end + end + + # Returns an array of products with a vendor_id that matches the Vendor id + def products + product_array = [] + FarMar::Product.all.find_all do |product| + if product.vendor_id == id + product_array.push(product) + end + end + return product_array + end + + # Returns array of sales with vendor_id that match the vendor's ID + def sales + sales_array = [] + FarMar::Sale.all.find_all do |sale| + if sale.vendor_id == id + sales_array.push(sale) + end + end + return sales_array + end + + # Add all sales for vendor. Return sum in cents. + # If the vendor_id in Sale matches the Vendor ID, add them. Start a count. + def revenue + sale_cents = 0 + FarMar::Sale.all.each do |sale| + if sale.vendor_id == id + sale_cents += sale.amount + end + end + return sale_cents + end + + # Returns all of the vendors with the given market_id. + # (check the Vendors and if market_id matches market_id, array them.) + def self.by_market(market_id) + vendor_by_market_array = [] + all.each do |vendors| + if vendors.market_id == market_id + vendor_by_market_array.push(vendors) + end + end + return vendor_by_market_array + end + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 3d9cf287..d42a0503 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(1, 2, 3, 4, 5, 6, 7) end describe "Market.new" do @@ -10,4 +10,30 @@ expect(@market).to be_an_instance_of FarMar::Market end end + + describe ".all" do + it "returns an array" do + expect(FarMar::Market.all).to be_an Array + end + end + + describe ".find" do + it "returns a single market" do + expect(FarMar::Market.find(1)).to be_an_instance_of FarMar::Market + expect(FarMar::Market.find(14).id).to eq 14 + end + end + + describe "#vendors" do + before :each do + @vendors = FarMar::Vendor.new(1,2,3,4) + end + it "returns an array of vendors" do + expect(@market.vendors).to be_an Array + end + + it "has elements in the array" do + expect(@market.vendors.length).to eq 6 + end + end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 5f4a040b..b11e730b 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(1,2,3) end describe "Product.new" do @@ -10,4 +10,45 @@ expect(@product).to be_an_instance_of FarMar::Product end end + + describe ".all" do + it "returns an array" do + expect(FarMar::Product.all).to be_an Array + end + end + + describe ".find" do + it "returns a single vendor" do + expect(FarMar::Product.find(2)).to be_an_instance_of FarMar::Product + expect(FarMar::Product.find(2).id).to eq 2 + end + end + + describe "#vendor" do + it "returns a single vendor" do + expect(@product.vendor).to be_an_instance_of FarMar::Vendor + end + end + + describe "#sales" do + it "returns an array of sales" do + expect(@product.sales).to be_an Array + end + end + + describe "#number_of_sales" do + it "returns number of times the product has been sold" do + expect(@product.number_of_sales).to be >= 0 + end + end + + describe ".by_vendor" do + it "returns an array" do + expect(FarMar::Product.by_vendor(1)).to be_an Array + end + it "returns a certain number of instances" do + expect(FarMar::Product.by_vendor(5).length).to eq 3 + end + end + end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 97435fad..93cc529d 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(19,9035,"2013-11-11 03:37:15 -0800",4,6) end describe "Sale.new" do @@ -10,4 +10,36 @@ expect(@sale).to be_an_instance_of FarMar::Sale end end + + describe ".all" do + it "returns an array" do + expect(FarMar::Sale.all).to be_an Array + end + end + + describe ".find" do + it "returns a single sale" do + expect(FarMar::Sale.find(2)).to be_an_instance_of FarMar::Sale + expect(FarMar::Sale.find(2).id).to eq 2 + end + end + + describe "#vendor" do + it "returns a specific vendor" do + expect(@sale.vendor).to be_an_instance_of FarMar::Vendor + end + end + + describe "#product" do + it "returns a specific product" do + expect(@sale.product).to be_an_instance_of FarMar::Product + end + end + + describe ".between" do + it "returns an array of sales" do + expect(FarMar::Sale.between(1,2)).to be_an Array + end + end + end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 1f584e68..7cc1ffc0 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, 2, 3, 1) end describe "Vendor.new" do @@ -10,4 +10,54 @@ expect(@vendor).to be_an_instance_of FarMar::Vendor end end + + describe ".all" do + it "returns an array" do + expect(FarMar::Vendor.all).to be_an Array + end + end + + describe ".find" do + it "returns a single vendor" do + expect(FarMar::Vendor.find(2)).to be_an_instance_of FarMar::Vendor + expect(FarMar::Vendor.find(2).id).to eq 2 + end + end + + describe "#market" do + it "returns a specific market" do + expect(@vendor.market).to be_an_instance_of FarMar::Market + end + end + + describe "#products" do + it "returns an array of products" do + expect(@vendor.products).to be_an Array + end + end + + describe "#sales" do + it "returns an array of sales" do + expect(@vendor.sales).to be_an Array + end + end + + describe "#revenue" do + it "returns a number of cents" do + expect(@vendor.revenue).to be > 0 + end + end + + describe ".by_market" do + it "returns an array of vendors" do + expect(FarMar::Vendor.by_market(1)).to be_an Array + expect(FarMar::Vendor.by_market(1).length).to eq 6 + end + + # it "returns the right number of things" do + # FarMar::Vendor.by_market(1).each do |s| + # expect() + # end + # end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 47ba5329..da54716a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,8 @@ require 'simplecov' SimpleCov.start require "./lib/far_mar" + +RSpec.configure do |config| + config.order = 'random' +end +