Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FarMar Final #33

Open
wants to merge 14 commits into
base: mmr/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/far_mar.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pry'
require 'csv'
require 'time'
require './lib/far_mar/market'
Expand Down
80 changes: 80 additions & 0 deletions lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -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
79 changes: 78 additions & 1 deletion spec/far_mar/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading