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 Final #37

Open
wants to merge 23 commits into
base: cas/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3f618e4
Added Market and Vendor classes and corressponding specs
CShekta Oct 20, 2015
0fa592f
updated rspecs and vendor class
CShekta Oct 21, 2015
1f41a81
Added market and product methods to vendor class
CShekta Oct 21, 2015
0afe8b7
added revenue method and self.by_market methods <--currently broken
CShekta Oct 21, 2015
6554df3
added vendor and sales methods to product class
CShekta Oct 21, 2015
26913cd
finished methods in the product class
CShekta Oct 21, 2015
ef6c180
added vendor and product methods for sale class
CShekta Oct 21, 2015
4d1f023
Adding self.between method for sale class
CShekta Oct 21, 2015
9888e82
bug fixing self.by_market and sales self.between
CShekta Oct 22, 2015
f50c757
Added optional requirements products and prefered_vendor for market c…
CShekta Oct 22, 2015
3fc6d20
Fixed market specs and broke self.search making regexp
CShekta Oct 22, 2015
55f8470
update market and vendor specs
CShekta Oct 22, 2015
99bd6f7
added more specs
CShekta Oct 22, 2015
7126c46
product specs
CShekta Oct 22, 2015
c41f50f
sales specs update. all 35 specs coverage 100% & passing
CShekta Oct 22, 2015
1419696
updates optional I methods market
CShekta Oct 22, 2015
5899204
started preferred_vendor method and spec; updated market.all method w…
CShekta Oct 22, 2015
f290d58
Improved .all methods of all classes for efficiency and time. Refacto…
CShekta Oct 23, 2015
33828fa
Added comments and bug fixes in the between method w/ specs
CShekta Oct 23, 2015
53cbc43
added comments for market and vendor
CShekta Oct 23, 2015
ec93374
Added comments for product and sale classes
CShekta Oct 23, 2015
9dd40b4
adding more market specs
CShekta Oct 23, 2015
5cdeaf3
Added specs for product and sales classes
CShekta 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,3 +1,4 @@
require "pry"
require "csv"
require "time"
require "./lib/far_mar/market"
Expand Down
101 changes: 100 additions & 1 deletion lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,106 @@
require 'pry'

module FarMar
class Market
def initialize
attr_accessor :id, :name, :address, :city, :county, :state, :zip
def initialize(market_hash)
@id = market_hash[:id].to_i
@name = market_hash[:name]
@address = market_hash[:address]
@city = market_hash[:city]
@county = market_hash[:county]
@state = market_hash[:state]
@zip = market_hash[:zip]
end

#Creates a class variable to store csv values in order to save time reading while running the program multiple times. Method reads CSV file and uses .map to create a .new object for each line, returning the whole array of objects
def self.all
if @market_array.nil?|| @market_array.empty?
@market_array = CSV.read("support/markets.csv").map do |line|
Market.new(
id: line[0],
name: line[1],
address: line[2],
city: line[3],
county: line[4],
state: line[5],
zip: line[6])
end
end
return @market_array
end

#Find converts the csv file to an array, then uses .find to locate where the first value of each array in each line is equal to the id passed through as the argument. It then initializes an object with the values of the array on that idenitfied line.
def self.find(id)
market_array = CSV.read("support/markets.csv")
matched_line = market_array.find do |line|
line[0].to_i == id
end
return specific_market = Market.new(
id: matched_line[0],
name: matched_line[1],
address: matched_line[2],
city: matched_line[3],
county: matched_line[4],
state: matched_line[5],
zip: matched_line[6])
end

#Calls the .all method of Vendors and parses through each vendor (already instantiated via the all method), finding all vendors where the id of the market matches the market id of the vendor.
def vendors
possibilities = FarMar::Vendor.all
associated_vendors = possibilities.find_all do |each|
@id == each.market_id
end
return associated_vendors
end

#Creates an array of all_products, then calls the vendor method on itself to return all the associated vendors. For each vendor, it calls the vendor product method which finds all the products sold by the vendor. For each product it checks if that product is already included on all_products. If it is not, it pushes the product to the mass array. The method return all_products, consisting of every product of every vendor at the market, with no duplicates.
def products
all_products = []
self.vendors.each do |vendor|
vendor.products.each do |product|
all_products.push(product) if !all_products.include?(product)
end
end
return all_products
end

