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 with part 1 of optionals #29

Open
wants to merge 47 commits into
base: jmn/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
eac6cfa
removed unnecessary parentheses
jennaplusplus Oct 19, 2015
6742555
added self.all tests and code
jennaplusplus Oct 19, 2015
7e9b73e
updated initialize method/tests for Market
jennaplusplus Oct 20, 2015
3681850
added self.all methods/tests for Vendor class
jennaplusplus Oct 20, 2015
aad588a
added self.all method/tests for Product class
jennaplusplus Oct 20, 2015
9a6162c
updated vendor with self.all
jennaplusplus Oct 20, 2015
46cfc74
updated integers and csv tests
jennaplusplus Oct 20, 2015
3eb24eb
updated purchase time formatting
jennaplusplus Oct 21, 2015
48623d5
code and test for self.find methods
jennaplusplus Oct 21, 2015
c112d1f
tests and code for vendors method in Market class
jennaplusplus Oct 21, 2015
2386dba
market method for Vendor class
jennaplusplus Oct 21, 2015
354e72f
products method for Vendor class
jennaplusplus Oct 21, 2015
3b33512
sales method for Vendor class
jennaplusplus Oct 21, 2015
43e48c1
revenue method for Vendor class
jennaplusplus Oct 21, 2015
594556c
self.by_market(market_id) method for Vendor class
jennaplusplus Oct 21, 2015
08ad325
vendor method for Product class
jennaplusplus Oct 21, 2015
7f76ce5
sales method for Product class
jennaplusplus Oct 21, 2015
62c6396
number_of_sales method in Product class
jennaplusplus Oct 21, 2015
3ff488f
self.by_vendor(vendor_id) method for Product class
jennaplusplus Oct 21, 2015
e4e0079
vendor method for Sale class
jennaplusplus Oct 21, 2015
e6661c5
product method for Sale class
jennaplusplus Oct 21, 2015
a7a602e
time method for Sale class
jennaplusplus Oct 21, 2015
bc01376
additional test for time method in Sale class
jennaplusplus Oct 21, 2015
bb0d790
refactored market specs
jennaplusplus Oct 21, 2015
50cfe81
updated self.all methods to be faster
jennaplusplus Oct 21, 2015
cff392e
refactored specs for creating example instances
jennaplusplus Oct 21, 2015
328b775
additional tests in Vendor class
jennaplusplus Oct 21, 2015
66343bb
added additional case for Product tests
jennaplusplus Oct 21, 2015
7073a69
done with primary reqs and basic refactoring
jennaplusplus Oct 21, 2015
d87afd2
added possible formatting to time method in Sale class
jennaplusplus Oct 22, 2015
34c851b
first pass at product method in Market class
jennaplusplus Oct 22, 2015
091ad57
added test to products method in Market class
jennaplusplus Oct 22, 2015
e3fbd75
first pass at search method in Market class
jennaplusplus Oct 22, 2015
ed2a177
added test to search method in Market class
jennaplusplus Oct 22, 2015
a398fd7
fixed variable labels
jennaplusplus Oct 22, 2015
c4571ab
best and worst vendor methods in Market class
jennaplusplus Oct 22, 2015
e0a6fbd
skeleton for best vendor by date
jennaplusplus Oct 22, 2015
fcccc00
best vendor by date
jennaplusplus Oct 22, 2015
5fdc4f8
worst vendor by date
jennaplusplus Oct 22, 2015
37710ed
more methods
jennaplusplus Oct 22, 2015
3d18f50
finished additional vendor methods
jennaplusplus Oct 22, 2015
59af043
finished part one optionals, LOOONG specs
jennaplusplus Oct 22, 2015
0c0cd52
updated vendor code
jennaplusplus Oct 22, 2015
994b203
added comments about long specs
jennaplusplus Oct 23, 2015
0655982
updated support folder
jennaplusplus Oct 23, 2015
2da0a05
changed code to speed up specs
jennaplusplus Oct 23, 2015
7b57967
updated hash creation in Sale class
jennaplusplus 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
119 changes: 119 additions & 0 deletions lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,125 @@
module FarMar
class Market
attr_reader :id, :name

def initialize(market_info)
@id = market_info[0].to_i
@name = market_info[1]
@address = market_info[2]
@city = market_info[3]
@county = market_info[4]
@state = market_info[5]
@zip = market_info[6]
end

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

if @@market_array == []
CSV.read("./support/markets.csv").each do |market|
new_market = FarMar::Market.new(market)
@@market_array.push(new_market)
end
end

return @@market_array
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 == self.id
end
return vendors
end

def products
products = []
vendors = self.vendors
vendors.each do |vendor|
vendor_products = vendor.products
vendor_products.each do |vendor_product|
products.push(vendor_product)
end
end
return products
end

def self.search(search_term)
results = []
name_matches = FarMar::Market.all.find_all do |market|
market.name.downcase.include?(search_term.downcase)
end
name_matches.each { |match| results.push(match)}
vendor_matches = FarMar::Vendor.all.find_all do |vendor|
vendor.name.downcase.include?(search_term.downcase)
end
vendor_matches.each { |match| results.push(match)}
return results
end

def preferred_vendor
vendors = self.vendors
best_vendor = vendors.max_by do |vendor|
vendor.revenue
end
return best_vendor
end

def preferred_vendor_by_date(date)
# expecting a string in the form yyyy-mm-dd
date = Date.parse(date.to_s)

vendors = self.vendors
vendor_hash = {}
vendors.each do |vendor|
vendor_sales = vendor.sales
day_sales = vendor_sales.find_all do |sale|
date == sale.purchase_time.to_date
end
day_revenue = 0
day_sales.each do |sale|
day_revenue = day_revenue + sale.amount
end
vendor_hash[vendor.id] = day_revenue
end
best_vendor_id = vendor_hash.key(vendor_hash.values.max)
return FarMar::Vendor.find(best_vendor_id)
end

