Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
dc5c1b2
adding #self.all and #self.find methods and tests for market
brittanykohler Oct 19, 2015
b0544c3
adding #self.all and #self.id methods and tests for Product class
brittanykohler Oct 19, 2015
e4d3f7a
adding initialize, #self.all, and #self.find methods and tests for Sa…
brittanykohler Oct 20, 2015
e78d23c
adding initialize, #self.all, and #self.find methods and tests for Ve…
brittanykohler Oct 20, 2015
309d81b
adding #vendors method and tests to Market class
brittanykohler Oct 20, 2015
4d97c44
Adding #market method and tests to Vendor class
brittanykohler Oct 20, 2015
64f8819
Fixing #market method in Vendor class
brittanykohler Oct 20, 2015
a4306f3
Adding #products method and tests for Vendor class
brittanykohler Oct 20, 2015
93b3142
Adding #sales method and tests for Vendor class
brittanykohler Oct 21, 2015
39f63d6
Adding #revenue method and tests for Vendor class
brittanykohler Oct 21, 2015
b0c5297
Adding #self.by_market(market_id) method and tests for Vendor class
brittanykohler Oct 21, 2015
174f26e
Adding #vendor method and tests for Product class
brittanykohler Oct 21, 2015
d1542f2
Adding additional tests in vendor_spec and product_spec to check mult…
brittanykohler Oct 21, 2015
3f95645
Adding #sales method and tests for Product class
brittanykohler Oct 21, 2015
5c1cd72
Adding #number_of_sales method and tests for Product class
brittanykohler Oct 21, 2015
8db671a
Adding #self.by_vendor(vendor_id) method and tests for Product class
brittanykohler Oct 21, 2015
0e0ecaa
Adding #vendor method and tests to Sale class
brittanykohler Oct 21, 2015
88f04df
Adding #product method and tests for Sale class
brittanykohler Oct 21, 2015
22e9c79
Adding #self.between(beginning_time, end_time) method and tests for S…
brittanykohler Oct 21, 2015
d107fee
Completion of Primary Requirements
brittanykohler Oct 21, 2015
cd7b58f
Updating all self.all methods to use instance variables on the class,…
brittanykohler Oct 21, 2015
004c918
Adding #products method and tests for Market class
brittanykohler Oct 21, 2015
494e69b
Adding #self.search(search_term) method and tests to Market class
brittanykohler Oct 22, 2015
4305a6f
Refactoring #products method for Market class
brittanykohler Oct 22, 2015
8cf2c8f
Adding #preferred_vendor method and tests for Market class
brittanykohler Oct 22, 2015
71b579b
Adding #worst_vendor and #worst_vendor_date methods and tests to Mark…
brittanykohler Oct 22, 2015
f6ac932
Adding self.most_revenue(n) method and tests for Vendor class
brittanykohler Oct 22, 2015
5321c0a
Refactoring self.search(search_term) method in Market class
brittanykohler Oct 22, 2015
1c95600
Adding self.most_items(n) method and tests for Vendor class
brittanykohler Oct 22, 2015
524ed7e
Refactoring preferred_vendor and worst_vendor methods in Market class
brittanykohler Oct 23, 2015
fe6370a
Adding self.revenue(date) method and tests for Vendor class
brittanykohler Oct 23, 2015
29e0ae5
Also adding revenue(date) method for Vendor class
brittanykohler Oct 23, 2015
476d8c8
Fixing unfortunate tabbing issues in sale_spec
brittanykohler Oct 23, 2015
eb76d8a
Adding self.most_revenue(n) for Product class
brittanykohler Oct 23, 2015
1858f93
fixing spacing issues
brittanykohler 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
59 changes: 59 additions & 0 deletions lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
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
if @all_markets.nil? || @all_markets.empty?
@all_markets = CSV.read("support/markets.csv").map do |line|
FarMar::Market.new(line[0].to_i, line[1], line[2], line[3], line[4], line[5], line[6])
end
end
return @all_markets
end

def self.find(id)
match = CSV.open("support/markets.csv").find { |n| n[0].to_i == id}
return FarMar::Market.new(match[0].to_i, match[1], match[2], match[3], match[4], match[5], match[6])
end

