Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/coverage
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
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; end

require_relative './lib/market'
require_relative './lib/vendor'
require_relative './lib/product'
require_relative './lib/sale'
47 changes: 47 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module FarMar
class Market
attr_reader :id, :name, :address, :city, :county, :state, :zip

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
market_array = []
CSV.read("/Users/yasminor/ada/Week_5/FarMar/support/markets.csv").each_with_index do |line, i|
market_array[i] = self.new(line[0], line[1], line[2], line[3], line[4], line[5], line[6])
end
return market_array
end

def self.find(id)
raise ArgumentError.new("invalid input type") unless id.is_a? (Fixnum)
object_array = FarMar::Market.all
object_array.map do |market|
if market.id.to_i == id
return market
end
end
end

def vendors #returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field
FarMar::Vendor.by_market(@id)
# market_vendors_array = []
# vendor_array = FarMar::Vendor.all #the id matches the market instance's id and the market_id of the vendors
# vendor_array.map do |vendor|
# if vendor.market_id == @id.to_s
# market_vendors_array << vendor
# end
# end
# return market_vendors_array
end

end
end
87 changes: 87 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

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
product_array = []
CSV.read("/Users/yasminor/ada/Week_5/FarMar/support/products.csv").each_with_index do |line, i|
product_array[i] = self.new(line[0].to_i, line[1], line[2].to_i)
end
return product_array
end

def self.find(id)
raise ArgumentError.new("invalid input type") unless id.is_a? (Fixnum)
all_products = FarMar::Product.all
all_products.each do |product|
if product.id == id
return product
end
end
end

def vendor # returns the Vendor instance that is associated with a Product instance
FarMar::Vendor.find(@vendor_id)
end

def sales # returns a collection of Sale instances that are associated with a Product instance
product_sales_array = []
all_sales = FarMar::Sale.all
all_sales.map do |sale|
if sale.product_id == @id
product_sales_array << sale
end
end
return product_sales_array
end

def number_of_sales # returns the number of times this product has been sold
a_products_sales = []
all_sales = FarMar::Sale.all
all_sales.each do |sale|
if sale.product_id == @id
a_products_sales << sale
end
end
return a_products_sales.length
end

def self.by_vendor(vendor_id) # returns all of the products with the given vendor_id
vendor_products = []
all_products = FarMar::Product.all
all_products.map do |product|
if product.vendor_id == vendor_id
vendor_products << product
end
return vendor_products
end
end




















end
end
53 changes: 53 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module FarMar
class Sale
attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id

def initialize(id, amount, purchase_time, vendor_id, product_id)
@id = id
@amount = amount
@purchase_time = purchase_time
@vendor_id = vendor_id
@product_id = product_id
end

def self.all
sale_array = []
CSV.read("/Users/yasminor/ada/Week_5/FarMar/support/sales.csv").each_with_index do |line, i|
sale_array[i] = self.new(line[0].to_i, line[1].to_i, DateTime.parse(line[2]), line[3].to_i, line[4].to_i)
end
return sale_array
end

def self.find(id)
raise ArgumentError.new("invalid input type") unless id.is_a? (Fixnum)
all_sales = FarMar::Sale.all
all_sales.each do |sale|
if sale.id == id
return sale
end
end
end

def vendor # returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field
#use the FarMar::Vendor.find method
FarMar::Vendor.find(@vendor_id)
end

def product # returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field
FarMar::Product.find(@product_id)
end

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


end
end
78 changes: 78 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
module FarMar
class Vendor
attr_reader :id, :name, :no_of_employees, :market_id

def initialize(id, name, no_of_employees, market_id)
@id = id
@name = name
@no_of_employees = no_of_employees
@market_id = market_id
end

def self.all
vendor_array = []
CSV.read("/Users/yasminor/ada/Week_5/FarMar/support/vendors.csv").each_with_index do |line, i|
vendor_array[i] = self.new(line[0].to_i, line[1], line[2].to_i, line[3].to_i)
end
return vendor_array
end

def self.find(id)
raise ArgumentError.new("invalid input type") unless id.is_a? (Fixnum)
vendor_obj_array = FarMar::Vendor.all
vendor_obj_array.map do |vendor|
if vendor.id == id
return vendor
end
end
end

def market # returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field
FarMar::Market.find(@market_id)
end

def products #returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field
FarMar::Product.by_vendor(@id)
end

def sales # returns a collection of FarMar::Sale instances that are associated by the vendor_id field
vendor_sales_array = []
sales_list = FarMar::Sale.all
sales_list.map do |sale|
if sale.vendor_id == @id
vendor_sales_array << sale
end
end
return vendor_sales_array
end

def revenue # returns the sum of all of a particular vendor's sales
vendor_sales_array = []
sales_list = FarMar::Sale.all
sales_list.map do |sale|
if sale.vendor_id == @id
vendor_sales_array << sale
end
end

amounts_array = []
vendor_sales_array.map do |vendor_sale|
amounts_array << vendor_sale.amount
end

sum = amounts_array.reduce(:+).to_i
end

def self.by_market(market_id) # returns all the vendors with a given market_id
market_vendors = []
all_vendors = FarMar::Vendor.all
all_vendors.map do |vendor|
if vendor.market_id == market_id
market_vendors << vendor
end
end
return market_vendors
end

end
end
Loading