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

Final Pull Request #32

Open
wants to merge 53 commits into
base: rrs/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
4c054db
Began writing specs for FarMar::Market initialize
rileslovesyall Oct 19, 2015
8a80115
Writing specs and code for Market self.all
rileslovesyall Oct 19, 2015
889ab6d
working on basic specs and code
rileslovesyall Oct 19, 2015
b43b225
Sales specs for .all still failing, moved over to Vendors, three spec…
rileslovesyall Oct 20, 2015
369f1ce
Sale.rb fixed, tests running
rileslovesyall Oct 20, 2015
c2843c4
.all tests passing for Vendor
rileslovesyall Oct 20, 2015
916634c
Failing tests for product specs (#init, .all) written
rileslovesyall Oct 20, 2015
390737e
Product #initialize and .self tests passing.
rileslovesyall Oct 20, 2015
f31e8eb
Changed the way I'm testing length of csv to length of array created …
rileslovesyall Oct 20, 2015
44440bc
Fixed the find.all nonsense! Vendor specs passing
rileslovesyall Oct 20, 2015
42289c3
Market .find(id) method passing tests
rileslovesyall Oct 21, 2015
a8001f6
Faled tests for Sale .find spec added
rileslovesyall Oct 21, 2015
0654dd9
Code written, spec fixed, test passing for Sale.find
rileslovesyall Oct 21, 2015
cc4cc6f
Spec for Product.find written and failing
rileslovesyall Oct 21, 2015
6795f3b
Product.find code written and passing tests
rileslovesyall Oct 21, 2015
5b1e8d2
Market.vendors specs written and failing
rileslovesyall Oct 21, 2015
bc437e7
Reworked specs slightly, code written and passing for Market.vendors
rileslovesyall Oct 21, 2015
92d99e8
Specs written and failing for Vendor.prodcuts()
rileslovesyall Oct 21, 2015
96aac56
Vendor.products(id) passing tests
rileslovesyall Oct 21, 2015
c696905
Vendor.sales specs written and failing
rileslovesyall Oct 21, 2015
77a2ffe
Vendor.sales code wirrten and passing
rileslovesyall Oct 21, 2015
ca629ab
Changed .all functions to use class variables, cutting down on test time
rileslovesyall Oct 21, 2015
8867fa6
Rewriting methods that i realize need to be instance not class
rileslovesyall Oct 21, 2015
4e360ef
Fixed Vendor.sales to #sales
rileslovesyall Oct 21, 2015
699ea90
Fixed Market class methods that are now instance methods.
rileslovesyall Oct 21, 2015
970d72f
Vendor.by_market specs written and failing
rileslovesyall Oct 21, 2015
d4082ed
revenue tests written adn failing
rileslovesyall Oct 21, 2015
eb2caea
Revenue code passing tests
rileslovesyall Oct 21, 2015
4015f31
Product#vendor spec written and failing
rileslovesyall Oct 21, 2015
12f1c6d
Product#Vendor passing test
rileslovesyall Oct 21, 2015
907a3e8
specs failing for Product#sales
rileslovesyall Oct 21, 2015
78267a6
Product#sales passing tests
rileslovesyall Oct 21, 2015
8917c76
Product#number_of_sales tests failing
rileslovesyall Oct 21, 2015
653514d
Product#number_of_sales passing tests, tests refactored
rileslovesyall Oct 21, 2015
7e00bab
Product.by_vendor(vendor_id) failing tests
rileslovesyall Oct 21, 2015
512ab53
Product.by_vendor tests debugged, code written, tests passing
rileslovesyall Oct 21, 2015
fa61bda
Sale#vendor spec written and failing
rileslovesyall Oct 21, 2015
fbecfc2
Sale#vendor code passing
rileslovesyall Oct 21, 2015
e191593
Sale#product passing tests
rileslovesyall Oct 21, 2015
3d1293a
Primary Requirements Complete, though code could probably use some re…
rileslovesyall Oct 21, 2015
7944a73
Market#products specs failing
rileslovesyall Oct 22, 2015
370283f
Market#products code passing tests
rileslovesyall Oct 22, 2015
ddd7c51
Tests for Market.search(search_term) written and failing
rileslovesyall Oct 22, 2015
d38ff08
100% of code covered -- had to search for a term that was in a vendor…
rileslovesyall Oct 22, 2015
51004d6
Market#preferred_vendor code written and passing
rileslovesyall Oct 22, 2015
32ac0e5
Specs written for Market#worst_vendor
rileslovesyall Oct 23, 2015
bba5f14
Market#worst_vendor code passing tests
rileslovesyall Oct 23, 2015
464b399
Specs written for Market#worst_vendor_by_date(date)
rileslovesyall Oct 23, 2015
a2e56f4
Code written and passing for Market#worst_vendor_by_date(date)
rileslovesyall Oct 23, 2015
4067e40
Vendor.most_items code passing, Vendor.revenue(date) tests written an…
rileslovesyall Oct 23, 2015
3e15f2a
Changed the Market#preferred_vendor method to include both with and w…
rileslovesyall Oct 23, 2015
be09d35
Commented out the Vendor.revenue(date) method because it is taking so…
rileslovesyall Oct 23, 2015
52f137f
Fixed the broken code yayyyyy
rileslovesyall Oct 25, 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/vendor'
require './lib/far_mar/sale'
Expand Down
116 changes: 112 additions & 4 deletions lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,121 @@
# require './support/markets.csv'
# CSV.read('./support/markets.csv')

module FarMar

class Market
attr_reader :market_id, :name, :address, :city, :county, :state, :zip

def initialize

def initialize(market_id, name, address, city, county, state, zip)
@market_id = market_id.to_i
@name = name
@address = address
@city = city
@county = county
@state = state
@zip = zip
end

end
def self.all
@@market_array ||= []
if @@market_array == []
CSV.read('./support/markets.csv').each do |row|
market = FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6])
@@market_array.push(market)
end
end
return @@market_array
end

