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
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
6 changes: 6 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'csv'
module FarMar; end
require_relative './lib/farmar_market'
require_relative './lib/farmar_product'
require_relative './lib/farmar_sale'
require_relative './lib/farmar_vendor'
43 changes: 43 additions & 0 deletions lib/farmar_market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module FarMar
class Market
attr_reader :id, :name, :address, :city, :county, :state, :zip

def initialize(id, name, address, city, county, state, zip)
@id = id # (Fixnum) a unique identifier for that market
@name = name # (String) the name of the market (not guaranteed unique)
@address = address # (String) street address of the market
@city = city # (String) city in which the market is located
@county = county # (String) county in which the market is located
@state = state # ((String) state in which the market is located
@zip = zip # (String) zipcode in which the market is located
end

def self.all
market_list = []

market_file = CSV.read('/Users/kelly/Desktop/Ada_C6/FarMar/support/markets.csv', 'r')
market_file.each do |line|
market_list << self.new(line[0].to_i, line[1], line[2], line[3], line[4], line[5], line[6])
end

return market_list
end

def self.find(id)
self.all.each do |item|
if item.id == id
return item
end
end
puts "Sorry, invalid Market ID"
return nil
end

#vendors: returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field.
def vendors
vendor_list = FarMar::Vendor.all
vendor_by_market_id = vendor_list.select {|item| item.market_id == @id }
return vendor_by_market_id
end
end
end
59 changes: 59 additions & 0 deletions lib/farmar_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module FarMar
class Product
attr_reader :id, :name, :vendor_id

def initialize(id, name, vendor_id)
@id = id #(Fixnum) uniquely identifies the product
@name = name #(String) the name of the product (not guaranteed unique)
@vendor_id = vendor_id #(Fixnum) a reference to which vendor sells this product
end

def self.all
product_list = []

product_file = CSV.read('/Users/kelly/Desktop/Ada_C6/FarMar/support/products.csv', 'r')
product_file.each do |line|
product_list << self.new(line[0].to_i, line[1], line[2].to_i)
end

return product_list
end

def self.find(id)
self.all.each do |item|
if item.id == id
return item
end
end
puts "Sorry, invalid Product ID"
return nil
end

# vendor: returns the FarMar::Vendor instance that is associated with this product using the FarMar::Product vendor_id field
def vendor
return FarMar::Vendor.find(@vendor_id)
end

# sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field.
def sales
sales_list = FarMar::Sale.all
product_sales = sales_list.select { |item| item.product_id == @id }
return product_sales
end

# number_of_sales: returns the number of times this product has been sold.
def number_of_sales
num_of_sales = sales.length
return num_of_sales
end

#self.by_vendor(vendor_id): returns all of the products with the given vendor_id
def self.by_vendor(vendor_id)
products_by_current_vendor = []
self.all.each { |item| products_by_current_vendor << item.name if item.vendor_id == vendor_id}
len = products_by_current_vendor.length
puts "There are #{len} products sold by the given Vendor ID #{vendor_id}"
return products_by_current_vendor
end
end
end
50 changes: 50 additions & 0 deletions lib/farmar_sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 # (Fixnum) uniquely identifies the sale
@amount = amount # (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50)
@purchase_time = DateTime.parse(purchase_time) # (Datetime) when the sale was completed
@vendor_id = vendor_id # (Fixnum) a reference to which vendor completed the sale
@product_id = product_id # (Fixnum) a reference to which product was sold
end

def self.all
sale_list = []

sale_file = CSV.read('/Users/kelly/Desktop/Ada_C6/FarMar/support/sales.csv', 'r')
sale_file.each do |line|
sale_list << self.new(line[0].to_i, line[1].to_i, line[2], line[3].to_i, line[4].to_i)
end

return sale_list
end

def self.find(id)
self.all.each do |item|
if item.id == id
return item
end
end
puts "Sorry, invalid Sale ID"
return nil
end

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

#product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field
def product
return FarMar::Product.find(@product_id)
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(beginning_time, end_time)
sales_within_selected_timeframe = FarMar::Sale.all.select { |item| DateTime.parse(beginning_time) < item.purchase_time && item.purchase_time < DateTime.parse(end_time)}
return sales_within_selected_timeframe
end
end
end
71 changes: 71 additions & 0 deletions lib/farmar_vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module FarMar
class Vendor
attr_reader :id, :name, :num_of_employees, :market_id

def initialize(id, name, num_of_employees, market_id)
@id = id # (Fixnum) uniquely identifies the vendor
@name = name # (String) the name of the vendor (not guaranteed unique)
@num_of_employees = num_of_employees # (Fixnum) How many employees the vendor has at the market
@market_id = market_id # (Fixnum) a reference to which market the vendor attends
end

def self.all
vendor_list = []

vendor_file = CSV.read('/Users/kelly/Desktop/Ada_C6/FarMar/support/vendors.csv', 'r')
vendor_file.each do |line|
vendor_list << self.new(line[0].to_i, line[1], line[2].to_i, line[3].to_i)
end

return vendor_list
end

def self.find(id)
self.all.each do |item|
if item.id == id
return item
end
end
puts "Sorry, invalid Vendor ID"
return nil
end

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

