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

Kld/master #41

Open
wants to merge 40 commits into
base: kld/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
bb9902f
Added market all and find methods and specs for those methods
kdefliese Oct 21, 2015
643cc61
Added all and find methods for vendor class and specs for those methods
kdefliese Oct 21, 2015
5b09403
Added all and find methods for vendor class and specs for those metho…
kdefliese Oct 21, 2015
01f4cab
Added sale all and find methods and specs for both methods
kdefliese Oct 21, 2015
48b1558
Added vendor method for market class and specs for it
kdefliese Oct 21, 2015
177ae78
Added find market method for vendor class and specs, fixed market fin…
kdefliese Oct 21, 2015
3f93427
Added the find_products method for the vendor class and specs for it
kdefliese Oct 21, 2015
6737bce
Added the find_sales method for the vendor class and specs for it
kdefliese Oct 21, 2015
9f4076e
Added total_sales method for vendor class and specs for it; added amo…
kdefliese Oct 21, 2015
b4f5c88
Renamed total_sales method to revenue per the specs
kdefliese Oct 21, 2015
da4daed
Added self.by_market method to the Vendor class and specs for it
kdefliese Oct 21, 2015
45e3032
Added find_vendor method for Product class and specs for it
kdefliese Oct 21, 2015
8245d18
Added find_sales method to the Product class and specs for it
kdefliese Oct 21, 2015
a129c4c
Added number_of_sales method for the Product class and specs for it
kdefliese Oct 21, 2015
91e37fb
Added self.by_vendor method for the Product class and specs for it
kdefliese Oct 21, 2015
0274da0
Added find_vendor method to Sale class and specs for it
kdefliese Oct 22, 2015
e36051a
Modified Sale class to use DateTime and added specs for Sale self.bet…
kdefliese Oct 22, 2015
157e7d4
Added self.between method for Sale class
kdefliese Oct 22, 2015
7084298
Refactored Market class to use a helper hash creater method
kdefliese Oct 22, 2015
b824f39
Refactored Vendor class to use a helper hash-creator method and updat…
kdefliese Oct 22, 2015
83b6457
Refactored Product class to use a helper hash-creator method and upda…
kdefliese Oct 22, 2015
8259556
Refactored Sale class to include a hash-creator-helper method and edi…
kdefliese Oct 22, 2015
a4de686
Modified Market self.all and self.find methods to use a class variabl…
kdefliese Oct 22, 2015
9ecfe0f
Modified Vendor self.all and self.find methods to use a class variabl…
kdefliese Oct 22, 2015
1e57a74
Modified Product self.all and self.find methods to use a class variab…
kdefliese Oct 22, 2015
24e825e
Modified Sale self.all and self.find methods to use a class variable …
kdefliese Oct 22, 2015
8ec566e
Modified Vendor find_sales method to make use of new Sale.all method
kdefliese Oct 22, 2015
9ce2970
Modified Sale self.between method to make use of new Sale.all method,…
kdefliese Oct 22, 2015
21ba3b0
Added more thorough Market specs
kdefliese Oct 22, 2015
069c5ef
Added more thorough vendor specs
kdefliese Oct 22, 2015
3524043
Added more thorough Product specs
kdefliese Oct 22, 2015
7d12d91
Added more thorough Sale specs and changed csv class variables in all…
kdefliese Oct 22, 2015
a7decdc
Added find_products method for Market class and specs for it
kdefliese Oct 23, 2015
11d632b
Fixed find_products Market method to return an array of products
kdefliese Oct 23, 2015
71820e4
Modified Market find_products method and fixed bug in Sale spec
kdefliese Oct 23, 2015
75feb45
Added parent class file but haven't hooked anything up to it yet
kdefliese Oct 23, 2015
b7c2bd9
Changed Market to inherit from FarMarParent class AND IT WORKED
kdefliese Oct 23, 2015
063564b
Refactored all classes to use the FarMar::FarMarParent class and did …
kdefliese Oct 23, 2015
9d4140e
Added self.search method to Market and specs for it
kdefliese Oct 23, 2015
3e69677
Updating end of Friday work
kdefliese 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 "./lib/far_mar/parent"
require "./lib/far_mar/market"
require "./lib/far_mar/vendor"
require "./lib/far_mar/product"
Expand Down
78 changes: 77 additions & 1 deletion lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,80 @@
require "csv"
require "pry"

module FarMar
class Market
attr_accessor :id, :name, :address, :city, :county, :state, :zip
class Market < FarMar::FarMarParent
def self.filepath
"./support/markets.csv"
end

def self.create_hash(market_array)
market_hash = {}
market_hash[:id] = market_array[0].to_i
market_hash[:name] = market_array[1]
market_hash[:address] = market_array[2]
market_hash[:city] = market_array[3]
market_hash[:county] = market_array[4]
market_hash[:state] = market_array[5]
market_hash[:zip] = market_array[6].to_i
return market_hash
end

def find_vendors(market_id)
vendors_array = []
FarMar::Vendor.all.each do |vendor|
if market_id == vendor.market_id
vendors_array.push(vendor)
end
end
return vendors_array
end

def find_products(market_id)
products_array = []
vendors = find_vendors(market_id)
vendors.each do |vendor|
products = vendor.find_products(vendor.id)
products_array.push(products)
end
return products_array.flatten
end