def self.find(id)
FarMar::Market.all.find {|mar| mar.market_id == id }
end

def vendors
FarMar::Vendor.all.find_all {|ven| ven.market == market_id }
end

def products
ven_array = self.vendors
prod_array = []
ven_array.each do |ven|
prod_array.push(ven.products)
end
return prod_array.flatten
end

def self.search(search_term)
search_term = search_term.downcase
match_markets = []
FarMar::Market.all.each do |market|
match_markets.push(market) if market.name.downcase.match(/#{search_term}/)
end
match_vendors = []
FarMar::Vendor.all.each do |vendor|
match_vendors.push(vendor) if vendor.name.downcase.match(/#{search_term}/)
end
match_vendors.map! {|vendor| vendor.market}
match_vendors.each do |market_id|
market = FarMar::Market.find(market_id)
match_markets.push(market)
end
return match_markets
end

def preferred_vendor(date = nil)
if date == nil
ven = self.vendors
preferred = ven.max_by{|ven| ven.revenue}
else
date = DateTime.strptime(date, "%Y-%m-%d").to_date
max_revenue = 0
pref_vend = nil
total = 0
self.vendors.each do |vendor_inst|
sales_array = []
vendor_inst.sales.each do |sale_inst|
if sale_inst.purchase_time.to_date == date
sales_array.push(sale_inst.amount)
end
end
if sales_array.length > 0
total = sales_array.inject(0, :+)
if total > max_revenue
max_revenue = total
pref_vend = vendor_inst
end
end
end
return pref_vend
end
end

def worst_vendor(date = nil)
if date == nil
ven = self.vendors
worst = ven.min_by{|ven| ven.revenue}
else
date = DateTime.strptime(date, "%Y-%m-%d").to_date
min_revenue = Float::INFINITY
worst_ven = nil
total = 0
self.vendors.each do |vendor_inst|
sales_array = []
vendor_inst.sales.each do |sale_inst|
if sale_inst.purchase_time.to_date == date
sales_array.push(sale_inst.amount)
end
end
if sales_array.length > 0
total = sales_array.inject(0, :+)
if total < min_revenue
min_revenue = total
worst_ven = vendor_inst
end
end
end
return worst_ven
end
end

end
end
43 changes: 42 additions & 1 deletion lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
# CSV.read("./support/products.csv")

module FarMar

class Product
attr_reader :product_id, :name, :vendor_id

def initialize(product_id, name, vendor_id)
@product_id = product_id.to_i
@name = name.to_s
@vendor_id = vendor_id.to_i
end

def self.all
@@product_array ||= []
if @@product_array == []
CSV.read('./support/products.csv').each do |row|
product = FarMar::Product.new(row[0], row[1], row[2])
@@product_array.push(product)
end
end
return @@product_array
end

def self.find(id)
FarMar::Product.all.find {|pro| pro.product_id == id}
end

def vendor
FarMar::Vendor.all.find{|ven| ven.vendor_id == @vendor_id}
end

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

def number_of_sales
self.sales.length
end

def self.by_vendor(vendor_id)
FarMar::Product.all.find_all{|prod| prod.vendor_id == vendor_id}
end

end

end
end
67 changes: 66 additions & 1 deletion lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,73 @@
# require "./support/sales.csv"
# CSV.read("./support/sales.csv")

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.to_i
@amount = amount.to_i
purchase_time = purchase_time.to_s
@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

# def self.all
# @@sales_array ||= []
# if @@sales_array = []
# CSV.read('./support/sales.csv').each do |row|
# sale = FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4])
# @@sales_array.push(sale)
# end
# end
# return @@sales_array
# end

def self.sales_init
@@sales_by_id = Hash.new
@@sales_by_product = Hash.new {|hash, key| hash[key] = []}
@@sales_by_vendor = Hash.new {|hash, key| hash[key] = []}
CSV.read('./support/sales.csv').each do |row|
new_sale = FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4])
@@sales_by_id[new_sale.sale_id] = new_sale
@@sales_by_product[new_sale.product_id].push(new_sale)
@@sales_by_vendor[new_sale.vendor_id].push(new_sale)
end
@@sales_all = sales_by_product.values.flatten
end