#This creates an array of results, then goes through all the names of all the markets and vendors. If the name of either of those instances matches the search term (using regexp and /i for case insensitivity), if will push that instance to the results and return it.
def self.search(search_term)
results = []
FarMar::Market.all.find_all do |market|
results.push(market) if market.name.match(/#{search_term}/i)
end
FarMar::Vendor.all.find_all do |vendor|
results.push(vendor.market) if vendor.name.match(/#{search_term}/i)
end
return results
end

#This calls the vendor method on itself, returning all vendors associated with the market. Then it sorts the vendors least to greatest by their revenue, a method in the vendor class. Finally, the method returns the last vendor of that group, or the vendor with the greatest revenue.
def pref_vendor
return self.vendors.sort_by { |vendor| vendor.revenue}.last
end

#This method starts be going through each vendor of the market. For each vendor, it calls sales, which returns all sales associated with the vendor. Next, for every sale it checks if the purchase_time is equal to the date passed through as an argument. If it is, it calls amount on that sale and adds it to the variable revenue. After the loops cycling through adding the sales of each vendor on a certain date, the method compares to see if the revenue on that date calculated is greater than the max_revenue, which startes at 0. If it is, it sets itself as max revenue and the vendor in question as the max_vendor. Thus, for each vendor it compares their revenue with the max revenue and max vendor of all the vendors the loops has already iterated, returning the overall max_vendor.
def preferred_vendor(date)
max_rev = 0
max_vendor = nil
self.vendors.each do |vendor|
rev = 0
vendor.sales.each do |sale|
if sale.purchase_time.to_date == date
rev += sale.amount
end
end
#puts vendor
if rev > max_rev
max_rev = rev
max_vendor = vendor
end
end
return max_vendor
end
end
end
55 changes: 54 additions & 1 deletion lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,60 @@
module FarMar
class Product
def initialize
attr_accessor :id, :name, :vendor_id
def initialize(product_hash)
@id = product_hash[:id].to_i
@name = product_hash[:name]
@vendor_id = product_hash[:vendor_id].to_i
end

#Creates a class variable to store csv values in order to save time reading while running the program multiple times. Method reads CSV file and uses .map to create a .new object for each line, returning the whole array of objects
def self.all
if @product_array.nil? || @product_array.empty?
@product_array = CSV.read("support/products.csv").map do |line|
Product.new(
id: line[0],
name: line[1],
vendor_id: line[2])
end
end
return @product_array
end

#Find converts the csv file to an array, then uses .find to locate where the first value of each array in each line is equal to the id passed through as the argument. It then initializes an object with the values of the array on that idenitfied line.
def self.find(id)
product_array = CSV.read("support/products.csv")
matched_line = product_array.find do |line|
line[0].to_i == id
end
return specific_product = Product.new(
id: matched_line[0],
name: matched_line[1],
vendor_id: matched_line[2])
end

#Passes the vendor_id of the product to the vendor find method in order to return the vendor that sells this product.
def vendor
return FarMar::Vendor.find(@vendor_id)
end

#Creates an array of all the sales in order to find every sale in which the id of the product matches the product id of the sale. Returns an array of all of those matches.
def sales
all_sales = FarMar::Sale.all.find_all do |each|
@id == each.product_id
end
return all_sales
end

#Calls the sales method and calculates the length of that array, or how many sales are associated with this product.
def number_of_sales
self.sales.length
end

#Goes through an array of all the products to find where the vendor id of the product is equal to the vendor_id passed through as a parameter.
def self.by_vendor(vendor_id)
return FarMar::Product.all.find_all do |each|
vendor_id == each.vendor_id
end
end
end
end
57 changes: 56 additions & 1 deletion lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
module FarMar
class Sale
def initialize
attr_accessor :id, :amount, :purchase_time, :vendor_id, :product_id
def initialize(sale_hash)
@id = sale_hash[:id].to_i
@amount = sale_hash[:amount].to_i
@purchase_time = DateTime.parse(sale_hash[:purchase_time])
@vendor_id = sale_hash[:vendor_id].to_i
@product_id = sale_hash[:product_id].to_i
end

#Creates a class variable to store csv values in order to save time reading while running the program multiple times. Method reads CSV file and uses .map to create a .new object for each line, returning the whole array of objects
def self.all
if @sales_array.nil? || @sales_array.empty?
@sales_array = CSV.read("support/sales.csv").map do |line|
Sale.new(
id: line[0],
amount: line[1],
purchase_time: line[2],
vendor_id: line[3],
product_id: line[4])
end
end
return @sales_array
end

#Find converts the csv file to an array, then uses .find to locate where the first value of each array in each line is equal to the id passed through as the argument. It then initializes an object with the values of the array on that idenitfied line.
def self.find(id)
sales_array = CSV.read("support/sales.csv")
matched_line = sales_array.find do |line|
line[0].to_i == id
end
return Sale.new(
id: matched_line[0],
amount: matched_line[1],
purchase_time: matched_line[2],
vendor_id: matched_line[3],
product_id: matched_line[4])
end

#This passes the vendor_id of the sale as an argument to the vendor.find method in order to return the vendor to which the sale refers.
def vendor
return FarMar::Vendor.find(@vendor_id)

end

#This passes the product_id of the sale as an argument to the product find method in order to to return the product to which the sale refers.
def product
return FarMar::Product.find(@product_id)
end

#This method first tries to sanitize the arguments of time passed through; if they are strings it converts them to DateTimes. Next, it creates an array of all the sales and compares the purchase time of each sale to see if its value is between the values of the argument. It returns an array of all the sales that match that condition.
def self.between(beginning_time, end_time)
beginning_time = DateTime.parse(beginning_time) if beginning_time.class == String
end_time = DateTime.parse(end_time) if end_time.class == String
matched_sales = self.all.find_all do |each|
each.purchase_time >= beginning_time && each.purchase_time <= end_time
end
return matched_sales
end
end
end
72 changes: 71 additions & 1 deletion lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,77 @@
module FarMar
class Vendor
def initialize
attr_accessor :id, :name, :employee_no, :market_id
def initialize(vendor_hash)
@id = vendor_hash[:id].to_i
@name = vendor_hash[:name]
@employee_no = vendor_hash[:employee_no].to_i
@market_id = vendor_hash[:market_id].to_i
end

#Creates a class variable to store csv values in order to save time reading while running the program multiple times. Method reads CSV file and uses .map to create a .new object for each line, returning the whole array of objects
def self.all
if @vendor_array.nil? || @vendor_array.empty?
@vendor_array = CSV.read("support/vendors.csv").map do |line|
Vendor.new(
id: line[0],
name: line[1],
employee_no: line[2],
market_id: line[3])
end
end
return @vendor_array
end

#Goes through every line of the CSV file and compares the first value [0], the id, to the argument passed through the method, creating a new object with the data where those two valued are equivalent.
def self.find(id)
vendor_array = CSV.read("support/vendors.csv")
matched_line = vendor_array.find do |line|
line[0].to_i == id
end
return specific_vendor = Vendor.new(
id: matched_line[0],
name: matched_line[1],
employee_no: matched_line[2],
market_id: matched_line[3])
end

#Uses the Market find method passing the argument of the vendor's market id, which is in fact the id of the market.
def market
return FarMar::Market.find(@market_id)
end

#Creates an array of all the products, then uses find all to identify when the id of the vendor matches the vendor id of the product. Returns an array of all such matches
def products
possibilities = FarMar::Product.all
associated_products = possibilities.find_all do |each|
@id == each.vendor_id
end
return associated_products
end

#Creates an array of all sales, then uses find all to identify when the id of the vendor matches the vendor id of the sale. Returns an array of all such matches.
def sales
possibilities = FarMar::Sale.all
associated_sales = possibilities.find_all do |each|
@id == each.vendor_id
end
return associated_sales
end

#Calls sales on the particular vendor, which returns an array of all sales the vendor has made. For each sale, it iterates through and adds the amount of that sale to the revenue variable. Then it returns the total revenue, or sum of every sale.
def revenue
revenue = 0
self.sales.each do |sale|
revenue += sale.amount.to_i
end
return revenue
end

#Starts with an array of all vendors using the .all method, then employs find_all to find where the market id passes as an argument equals the market id of each vendor of every iteration. It returns an array of vendors of all of these matches.
def self.by_market(market_id)
return self.all.find_all do |each|
market_id == each.market_id
end
end
end
end
Loading