def name
return @name
end

def self.search(search_term)
#need to sanitize input, needs to be a string only
results = []
market_list = self.all.find_all do |market|
market_name = market.name.downcase
market_name.include?(search_term.downcase)
end
vendor_list = FarMar::Vendor.all.find_all do |vendor|
vendor_name = vendor.name.downcase
vendor_name.include?(search_term.downcase)
end
vendor_list.map! do |vendor|
FarMar::Vendor.find_market(FarMar::Vendor.market_id)
end
results.push(market_list)
results.push(vendor_list)
return results.flatten
end

# def preferred_vendor
# results = []
# # this is an array of vendor objects
# vendors = find_vendors(self.id)
# # this calculates the revenue per vendor
# vendors.each do |vendor|
# moniez = vendor.revenue(vendor.id)
# results.push([vendor, moniez])
# end
# # need to return highest revenue vendor
# results.
# end

end
end
32 changes: 32 additions & 0 deletions lib/far_mar/parent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "csv"

module FarMar
class FarMarParent
def initialize(init_hash)
# This block sets each key to an instance variable and assigns the value
init_hash.each do |k,v|
instance_variable_set("@#{k}", v) unless v.nil?
end
end

def self.all()
if !instance_variable_defined?("@all")
instance_variable_set("@all", CSV.read(self.filepath))
@all.map! do |line|
self.new(self.create_hash(line))
end
end
return @all
end


def self.find(id)
self.all.find {|object| object.id == id}
end

def id
return @id
end

end
end
51 changes: 50 additions & 1 deletion lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
module FarMar
class Product
class Product < FarMar::FarMarParent
attr_accessor :id, :name, :vendor_id

def self.filepath
"./support/products.csv"
end

def self.create_hash(product_array)
product_hash = {}
product_hash[:id] = product_array[0].to_i
product_hash[:name] = product_array[1]
product_hash[:vendor_id] = product_array[2].to_i
return product_hash
end

def vendor_id
return @vendor_id
end

def name
return @name
end

def find_vendor(vendor_id)
FarMar::Vendor.find(vendor_id)
end

def find_sales(product_id)
sales_array = []
FarMar::Sale.all.each do |sale|
if product_id == sale.product_id
sales_array.push(sale)
end
end
return sales_array
end

def number_of_sales(product_id)
sales = find_sales(product_id)
total = 0
sales.each {|sale| total += 1}
return total
end

def self.by_vendor(vendor_id)
all_products = self.all
#all_products is an array of Vendor objects
matches = all_products.find_all {|product| product.vendor_id == vendor_id}
return matches
end
end
end
39 changes: 38 additions & 1 deletion lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
module FarMar
class Sale
class Sale < FarMar::FarMarParent
attr_accessor :id, :amount, :purchase_time, :vendor_id, :product_id
def self.filepath
"./support/sales.csv"
end

def self.create_hash(sale_array)
sale_hash = {}
sale_hash[:id] = sale_array[0].to_i
sale_hash[:amount] = sale_array[1].to_i
sale_hash[:purchase_time] = DateTime.parse(sale_array[2])
sale_hash[:vendor_id] = sale_array[3].to_i
sale_hash[:product_id] = sale_array[4].to_i
return sale_hash
end

def amount
return @amount
end

def find_vendor(vendor_id)
FarMar::Vendor.find(vendor_id)
end

def find_product(product_id)
FarMar::Product.find(product_id)
end

def self.between(beginning_time, end_time)
# add input validation
matching_sales_array = []
self.all.find_all do |sale|
sale.purchase_time >= beginning_time && sale.purchase_time <= end_time
matching_sales_array.push(sale)
end
return matching_sales_array
end

end
end
65 changes: 64 additions & 1 deletion lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
module FarMar
class Vendor
class Vendor < FarMar::FarMarParent
attr_accessor :id, :name, :num_employees, :market_id
def self.filepath
"./support/vendors.csv"
end

def self.create_hash(vendor_array)
vendor_hash = {}
vendor_hash[:id] = vendor_array[0].to_i
vendor_hash[:name] = vendor_array[1]
vendor_hash[:num_employees] = vendor_array[2].to_i
vendor_hash[:market_id] = vendor_array[3].to_i
return vendor_hash
end

def market_id
return @market_id
end

def name
return @name
end

def find_market(market_id)
return FarMar::Market.find(market_id)
end

def find_products(vendor_id)
products_array = []
FarMar::Product.all.each do |product|
if vendor_id == product.vendor_id
products_array.push(product)
end
end
return products_array
end

def find_sales(vendor_id)
sales_array = []
FarMar::Sale.all.each do |sale|
if vendor_id == sale.vendor_id
sales_array.push(sale)
end
end
return sales_array
end

def revenue(vendor_id)
sales = find_sales(vendor_id)
#sales is an array of Sale objects
sum = 0
sales.each do |sale|
sum += sale.amount
end
return sum
end

def self.by_market(market_id)
all_vendors = self.all
#all_vendors is an array of Vendor objects
matches = all_vendors.find_all {|vendor| vendor.market_id == market_id}
return matches
end

end
end
Loading