#Returns a collection of Vendors associated with the market's id
def vendors
return FarMar::Vendor.all.find_all {|vendor| vendor.market_id == @id}
end

#returns all products sold at the specific market
def products
all_products = []
vendors.each do |vendor|
vendor.products.each do |product|
all_products << product
end
end
return all_products
end

def self.search(search_term)
markets = []
FarMar::Vendor.all.find_all do |vendor|
markets.push(vendor.market) if vendor.name.match(/#{search_term}/i)
end
FarMar::Market.all.find_all do |market|
markets.push(market) if market.name.match(/#{search_term}/i)
end
return markets
end

def preferred_vendor(date = nil)
return vendors.max_by {|vendor| vendor.revenue(date)}
end

def worst_vendor(date = nil)
return vendors.min_by {|vendor| vendor.revenue(date)}
end
end
end
51 changes: 51 additions & 0 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
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
if @all_products.nil? || @all_products.empty?
@all_products =CSV.read("support/products.csv").map do |line|
FarMar::Product.new(line[0].to_i, line[1], line[2].to_i)
end
end
return @all_products
end

def self.find(id)
match = CSV.open("support/products.csv").find { |n| n[0].to_i == id}
return FarMar::Product.new(match[0].to_i, match[1], match[2].to_i)
end

def vendor
return FarMar::Vendor.all.find {|vendor| vendor.id == @vendor_id}
end

def sales
return FarMar::Sale.all.find_all {|sale| sale.product_id == @id}
end

def number_of_sales
return sales.length
end

def self.by_vendor(vendor_id)
vendor = FarMar::Vendor.find(vendor_id)
return vendor.products
end

def revenue
total_revenue = 0
sales.each do |sale|
if sale.product_id == @id
total_revenue += sale.amount
end
end
return total_revenue
end

def self.most_revenue(n)
return FarMar::Product.all.max_by(n) {|product| product.revenue}
end
end
end
35 changes: 35 additions & 0 deletions lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
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 = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z")
@vendor_id = vendor_id
@product_id = product_id
end

def self.all
if @all_sales.nil? || @all_sales.empty?
@all_sales = CSV.read("support/sales.csv").map do |line|
FarMar::Sale.new(line[0].to_i, line[1].to_i, line[2], line[3].to_i, line[4].to_i)
end
end
return @all_sales
end

def self.find(id)
match = CSV.open("support/sales.csv").find { |n| n[0].to_i == id}
return FarMar::Sale.new(match[0].to_i, match[1].to_i, match[2].to_s, match[3].to_i, match[4].to_i)
end

def vendor
return FarMar::Vendor.all.find {|vendor| vendor.id == @vendor_id}
end

def product
return FarMar::Product.all.find {|product| product.id == @product_id}
end

def self.between(beginning_time, end_time)
self.all.find_all {|sale| sale.purchase_time.between?(beginning_time, end_time)}
end

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

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

def self.all
if @all_vendors.nil? || @all_vendors.empty?
@all_vendors =CSV.read("support/vendors.csv").map do |line|
FarMar::Vendor.new(line[0].to_i, line[1], line[2].to_i, line[3].to_i)
end
end
return @all_vendors
end

def self.find(id)
match = CSV.open("support/vendors.csv").find { |n| n[0].to_i == id}
return FarMar::Vendor.new(match[0].to_i, match[1], match[2].to_i, match[3].to_i)
end

def market
return FarMar::Market.all.find {|market| market.id == @market_id}
end

def products
return FarMar::Product.all.find_all {|product| product.vendor_id == @id}
end

def sales
return FarMar::Sale.all.find_all {|sale| sale.vendor_id == @id}
end

def revenue(date = nil)
date = Date.parse(date) if !date.nil?
total_revenue = 0
sales.each do |sale|
if sale.purchase_time.to_date == date || date.nil?
total_revenue += sale.amount
end
end
return total_revenue
end

def self.by_market(market_id)
market = FarMar::Market.find(market_id)
return market.vendors
end

def self.most_revenue(n)
return FarMar::Vendor.all.max_by(n) {|vendor| vendor.revenue}
end

def self.most_items(n)
return FarMar::Vendor.all.max_by(n) {|vendor| vendor.products.length}
end

def self.revenue(date)
date = Date.parse(date)
total_revenue = 0
FarMar::Sale.all.each do |sale|
if sale.purchase_time.to_date == date
total_revenue += sale.amount
end
end
return total_revenue
end
end
end
90 changes: 84 additions & 6 deletions spec/far_mar/market_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,89 @@
require "./spec/spec_helper"

describe "initialize" do
before :each do
@market = FarMar::Market.new
end
describe FarMar::Market do
describe "initialize" do
before :each do
@market = FarMar::Market.new(1, "People's Co-op Farmers Market", "30th and Burnside", "Portland", "Multnomah", "Oregon", "97202")
@market2 = FarMar::Market.new(54, "Crescent City Farmers Market", "1 South Summit Street", "Crescent City", "Putnam", "Florida", "32112")
@market3 = FarMar::Market.new(2, "Silverdale Farmers Market", "98383", "Silverdale", "Kitsap", "Washington", "98383")
end

it "creates an instance of Farmar::Market class" do
expect(@market).to be_an_instance_of FarMar::Market
end

describe "#self.all" do
it "returns an Array" do
expect(FarMar::Market.all).to be_an Array
end
it "returns all 500 instances in csv file" do
expect(FarMar::Market.all.length).to eq 500
end
it "returns instances of FarMar::Market" do
expect(FarMar::Market.all[0].id).to eq 1
expect(FarMar::Market.all[0].name).to eq "People's Co-op Farmers Market"
expect(FarMar::Market.all[0].address).to eq "30th and Burnside"
end
end

describe "#self.find" do
it "returns the FarMar::Market with the specified id" do
expect(FarMar::Market.find(2)).to be_an_instance_of FarMar::Market
expect(FarMar::Market.find(2).name).to eq "Silverdale Farmers Market"
end
end

describe "#vendors" do
it "returns all Vendors for the specific market instance" do
expect(@market.vendors).to be_an Array
expect(@market.vendors.length).to eq 6
expect(@market.vendors[0].name).to eq "Feil-Farrell"
end
end

describe "#products" do
it "returns all Products for the specific market instance" do
expect(@market.products).to be_an Array
expect(@market.products.length).to eq 13
expect(@market.products[1].name).to eq "Fierce Greens"
end
end

describe "#self.search(search_term)" do
it "returns all Markets where the market or vendor name contain the search term" do
expect(FarMar::Market.search("School").length).to eq 3
expect(FarMar::Market.search("School")[0].id). to eq 75
expect(FarMar::Market.search("green").length).to eq 54
end
end

describe "#preferred_vendor(date = nil)" do
it "returns the vendor with the highest revenue" do
expect(@market.preferred_vendor).to be_an_instance_of FarMar::Vendor
expect(@market.preferred_vendor.name).to eq "Reynolds, Schmitt and Klocko"
expect(@market2.preferred_vendor.name).to eq "Denesik and Sons"
end
end

describe "#preferred_vendor(date)" do
it "returns the vendor with the highest revenue for a given date" do
expect(@market3.preferred_vendor("2013-11-10")).to be_an_instance_of FarMar::Vendor
expect(@market3.preferred_vendor("2013-11-07").id).to eq 7
end
end

describe "#worst_vendor(date = nil)" do
it "returns the vendor with the lowest revenue" do
expect(@market3.worst_vendor).to be_an_instance_of FarMar::Vendor
expect(@market3.worst_vendor.id).to eq 9
end
end

it "creates an instance of Farmar::Market class" do
expect(@market).to be_an_instance_of FarMar::Market
describe "#worst_vendor(date)" do
it "returns the vendor with the lowest revenue for a give date" do
expect(@market3.worst_vendor("2013-11-07")).to be_an_instance_of FarMar::Vendor
expect(@market3.worst_vendor("2013-11-07").id).to eq 9
end
end
end
end
Loading