#products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field.
def products
products_list = FarMar::Product.all
products_by_vendor_id = products_list.select {|i| i.vendor_id == @id }
return products_by_vendor_id
end

#sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field.
def sales
sales_list = FarMar::Sale.all
sales_by_vendor_id = sales_list.select {|i| i.vendor_id == @id }
return sales_by_vendor_id
end

#revenue: returns the the sum of all of the vendor's sales (in cents)
def revenue
sales_of_current_vendor_id = sales
revenue = 0
sales_of_current_vendor_id.each do |item|
revenue += item.amount
end
return revenue
end

#self.by_market(market_id): returns all of the vendors with the given market_id
def self.by_market(market_id)
vendors_in_current_market = []
self.all.each { |item| vendors_in_current_market << item.name if item.market_id == market_id }
len = vendors_in_current_market.length
puts "There are #{len} vendors with the given Market ID #{market_id}"
return vendors_in_current_market
end
end
end
37 changes: 37 additions & 0 deletions specs/farmar_market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require_relative 'spec_helper'

describe FarMar::Market do
let(:my_market) { FarMar::Market.new(123, 'KellyMarket', '11000 NE 10th Street', 'Bellevue', 'King County', 'WA', 98004) }

describe "#initialize" do
it "should return the correct values of instance variables that were established after initializing" do
my_market.id.must_equal(123)
my_market.name.must_equal('KellyMarket')
my_market.address.must_equal('11000 NE 10th Street')
my_market.city.must_equal('Bellevue')
my_market.county.must_equal('King County')
my_market.state.must_equal('WA')
my_market.zip.must_equal(98004)
end
end

describe "self.all" do
it "should return a collection of instances, representing all of the objects described in the CSV" do
a = FarMar::Market.all
a[0].name.must_equal("People's Co-op Farmers Market")
end

end

describe "self.find(id)" do
it "should return an instance of the object where the value of the id field in the CSV matches the passed parameter" do
FarMar::Market.find(1).name.must_equal("People's Co-op Farmers Market")
end
end

describe "#vendors" do
it "should return a collection of FarMar::Vendor instances that are associated with the market by the market_id field " do
my_market.vendors.length.must_equal(6)
end
end
end
55 changes: 55 additions & 0 deletions specs/farmar_product_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require_relative 'spec_helper'

describe FarMar::Product do
let(:my_product) { FarMar::Product.new(1, 'Dry Beets', 1) }

describe "#initialize" do
it "should return the correct values of instance variables that were established after initializing" do
my_product.id.must_equal(1)
my_product.name.must_equal('Dry Beets')
my_product.vendor_id.must_equal(1)
end
end

describe "self.all" do
it "should return a collection of instances, representing all of the objects described in the CSV" do
a = FarMar::Product.all
a[0].name.must_equal("Dry Beets")
end

end

describe "self.find(id)" do
it "should return an instance of the object where the value of the id field in the CSV matches the passed parameter" do
FarMar::Product.find(1).name.must_equal("Dry Beets")
end
end

describe "#vendor" do
it "returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field" do
my_product.vendor.name.must_equal("Feil-Farrell")
end
end

describe "#sales" do
let(:my_products_sales) { my_product.sales }

it "returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field" do
my_products_sales.length.must_equal(7)
my_products_sales.first.amount.must_equal(9290)
my_products_sales.last.amount.must_equal(4095)
end
end

describe "#number_of_sales" do
it "returns the number of times this product has been sold" do
my_product.number_of_sales.must_equal(7)
end
end

describe "self.by_vendor(vendor_id)" do
it "returns all of the products with the given vendor_id" do
FarMar::Product.by_vendor(123).must_equal(["Spotty Mushrooms", "Bad Carrots", "Slow Mushrooms"])
end
end
end
51 changes: 51 additions & 0 deletions specs/farmar_sale_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require_relative 'spec_helper'

describe FarMar::Sale do
let(:transact) { FarMar::Sale.new(1, 9290, '2013-11-07 04:34:56 -0800', 1, 1) }

describe "#initialize" do
it "should return the correct values of instance variables that were established after initializing" do
transact.id.must_equal(1)
transact.amount.must_equal(9290)
transact.purchase_time.must_equal(DateTime.parse('2013-11-07 04:34:56 -0800'))
transact.vendor_id.must_equal(1)
transact.product_id.must_equal(1)
end
end

describe "self.all" do
it "should return a collection of instances, representing all of the objects described in the CSV" do
a = FarMar::Sale.all
a[0].amount.must_equal(9290)
end

end

describe "self.find(id)" do
it "should return an instance of the object where the value of the id field in the CSV matches the passed parameter" do
FarMar::Sale.find(456).amount.must_equal(9450)
end
end

describe "#vendor" do
it "returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field" do
transact.vendor.name.must_equal("Feil-Farrell")
end
end

describe "#product" do
it "returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field" do
transact.product.name.must_equal("Dry Beets")
end
end

describe "self.between(beginning_time, end_time)" do
let(:timeframe) { FarMar::Sale.between('2013-11-06 20:44:00 -0800', '2013-11-07 04:34:56 -0800')}

it "returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments" do
timeframe.length.must_equal(595)
timeframe.first.amount.must_equal(8296)
timeframe.last.amount.must_equal(5200)
end
end
end
Loading