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
64 changes: 63 additions & 1 deletion lib/far_mar/market_class.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,69 @@
require "pry"

module FarMar
class Market
def initialize

attr_accessor :id, :name, :address, :city, :county, :state, :zip

# Each individual market has many vendors associated with it. The `FarMar::Market` data, in order in the CSV, consists of:
#
# 1. ID - (Fixnum) a unique identifier for that market
# 2. Name - (String) the name of the market (not guaranteed unique)
# 3. Address - (String) street address of the market
# 4. City - (String) city in which the market is located
# 5. County - (String) county in which the market is located
# 6. State - (String) state in which the market is located
# 7. Zip - (String) zipcode in which the market is located

def initialize(id, name, address, city, county, state, zip)
@id = id
@name = name
@address = address
@city = city
@county = county
@state = state
@zip = zip
end

# - `self.all` - returns a collection of Market instances, representing all of the markets described in the CSV
def self.all
market_array = CSV.read("./support/markets.csv")
all_markets_array = []
market_array.each do |single_array|
id = single_array[0]
name = single_array[1]
address = single_array[2]
city = single_array[3]
county = single_array[4]
state = single_array[5]
zip = single_array[6]
all_markets_array.push(FarMar::Market.new(id, name, address, city, county, state, zip))
end
return all_markets_array
end

# # - `self.find(id)` - returns an instance of Market where the value of the `id` field in the CSV matches the passed parameter.

def self.find_market(market_id)
FarMar::Market.all.find do |single_market|
if single_market.id == market_id.to_s
return single_market
end
end
end

# - `vendors` - returns a collection of `FarMar::Vendor` instances that are associated with the market by the `market_id` field.

def vendors
var = []
FarMar::Vendor.all.find_all do |seller|
if seller.market_id == self.id.to_s
var.push(seller)
end #close if
end #close do
#return array of vendor instances
return var
end

end
end
79 changes: 77 additions & 2 deletions lib/far_mar/product_class.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,89 @@
module FarMar
class Product

attr_accessor :id, :name, :vendor_id

# Each product belongs to a vendor. The `vendor_id` field refers to the `FarMar::Vendor` ID field. The `FarMar::Product` data, in order in the CSV, consists of:
#
# 1. ID - (Fixnum) uniquely identifies the product
# 2. Name - (String) the name of the product (not guaranteed unique)
# 3. Vendor_id - (Fixnum) a reference to which vendor sells this product

def initialize
array_of_product = CSV.read("./support/products.csv")
def initialize(id, name, vendor_id)
@id = id
@name = name
@vendor_id = vendor_id
end

# - `self.all` - returns a collection of Product instances, representing all of the products described in the CSV
def self.all
product_array = CSV.read("./support/products.csv")
all_products_array = []
product_array.each do |single_array|
id = single_array[0]
name = single_array[1]
vendor_id = single_array[2]
all_products_array.push(self.new(id, name, vendor_id))
end
return all_products_array
end

# - `self.find(id)` - returns an instance of Product where the value of the `id` field in the CSV matches the passed parameter.
def self.find_product(product_id)
self.all.find do |single_product|
if single_product.id == product_id.to_s
return single_product
end
end
end

# - `find_product_things` - returns and array of all of the product item instances from a given class

def find_product_things(pd_id)
products_items = []
FarMar::Sale.all.find_all do |single_item|
if single_item.product_id == pd_id.to_s
products_items.push(single_item)
end
end
return products_items
end

# - `vendor` - returns the `FarMar::Vendor` instance that is associated with this vendor using the `FarMar::Product` `vendor_id` field

def vendor(prod_id)
prod = FarMar::Product.find_product(prod_id)
vendor = FarMar::Vendor.find_vendor(prod.vendor_id)
return vendor[0]
end

# - `sales` - returns a collection of `FarMar::Sale` instances that are associated using the `FarMar::Sale` `product_id` field.

def sales(prod_id)
find_product_things(prod_id)
end

# - `number_of_sales` - returns the number of times this product has been sold.

def number_of_sales(prod_id)
find_product_things(prod_id).length
end

# - `self.by_vendor(vendor_id)` - returns all of the products with the given `vendor_id`

def self.by_vendor(vend_id)
var = []
#passes to the variable item each of the vendor instances in turn
self.all.find_all do |item|
# compares the vendor id of a product instance to the vendor id we have taken in as a parameter
if item.vendor_id == vend_id.to_s
#pushes the vendor instances that pass the comparison to the var array
var.push(item)
end #close if
end #close do
#return array of vendor instances
return var
end

end
end
76 changes: 75 additions & 1 deletion lib/far_mar/sale_class.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module FarMar
class Sale

attr_accessor :id, :amount, :purchase_time, :vendor_id, :product_id

# Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id` fields refer to the `FarMar::Vendor` and `FarMar::Product` ID fields, respectively. The `FarMar::Sale` data, in order in the CSV, consists of:
#
# 1. ID - (Fixnum) uniquely identifies the product
Expand All @@ -9,8 +11,80 @@ class Sale
# 4. Vendor_id - (Fixnum) a reference to which vendor completed the sale
# 5. Product_id - (Fixnum) a refere

def initialize
def initialize(id, amount, purchase_time, vendor_id, product_id)
@id = id
@amount = amount
@purchase_time = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z")
@vendor_id = vendor_id
@product_id = product_id
end

# - `self.all` - returns a collection of Sale instances, representing all of the markets described in the CSV
def self.all
array_of_sale = CSV.read("./support/sales.csv")
all_sales_array = []
array_of_sale.each do |single_sale|
id = single_sale[0]
amount = single_sale[1]
purchase_time = single_sale[2]
vendor_id = single_sale[3]
product_id = single_sale[4]
all_sales_array.push(FarMar::Sale.new(id, amount, purchase_time, vendor_id, product_id))
end
return all_sales_array
end

