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 #48

Open
wants to merge 28 commits into
base: erg/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
880e889
created spec and methods for initialiaze and self.all for market class
emgord Oct 20, 2015
ac336f9
added initialize and self.all for product. one test still failing
emgord Oct 21, 2015
80165c7
fixed typo so rspec works now for product class
emgord Oct 21, 2015
038c1cf
added initialize and self.all method for sale
emgord Oct 21, 2015
29dd84b
added initialize and self.all method for vendors
emgord Oct 21, 2015
353b5df
added self.find(id) method and spec to all classes
emgord Oct 21, 2015
45fe4e5
added class variables to store csv files
emgord Oct 21, 2015
c2f0957
added method to find vendors with a market id to market class
emgord Oct 21, 2015
d3b495e
changed vendors method in market class to be an instance method, adde…
emgord Oct 21, 2015
5f1c93e
added sales method to vendor class to return all of the vendor sales
emgord Oct 21, 2015
adaa7af
added revenue method to vendor class
emgord Oct 21, 2015
da4aa36
added self.by_market(market_id) method to vendor clss
emgord Oct 21, 2015
87eccba
added vendor method to the product class
emgord Oct 22, 2015
3264c3f
added sales method to product class
emgord Oct 22, 2015
2b24c92
added number_of_sales method to the product class
emgord Oct 22, 2015
99610b4
added self.by_vendor method to the product class
emgord Oct 22, 2015
483bd57
added vendor method to sale class
emgord Oct 22, 2015
1c1d62e
added product method to sale class
emgord Oct 22, 2015
cf92d24
added self.between method to sale class. completed primary requirment…
emgord Oct 22, 2015
4e951a9
added products method to the market class
emgord Oct 22, 2015
2d7e8b4
addedself.search(search_term) to the market class
emgord Oct 22, 2015
7e4619d
added preferred vendor method to market class
emgord Oct 22, 2015
ca9cb9e
added prefered_vendor by date method to the market class
emgord Oct 23, 2015
3d7960c
added worst vendor and worst vendor by date method to market
emgord Oct 23, 2015
3b3915d
added self.most_revenue(n) method to vendor class
emgord Oct 23, 2015
8596666
added self.most_items(n) to vendor class
emgord Oct 23, 2015
e6e335d
added self.revenue(date) to the vendor class
emgord Oct 23, 2015
9b2897e
submitting pull request, still working on self.most_revenue(n)
emgord Oct 23, 2015
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,5 +1,6 @@
require "csv"
require "time"
require "pry"
require "./lib/far_mar/market"
require "./lib/far_mar/product"
require "./lib/far_mar/sale"
Expand Down
81 changes: 80 additions & 1 deletion lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,85 @@
module FarMar

class Market
end

attr_reader :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
@@markets_all ||= []

if @@markets_all == []

markets_array = CSV.read("./support/markets.csv")

markets_array.map! do |market|
FarMar::Market.new(market[0],market[1],market[2], market[3], market[4], market[5], market[6])
end
@@markets_all = markets_array
end
return @@markets_all
end

def self.find(id)
FarMar::Market.all.find do |market|
market.id == id
end
end

def vendors
vendors = FarMar::Vendor.all.find_all do |vendor|
vendor.market_id == @id
end
return vendors
end

def products
market_vendors = self.vendors
market_products = []
market_vendors.each do |vendor|
products = vendor.products
market_products += products
end
return market_products
end

def self.search(search_term)
search_term = search_term.downcase
name_search_results = FarMar::Market.all.find_all do |market|
market.name.downcase.include?(search_term)
end
vendor_search_results = FarMar::Vendor.all.find_all do |vendor|
vendor.name.downcase.include?(search_term)
end
vendor_search_results.map! do |vendor|
vendor.market
end
return name_search_results + vendor_search_results
end

def prefered_vendor(date = nil)
top_vendor = vendors.max_by do |vendor|
vendor.revenue(date)
end
return top_vendor
end