def worst_vendor
vendors = self.vendors
worst_vendor = vendors.min_by do |vendor|
vendor.revenue
end
return worst_vendor
end

def worst_vendor_by_date(date)
# expecting a string in the form yyyy-mm-dd
date = Date.parse(date.to_s)

vendors = self.vendors
vendor_hash = {}
vendors.each do |vendor|
vendor_sales = vendor.sales
day_sales = vendor_sales.find_all do |sale|
date == sale.purchase_time.to_date
end
day_revenue = 0
day_sales.each do |sale|
day_revenue = day_revenue + sale.amount
end
vendor_hash[vendor.id] = day_revenue
end
worst_vendor_id = vendor_hash.key(vendor_hash.values.min)
return FarMar::Vendor.find(worst_vendor_id)
end

end
end
69 changes: 69 additions & 0 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,75 @@
module FarMar
class Product
attr_reader :id, :vendor_id

def initialize(product_info)
@id = product_info[0].to_i
@name = product_info[1]
@vendor_id = product_info[2].to_i
end

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

if @@product_array == []
CSV.read("./support/products.csv").each do |product|
new_product = FarMar::Product.new(product)
@@product_array.push(new_product)
end
end

return @@product_array
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 == self.vendor_id
end
end

def sales
sales_hash = FarMar::Sale.sales_by_product
return sales_hash[@id]
end

def product_revenue
product_revenue = 0
sales = self.sales
sales.each do |sale|
product_revenue = product_revenue + sale.amount
end
return product_revenue
end

def number_of_sales
return self.sales.length
end

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

def self.most_revenue(n)
sorted_products = FarMar::Product.all.sort_by do |product|
product_revenue = 0
product_sales = product.sales
product_sales.each do |sale|
product_revenue = product_revenue + sale.amount
end
product_revenue
end
sorted_products.reverse!
return sorted_products[0..n-1]
end

end
end
67 changes: 67 additions & 0 deletions lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,73 @@
module FarMar
class Sale
attr_reader :id, :vendor_id, :product_id, :amount, :purchase_time

def initialize(sale_info)
@id = sale_info[0].to_i
@amount = sale_info[1].to_i
@purchase_time = DateTime.strptime(sale_info[2], "%Y-%m-%d %H:%M:%S %z")
@vendor_id = sale_info[3].to_i
@product_id = sale_info[4].to_i
end

def self.all
return sales_by_product.values.flatten
end

def self.sales_by_product
@@sales_by_product ||= Hash.new { |hash,key| hash[key] = [] }

if @@sales_by_product.empty?
CSV.read("./support/sales.csv").each do |sale|
new_sale = FarMar::Sale.new(sale)
@@sales_by_product[new_sale.product_id].push(new_sale)
end
end
return @@sales_by_product
end

def self.sales_by_vendor
@@sales_by_vendor ||= Hash.new { |hash,key| hash[key] = [] }

if @@sales_by_vendor.empty?
CSV.read("./support/sales.csv").each do |sale|
new_sale = FarMar::Sale.new(sale)
@@sales_by_vendor[new_sale.vendor_id].push(new_sale)
end
end
return @@sales_by_vendor
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 == self.vendor_id
end
end

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

def self.between(beginning_time, end_time)
if beginning_time.class == String
beginning_time = DateTime.strptime(beginning_time, "%Y-%m-%d %H:%M:%S %z")
end
if end_time.class == String
end_time = DateTime.strptime(end_time, "%Y-%m-%d %H:%M:%S %z")
end
sales_between = FarMar::Sale.all.find_all do |sale|
sale.purchase_time.between?(beginning_time,end_time)
end
return sales_between
end

end
end
99 changes: 99 additions & 0 deletions lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,105 @@
module FarMar
class Vendor
attr_reader :id, :market_id, :name

def initialize(vendor_info)
@id = vendor_info[0].to_i
@name = vendor_info[1]
@number_of_employees = vendor_info[2].to_i
@market_id = vendor_info[3].to_i
end

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

if @@vendor_array == []
CSV.read("./support/vendors.csv").each do |vendor|
new_vendor = FarMar::Vendor.new(vendor)
@@vendor_array.push(new_vendor)
end
end

return @@vendor_array
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 == self.market_id
end
end

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

def sales
sales_hash = FarMar::Sale.sales_by_vendor
return sales_hash[@id]
end

def revenue
revenue = 0
sales_array = self.sales
sales_array.each do |sale|
revenue = revenue + sale.amount
end
return revenue
end

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

def self.most_revenue(n)
sorted_vendors = FarMar::Vendor.all.sort_by do |vendor|
vendor.revenue
end
sorted_vendors.reverse!
return sorted_vendors[0..n-1]
end

def self.most_items(n)
sorted_vendors = FarMar::Vendor.all.sort_by do |vendor|
vendor.products.length
end
sorted_vendors.reverse!
return sorted_vendors[0..n-1]
end

def self.revenue_by_date(date)
date = Date.parse(date.to_s)
total_revenue = 0
FarMar::Sale.all.each do |sale|
if sale.purchase_time.to_date == date
total_revenue = total_revenue + sale.amount
end
end
return total_revenue
end

def vendor_revenue_by_date(date)
date = Date.parse(date.to_s)
day_sales = self.sales.find_all do |sale|
date == sale.purchase_time.to_date
end
day_revenue = 0
day_sales.each do |sale|
day_revenue = day_revenue + sale.amount
end
return day_revenue
end

end
end
Loading