def self.sales_by_product
return @@sales_by_product
end

def self.sales_by_vendor
return @@sales_by_vendor
end

def self.all
return @@sales_all
end

def self.find(id)
#FarMar::Sale.all.find {|sale| sale.sale_id == id}
return @@sales_by_id[id]
end

def vendor
FarMar::Vendor.all.find {|ven| ven.vendor_id == @vendor_id}
end

def product
FarMar::Product.all.find {|pro| pro.product_id == @product_id}
end

def self.between(beginning_time, ending_time)
FarMar::Sale.all.find_all do |sale|
sale.purchase_time.between?(beginning_time, ending_time)
end
end

end

Expand Down
80 changes: 79 additions & 1 deletion lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,86 @@
# require './suport/vendors.csv'
# CSV.read('./support/vendors.csv')

module FarMar

class Vendor
attr_reader :vendor_id, :name, :employees, :market

def initialize(vendor_id, name, employees, market)
@vendor_id = vendor_id.to_i
@name = name
@employees = employees
@market = market.to_i
end

def self.all
@@vendor_array ||= []
if @@vendor_array == []
CSV.read('./support/vendors.csv').each do |row|
vendor = FarMar::Vendor.new(row[0], row[1], row[2], row[3])
@@vendor_array.push(vendor)
end
end
return @@vendor_array
end

def self.find(vendor_id)
FarMar::Vendor.all.find {|ven| ven.vendor_id == vendor_id}
end

def products
FarMar::Product.all.find_all {|pro| pro.vendor_id == @vendor_id}
end

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

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

def self.by_market(market_id)
FarMar::Vendor.all.find_all {|ven| ven.market == market_id }
end

# Couldn't figure out how to rewrite this to get it to run in a decent amount of time
# def self.most_revenue(n)
# top_vendors = @@vendor_array.sort_by{|ven| ven.revenue}
# return top_vendors[0..n]
# end

def self.most_items(n)
most_items = 0
vendors_by_items = []
vendor_array = @@vendor_array.dup
n.times do
vendor = vendor_array.max_by{|ven| ven.products.length}
vendors_by_items.push(vendor)
vendor_array.delete(vendor)
end
return vendors_by_items
end

# This method is currently taking too long to run; I can't even get the tests to finish on it. Not sure if I'll have the time to retool it
# def self.revenue(date)
# date = DateTime.strptime(date, "%Y-%m-%d").to_date
# revenue = 0
# FarMar::Vendor.all.each do |vendor_inst|
# vendor_inst.sales.each do |sale_inst|
# if sale_inst.purchase_time.to_date == date
# revenue += sale_inst.amount
# end
# end
# end
# return revenue
# end

end

Expand Down
Loading