# - `self.find(id)` - returns an instance of Sale where the value of the `id` field in the CSV matches the passed parameter.
def self.find_sale(sale_id)
all_the_sales = self.all
all_the_sales.find do |single_sale|
if single_sale.id == sale_id.to_s
return single_sale
end
end
end

# - `find_sale_things` - returns and array of all of the sale item related instances from a given class

def find_sale_things(class_type)
case class_type
when "product"
item_array = FarMar::Product.all
compare = self.product_id.to_s
when "vendor"
item_array = FarMar::Vendor.all
compare = self.vendor_id.to_s
end
item_array.find_all do |sale_item|
if sale_item.id == compare
return sale_item
end
end
end


# - `vendor` - returns the `FarMar::Vendor` instance that is associated with this sale using the `FarMar::Sale` `vendor_id` field
def vendor
find_sale_things("vendor")
end

# - `product` - returns the `FarMar::Product` instance that is associated with this sale using the `FarMar::Sale` `product_id` field

def product
find_sale_things("product")
end

# - `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

def self.between(begin_time, end_time)
time_array = []
self.all.find_all do |purchase|
if begin_time <= purchase.purchase_time && end_time >= purchase.purchase_time
time_array.push(purchase)
end
end
return time_array
end

end
end
89 changes: 87 additions & 2 deletions lib/far_mar/vendor_class.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module FarMar
class Vendor

attr_accessor :id, :name, :employees_count, :market_id

# Each vendor belongs to a market, the `market_id` field refers to the `FarMar::Market` ID field.
# Each vendor has many products for sell. The `FarMar::Vendor` data, in order in the CSV, consists of:
#
Expand All @@ -9,8 +11,91 @@ class Vendor
# 3. No. of Employees - (Fixnum) How many employees the vendor has at the market
# 4. Market_id - (Fixnum) a reference to which market the vendor attends

def initialize
array_of_vendor = CSV.read("./support/vendors.csv")
def initialize(id, name, employees_count, market_id)
@id = id
@name = name
@employees_count = employees_count
@market_id = market_id
end

# - `self.all` - returns a collection of Vendor instances, representing all of the markets described in the CSV
def self.all
vendor_array = CSV.read("./support/vendors.csv")
all_vend_instances = []
vendor_array.each do |single_vend|
id = single_vend[0]
name = single_vend[1]
employees_count = single_vend[2]
market_id = single_vend[3]
all_vend_instances.push(self.new(id, name, employees_count, market_id))
end
return all_vend_instances
end

# - `self.find(id)` - returns an instance of Vendor where the value of the `id` field in the CSV matches the passed parameter.
def self.find_vendor(vend_id)
var = []
self.all.find_all do |single_vend|
if single_vend.id == vend_id.to_s
var.push(single_vend)
return var
end
end
end

# - `find_vend_things` - returns an array of all of the vendor item instances from a given class
def find_vend_things(vend_id, class_name)
case class_name
when "product"
item_array = FarMar::Product.all
when "sale"
item_array = FarMar::Sale.all
end
vendors_items = []
item_array.find_all do |single_item|
if single_item.vendor_id == vend_id.to_s
vendors_items.push(single_item)
end
end
return vendors_items
end

# - `market` - returns the `FarMar::Market` instance that is associated with this vendor using the `FarMar::Vendor` `market_id` field
def market(vend_id)
vend = FarMar::Vendor.find_vendor(vend_id)
market = FarMar::Market.find_market(vend[0].market_id)
return market
end

# - `products` - returns a collection of `FarMar::Product` instances that are associated by the `FarMar::Product` `vendor_id` field.
def products(vend_id)
find_vend_things(vend_id, "product")
end

# - `sales` - returns a collection of `FarMar::Sale` instances that are associated by the `vendor_id` field.
def sales(vend_id)
find_vend_things(vend_id, "sale")
end

# - `revenue` - returns the the sum of all of the vendor's sales (in cents)
def revenue(vend_id)
total_sales = 0
sales(vend_id).each do |money|
total_sales += money.amount.to_i
end
return total_sales
end

# # - `self.by_market(market_id)` - returns all of the vendors with the given `market_id`
def self.by_market(mkt_id)
var = []
FarMar::Vendor.all.find_all do |seller|
if seller.market_id == mkt_id.to_s
var.push(seller)
end
end
return var
end

end
end
28 changes: 25 additions & 3 deletions spec/far_mar/markets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@
require "./lib/far_mar/market_class.rb"

describe FarMar::Market do

describe "initialize"
before :each do
@market_test = FarMar::Market.new
end
it "creates a new instace of the market class" do
@market_test = FarMar::Market.new(0, "name", "1234 street", "city", "county", "state", 12345)
expect(@market_test).to be_an_instance_of FarMar::Market
end

describe "self.all"
it "returns an array" do
expect(FarMar::Market.all).to be_an Array
end
it "returns all markets in the csv file" do
expect(FarMar::Market.all.count).to eq 500
end

describe "self.find"
it "returns the information for a given market ID" do
expect(FarMar::Market.find_market(303).zip).to eq "35804"
end

describe "vendors"
it "returns a collection" do
@market_test = FarMar::Market.new("7", "Petaluma Evening Farmers' Market", "1 2nd Street", "Petaluma", "Sonoma", "California", "94952")
expect(@market_test.vendors).to be_an Array
end
it "returns all vendors for a given market id" do
@market_test = FarMar::Market.new("7", "Petaluma Evening Farmers' Market", "1 2nd Street", "Petaluma", "Sonoma", "California", "94952")
expect(@market_test.vendors.length).to eq 2
end
end
Loading