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

Rlh/master #35

Open
wants to merge 36 commits into
base: rlh/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
734d92a
created specs and methods self.all for market and product classes
hougland Oct 19, 2015
83102d2
added self.all and specs for sales class
hougland Oct 19, 2015
3f8f4bf
added self.all method and specs for sale class
hougland Oct 19, 2015
8f125de
created self.all method and specs for vendor
hougland Oct 19, 2015
c0a4a18
started working on self.find for market class
hougland Oct 19, 2015
2a7bdae
finished market self.find(id) method and specs
hougland Oct 20, 2015
7639bb3
added vendor.find(id) method and specs
hougland Oct 20, 2015
49929a5
fixed problem with vendor self.find(id) and added product.find(id) an…
hougland Oct 20, 2015
1f54131
added self.find(id) method and specs for sale class
hougland Oct 20, 2015
f6580de
created specs for Market.vendors method
hougland Oct 20, 2015
efa78bc
added vendors method to market class + specs
hougland Oct 20, 2015
c9076ea
added market method to vendor class + specs
hougland Oct 20, 2015
f93f261
add products method to vendor class + specs
hougland Oct 20, 2015
f251fb7
add sales method for vendor class + specs
hougland Oct 20, 2015
a592f04
add revenue method to vendor class + specs
hougland Oct 20, 2015
8b1b71f
add specs for self.by_market in vendor class
hougland Oct 21, 2015
68fa8ce
add self.by_market for vendor class
hougland Oct 21, 2015
af11a3b
add vendor method + specs to product class
hougland Oct 21, 2015
be031c4
add sales method + specs to product class
hougland Oct 21, 2015
d4e4af0
add number_of_sales + specs to product class
hougland Oct 21, 2015
f706259
add self.by_vendor(vendor_id) method for product class + specs
hougland Oct 21, 2015
628601f
add vendor method + specs to sale class
hougland Oct 21, 2015
4e0f5ba
add product method + specs to sale class
hougland Oct 21, 2015
770b4fd
add self.between method to sale class + specs
hougland Oct 21, 2015
6ad6efe
add products method to market class + specs
hougland Oct 21, 2015
5f4ef88
self.search(search_term in market class correctly returns market inst…
hougland Oct 22, 2015
d227797
finished self.search for market class
hougland Oct 22, 2015
6b73fa1
add prefered vendor method to market class + specs
hougland Oct 22, 2015
cd51162
add worst_vendor method for market class + specs
hougland Oct 22, 2015
f94e413
ad prefered_vendor_on(date) method + specs in market class
hougland Oct 22, 2015
1cb2fc2
add worst_vendor_on(date) for market class + specs
hougland Oct 22, 2015
4e55aca
add self.most_revenue(n) method to vendor + specs
hougland Oct 22, 2015
4a62ed9
add self.most_items(n) to vendor class + specs
hougland Oct 23, 2015
788e8d5
added self.revenue(date) method + specs to vendor class
hougland Oct 23, 2015
29ea56a
add revenue(date) method + specs to vendor class
hougland Oct 23, 2015
cb3f3b6
didn't finish self.most_revenue(n) for product class, but specs are done
hougland 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
Expand Up @@ -2,6 +2,7 @@
require "time"
require "rspec"
require "csv"
require "date"
require "./lib/far_mar/market.rb"
require "./lib/far_mar/vendor.rb"
require "./lib/far_mar/product.rb"
Expand Down
160 changes: 151 additions & 9 deletions lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,157 @@
CSV.read("./support/markets.csv")

module FarMar
class Market
CSV.read("./support/markets.csv")
# @id =
# @name =
# @address =
# @city =
# @county =
# @state =
# @zip =

attr_reader :id, :name

def initialize(id, name, address, city, county, state, zip)
@id = id
@name = name
@address = address
@city = city
@county = county
@state = state
@zip = zip
end

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

if @@all_markets == []
CSV.read("./support/markets.csv").each do |line|
y = FarMar::Market.new(line[0].to_i, line[1], line[2], line[3], line[4], line[5], line[6])
@@all_markets.push(y)
end
end

return @@all_markets
end

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

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

def products
# empty array to fill with collection of products
all_products = []

# get a collection of all vendors
all_vendors = vendors

# to each vendor instance, run the products method to get a list of products,
# then push all of those proudcts into an array
all_vendors.each do |vendor_instance|
all_products.push(vendor_instance.products)
end

# need to flatten all products so it doesn't return an array of arrays
return all_products.flatten!
end

def self.search(search_term)
search_term_instances = []

markets = FarMar::Market.all

markets.each do |mkt_instance|
if mkt_instance.name.match(/#{search_term}/i)
search_term_instances.push(mkt_instance)
end

mkt_vendors = mkt_instance.vendors

mkt_vendors.each do |vendor_instance|
if vendor_instance.name.match(/#{search_term}/i)
search_term_instances.push(mkt_instance)
end
end
end
return search_term_instances
end

def prefered_vendor
highest_revenue = 0
ven_highest_revenue = nil

vendors.each do |vendor_instance|
ven_revenue = vendor_instance.revenue
if ven_revenue > highest_revenue
highest_revenue = ven_revenue
ven_highest_revenue = vendor_instance
end
end
return ven_highest_revenue
end

def prefered_vendor_on(date)
date = DateTime.strptime(date, "%Y-%m-%d").to_date
highest_revenue = 0
ven_highest_revenue = nil

vendors.each do |vendor_instance|
vendor_instance.sales.each do |sales_instance|
sales_on_date = []
revenue = 0
if sales_instance.purchase_time.to_date == date
sales_on_date.push(sales_instance.amount)
end
if sales_on_date.length > 0
sales_on_date.each {|amount| revenue += amount}
if revenue > highest_revenue
highest_revenue = revenue
ven_highest_revenue = vendor_instance
end
end
end
end
return ven_highest_revenue
end

def worst_vendor
lowest_revenue = Float::INFINITY
ven_lowest_revenue = nil

vendors.each do |vendor_instance|
ven_revenue = vendor_instance.revenue
if ven_revenue < lowest_revenue
lowest_revenue = ven_revenue
ven_lowest_revenue = vendor_instance
end
end
return ven_lowest_revenue
end

def worst_vendor_on(date)
date = Date.parse(date)
lowest_revenue = Float::INFINITY
ven_lowest_revenue = nil

vendors.each do |vendor_instance|
sales_on_date = []
revenue = 0
vendor_instance.sales.each do |sales_instance|
if sales_instance.purchase_time.to_date == date
sales_on_date.push(sales_instance.amount)
end
end
if sales_on_date.length > 0
sales_on_date.each {|amount| revenue += amount}
if revenue < lowest_revenue
lowest_revenue = revenue
ven_lowest_revenue = vendor_instance
end
end
end
return ven_lowest_revenue
end

end
end
58 changes: 58 additions & 0 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,63 @@

module FarMar
class Product
attr_reader :id, :name, :vendor_id

def initialize(id, name, vendor_id)
@id = id
@name = name
@vendor_id = vendor_id
end

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

if @@all_products == []
CSV.read("./support/products.csv").each do |line|
y = FarMar::Product.new(line[0].to_i, line[1], line[2].to_i)
@@all_products.push(y)
end
end

return @@all_products
end

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

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

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

def sales
FarMar::Sale.all.find_all do |sales_instance|
sales_instance.product_id == @id
end
end

def number_of_sales
self.sales.length
end

# def self.most_revenue(n)
# most_revenue = []
#
# all.each do |product_instance|
# product_instance
# end
#
#
# end
end
end
50 changes: 49 additions & 1 deletion lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
CSV.read("./support/sales.csv")

module FarMar
class Sale
attr_reader :id, :amount, :purchase_time, :vendor_id, :amount, :product_id

def initialize(id, amount, purchase_time, vendor_id, product_id)
@id = 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
@@all_sales ||= []

if @@all_sales == []
CSV.read("./support/sales.csv").each do |line|
y = FarMar::Sale.new(line[0].to_i, line[1].to_i, line[2], line[3].to_i, line[4].to_i)
@@all_sales.push(y)
end
end

return @@all_sales
end

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

def self.between(beginning_time, end_time)
beginning_time = DateTime.strptime(beginning_time, "%Y-%m-%d %H:%M:%S %z")
end_time = DateTime.strptime(end_time, "%Y-%m-%d %H:%M:%S %z")

FarMar::Sale.all.find_all do |sales_instance|
sales_instance.purchase_time >= beginning_time && sales_instance.purchase_time <= end_time
end
end

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

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