Skip to content
Open
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/test/tmp/
/test/version_tmp/
/tmp/
/.DS_Store

## Specific to RubyMotion:
.dat*
.repl_history
build/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalisation:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
farmar
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList["specs/*_spec.rb"]

end

task default: :test
8 changes: 8 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'CSV'

module FarMar
require_relative "./lib/markets.rb"
require_relative "./lib/products.rb"
require_relative "./lib/sales.rb"
require_relative "./lib/vendors.rb"
end
114 changes: 114 additions & 0 deletions lib/markets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
class FarMar::Market
attr_accessor :id, :name, :address, :city, :county, :state, :zip
MARKET_DATA = "./support/markets.csv"

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
#returns a collection of instances, representing all of
#the objects described in the CSV
def self.all
all = []
CSV.foreach(MARKET_DATA, "r") do |line|
market = FarMar::Market.new(id: line[0].to_i, name: line[1],
address: line[2], city: line[3], county: line[4],
zip: line[6])
all << market
end

return all
end

#returns an instance of the object where the value of the
# id field in the CSV matches the passed parameter.
def self.find(id)
find = []
CSV.foreach(MARKET_DATA, "r") do |line|
if line[0] == id.to_s
find =[line[0].to_i, line[1], line[2], line[3],
line[4], line[5], line[6]]
return find
end
end
return nil
end

#returns a collection of FarMar::Vendor instances that
#are associated with the market by the market_id field.
def vendors
FarMar::Vendor.all.find_all do |vendor|
vendor.market_id == @id
end
end

#returns a collection of FarMar::Product instances that
#are associated to the market through the FarMar::Vendor class
def products
products = []
FarMar::Vendor.all.each do |vendor|
if vendor.market_id == @id
find_product = FarMar::Product.by_vendor(vendor.id)
find_product.each do |product|
products << product
end
end
end
return products
end

#returns a collection of FarMar::Market instances where the
#market name or vendor name contain the search_term
def self.search(search_term)
market_collection = []
markets = FarMar::Market.all
vendors = FarMar::Vendor.all
search_term = search_term.downcase

vendors.each do |vendor|
if vendor.name.downcase.include? search_term
markets.each do |market|
if vendor.market_id == market.id
market_collection << market
end
end
end
end

markets.each do |market|
if market.name.downcase.include? search_term
market_collection << market
end
end
return market_collection
end

#returns the vendor with the highest revenue
def prefered_vendor
best_vendor = []
sum = 0
max_sum = 0
FarMar::Vendor.all.each do |vendor|
if vendor.market_id == @id
sum = 0
FarMar::Sale.all.each do |sale|
if sale.vendor_id == vendor.id
sum += sale.amount
end
end

if sum > max_sum
max_sum = sum
best_vendor = vendor
end
end
end
return best_vendor
end
end
73 changes: 73 additions & 0 deletions lib/products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class FarMar::Product
attr_accessor :id, :name, :vendor_id
PRODUCT_DATA = "./support/products.csv"

def initialize(product_hash)
@id = product_hash[:id].to_i
@name = product_hash[:name]
@vendor_id = product_hash[:vendor_id].to_i

end
#returns a collection of instances, representing all of
#the objects described in the CSV
def self.all
all = []
CSV.foreach(PRODUCT_DATA, "r") do |line|
product = FarMar::Product.new(id: line[0].to_i, name: line[1],
vendor_id: line[2].to_i)
all << product
end
return all
end

#returns an instance of the object where the value of the
# id field in the CSV matches the passed parameter.
def self.find(id)
find = []
CSV.foreach(PRODUCT_DATA, "r") do |line|
if line[0] == id.to_s
find =[line[0].to_i, line[1], line[2].to_i]
return find
end
end
return nil
end

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

#returns a collection of FarMar::Sale instances that are
#associated using the FarMar::Sale product_id field.
def sales
FarMar::Sale.all.find_all do |sale|
sale.product_id == @id
end
end

#returns the number of times this product has been sold.
def number_of_sales
sum = 0
FarMar::Sale.all.each do |sale|
if sale.product_id == @id
sum += 1
end
end
return sum
end

#returns all of the products with the given vendor_id
def self.by_vendor(vendor_id)
products = []
CSV.foreach(PRODUCT_DATA, "r") do |line|
if line[2].to_i == vendor_id
products << FarMar::Product.new(id: line[0].to_i, name: line[1],
vendor_id: line[2].to_i)
end
end
return products
end

end
71 changes: 71 additions & 0 deletions lib/sales.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class FarMar::Sale
attr_accessor :id, :amount, :purchase_time, :vendor_id, :product_id
SALE_DATA = "./support/sales.csv"

def initialize(sale_hash)
@id = sale_hash[:id].to_i
@amount = sale_hash[:amount].to_i
@purchase_time = sale_hash[:purchase_time]
@vendor_id = sale_hash[:vendor_id].to_i
@product_id = sale_hash[:product_id].to_i

end

#returns a collection of instances, representing all of
#the objects described in the CSV
def self.all
all = []
CSV.foreach(SALE_DATA, "r") do |line|
sale = FarMar::Sale.new(id: line[0].to_i,
amount: line[1].to_i, purchase_time: DateTime.parse(line[2]),
vendor_id: line[3].to_i, product_id: line[4].to_i)
all << sale
end
return all
end

#returns an instance of the object where the value of the
# id field in the CSV matches the passed parameter.
def self.find(id)
find = []
CSV.foreach(SALE_DATA, "r") do |line|
if line[0] == id.to_s
find =[line[0].to_i, line[1].to_i, line[2].to_i,
line[3].to_i, line[4].to_i]
return find
end
end
return nil
end

#returns the FarMar::Vendor instance that is associated
# with this sale using the FarMar::Sale vendor_id field
def vendor
FarMar::Vendor.all.find do |vendor|
vendor.id == @vendor_id
end
end

#returns the FarMar::Product instance that is associated
#with this sale using the FarMar::Sale product_id field
def product
FarMar::Product.all.find do |product|
product.id == @product_id
end
end

#returns a collection of FarMar::Sale objects where the
#purchase time is between the two times given as arguments
def self.between(beginning_time, end_time)
sales = []
beginning_time = DateTime.parse(beginning_time)
end_time = DateTime.parse(end_time)
FarMar::Sale.all.find_all do |sale|
if (sale.purchase_time >= beginning_time) &&
(sale.purchase_time <= end_time)
sales << sale
end
end
return sales
end
end
Loading