def worst_vendor(date = nil)
low_vend = vendors.min_by do |vendor|
vendor.revenue(date)
end
return low_vend
end


end
end
56 changes: 56 additions & 0 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
module FarMar

class Product

attr_reader :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
@@products_all ||= []

if @@products_all == []

product_array = CSV.read("./support/products.csv")

product_array.map! do |product|
FarMar::Product.new(product[0],product[1],product[2])
end
@@products_all = product_array
end
return @@products_all
end

def self.find(id)
FarMar::Product.all.find do |product|
product.id == id
end
end

def vendor
FarMar::Vendor.all.find do |vendor|
vendor.id == @vendor_id
end
end

def sales
productsales = FarMar::Sale.all.find_all do |sale|
sale.product_id == @id
end
return productsales
end

def number_of_sales
self.sales.length
end

def self.by_vendor(vendor_id)
product_vendors = FarMar::Product.all.find_all do |product|
product.vendor_id == vendor_id
end
return product_vendors
end



end

end
52 changes: 52 additions & 0 deletions lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
module FarMar

class Sale

attr_reader :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.parse(purchase_time)
@vendor_id = vendor_id.to_i
@product_id = product_id.to_i
end

def self.all
@@sales_all ||= []

if @@sales_all == []

sale_array = CSV.read("./support/sales.csv")

sale_array.map! do |sale|
FarMar::Sale.new(sale[0],sale[1],sale[2],sale[3],sale[4])
end
@@sales_all = sale_array
end
return @@sales_all
end

def self.find(id)
FarMar::Sale.all.find do |sale|
sale.id == id
end
end

def vendor
FarMar::Vendor.all.find do |vendor|
vendor.id == @vendor_id
end
end

def product
FarMar::Product.all.find do |product|
product.id == @product_id
end
end

def self.between(beginning_time, end_time)
beginning_time = DateTime.parse(beginning_time)
end_time = DateTime.parse(end_time)
FarMar::Sale.all.find_all do |sale|
sale.purchase_time >= beginning_time && sale.purchase_time <= end_time
end
end
end


end
92 changes: 92 additions & 0 deletions lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,98 @@
module FarMar

class Vendor

attr_reader :id, :name, :employees, :market_id

def initialize(id, name, employees, market_id)
@id = id.to_i
@name = name
@employees = employees.to_i
@market_id = market_id.to_i
end

def self.all
@@vendors_all ||= []

if @@vendors_all == []

vendor_array = CSV.read("./support/vendors.csv")

vendor_array.map! do |vendor|
FarMar::Vendor.new(vendor[0],vendor[1],vendor[2],vendor[3])
end
@@vendors_all = vendor_array
end
return @@vendors_all
end

def self.find(id)
FarMar::Vendor.all.find do |vendor|
vendor.id == id
end
end

def market
FarMar::Market.all.find do |market|
market.id == @market_id
end
end

def products
products = FarMar::Product.all.find_all do |product|
product.vendor_id == @id
end
return products
end

def sales
sales = FarMar::Sale.all.find_all do |sale|
sale.vendor_id == @id
end
return sales
end

def revenue(date = nil)
date = DateTime.parse(date) if date != nil
revenue = 0
sales.each do |sale|
if sale.purchase_time.to_date == date || date == nil
revenue += sale.amount
end
end
return revenue
end

def self.by_market(market_id)
bymarket = FarMar::Vendor.all.find_all do |vendor|
vendor.market_id == market_id
end
return bymarket
end

def self.most_revenue(n)
revenue_array = FarMar::Vendor.all.sort_by do |vendor|
vendor.revenue
end
revenue_array.first(n)
end

def self.most_items(n)
vendors_by_items = FarMar::Vendor.all.sort_by do |vendor|
vendor.products.length
end
vendors_by_items.first(n)
end

def self.revenue(date)
total_revenue = 0
FarMar::Vendor.all.each do |vendor|
total_revenue += vendor.revenue(date)
end
return total_revenue

end

end

end
Loading