Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Noelle Morris #56

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c11e276
Created baseline (lib, specs,Rakefile.
johnanmorris Sep 6, 2016
06ad599
edited file names; edited spec_helper to reference far_mar; wrote mus…
johnanmorris Sep 6, 2016
453aad2
added tests and method self.all for Market class
johnanmorris Sep 6, 2016
2b39258
wrote tests and methods for self.all for each class
johnanmorris Sep 6, 2016
4155ad5
fixed variable names
johnanmorris Sep 6, 2016
eab185a
added tests and methods for self.find(id) for all classes.
johnanmorris Sep 7, 2016
71fdf02
added additional tests for find methods
johnanmorris Sep 8, 2016
804a696
wrote tests for and implemented vendors method
johnanmorris Sep 8, 2016
e6081b6
Wrote tests for and implemented #market method
johnanmorris Sep 8, 2016
b1d58d8
Wrote tests for and implemented Vendor #products method
johnanmorris Sep 8, 2016
d97099b
wrote tests and implemented Vendor revenue method
johnanmorris Sep 8, 2016
4ad20f7
wrote tests and implemented FarMar::Vendor.by_market(market_id); refa…
johnanmorris Sep 8, 2016
01e54c7
refactored Market #vendors and Vendors.by_market; re-worded test desc…
johnanmorris Sep 8, 2016
1ba113b
wrote tests for and implemented Product methods; refactored Market #p…
johnanmorris Sep 8, 2016
99f1737
wrote tests for and implemented methods for Sale class.
johnanmorris Sep 8, 2016
2af0c48
refactoring tests and adding cases for no IDs or empty return values.
johnanmorris Sep 8, 2016
0110ccc
cleaned up code and refactored a couple tests
johnanmorris Sep 9, 2016
26cfa7f
re-added necessary variables
johnanmorris Sep 9, 2016
a451c8a
refactors
johnanmorris Sep 9, 2016
80000f3
WIP self.most_revenue(n)
johnanmorris Sep 9, 2016
479d05a
undid previous commit
johnanmorris Sep 9, 2016
35155f6
tried some things that didn't work; notes
johnanmorris Sep 9, 2016
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/coverage/
.DS_Store
/support/.DS_Store
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
11 changes: 11 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# gems your project needs
require 'csv'

# our namespace module
module FarMar; end

# all of our data classes that live in the module
require_relative 'lib/market.rb'
require_relative 'lib/product.rb'
require_relative 'lib/sale.rb'
require_relative 'lib/vendor.rb'
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(market_hash)
@id = market_hash[:id]
@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

def self.all
markets = []
CSV.read("/Users/johnamorris/ada/project-forks/FarMar/support/markets.csv").each do |line|
info_hash = {}
info_hash[:id] = line[0].to_i
info_hash[:name] = line[1]
info_hash[:address] = line[2]
info_hash[:city] = line[3]
info_hash[:county] = line[4]
info_hash[:state] = line[5]
info_hash[:zip] = line[6].to_i
market = FarMar::Market.new(info_hash)
markets << market
end
return markets
end

def self.find(id)
raise ArgumentError.new("Invalid ID") if !id.is_a?(Fixnum)
markets = self.all
markets.each do |market|
if market.id == id
return market
end
end
return nil
end

def vendors
FarMar::Vendor.by_market(id)
end
end
end
57 changes: 57 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module FarMar
class Product
attr_reader :id, :name, :vendor_id

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

def self.all
products = []
CSV.read("/Users/johnamorris/ada/project-forks/FarMar/support/products.csv").each do |line|
info_hash = {}
info_hash[:id] = line[0].to_i
info_hash[:name] = line[1]
info_hash[:vendor_id] = line[2].to_i
product = FarMar::Product.new(info_hash)
products << product
end
return products
end

def self.find(id)
raise ArgumentError.new("Invalid ID") if !id.is_a?(Fixnum)
products = self.all
products.each do |product|
if product.id == id
return product
end
end
return nil
end

def vendor
all_vendors = FarMar::Vendor.all
all_vendors.each do |vendor|
if vendor.id == vendor_id
return vendor
end
end
end

def sales
FarMar::Sale.all.select {|sale| sale.product_id == id}
end

def number_of_sales
sales_list = sales
return sales_list.length
end

def self.by_vendor(vendor_id)
FarMar::Product.all.select { |product| product.vendor_id == vendor_id}
end
end
end
72 changes: 72 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module FarMar
class Sale
attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id

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

def self.all
sales = []
CSV.read("/Users/johnamorris/ada/project-forks/FarMar/support/sales.csv").each do |line|
info_hash = {}
info_hash[:id] = line[0].to_i
info_hash[:amount] = line[1].to_i
info_hash[:purchase_time] = DateTime.parse(line[2])
info_hash[:vendor_id] = line[3].to_i
info_hash[:product_id] = line[4].to_i
sale = FarMar::Sale.new(info_hash)
sales << sale
end
return sales
end

def self.find(id)
raise ArgumentError.new("Invalid ID") if !id.is_a?(Fixnum)
sales = self.all
sales.each do |sale|
if sale.id == id
return sale
end
end
return nil
end

def vendor
all_vendors = FarMar::Vendor.all
all_vendors.each do |vendor|
if vendor.id == vendor_id
return vendor
end
end
end

def product
all_products = FarMar::Product.all
all_products.each do |product|
if product.id == product_id
return product
end
end
end

def self.between(beginning_time, end_time)
beginning_time = DateTime.parse(beginning_time)
end_time = DateTime.parse(end_time)
all_sales = FarMar::Sale.all

sales_between = all_sales.select do |sale|
sale.purchase_time >= beginning_time && sale.purchase_time <= end_time
end

unless sales_between.length == 0
return sales_between
end
return nil
end
end
end
63 changes: 63 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module FarMar
class Vendor
attr_reader :id, :name, :employees, :market_id

def initialize(vendor_hash)
@id = vendor_hash[:id]
@name = vendor_hash[:name]
@employees = vendor_hash[:employees]
@market_id = vendor_hash[:market_id]
end

def self.all
vendors = []
CSV.read("/Users/johnamorris/ada/project-forks/FarMar/support/vendors.csv").each do |line|
info_hash = {}
info_hash[:id] = line[0].to_i
info_hash[:name] = line[1]
info_hash[:employees] = line[2].to_i
info_hash[:market_id] = line[3].to_i
vendor = FarMar::Vendor.new(info_hash)
vendors << vendor
end
return vendors
end

def self.find(id)
raise ArgumentError.new("Invalid ID") if !id.is_a?(Fixnum)
vendors = self.all
vendors.each do |vendor|
if vendor.id == id
return vendor
end
end
return nil
end

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

def products
FarMar::Product.by_vendor(id)
end

def sales
FarMar::Sale.all.select {|sale| sale.vendor_id == id }
end

def revenue
sales_list = sales
total = 0
sales_list.each do |sale|
total += sale.amount
end
return total
end

def self.by_market(market_id)
FarMar::Vendor.all.select { |vendor| vendor.market_id == market_id}
end
end
end
69 changes: 69 additions & 0 deletions specs/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require_relative 'spec_helper'

describe FarMar::Market do
before(:all) do
@market = FarMar::Market.find(5)
@vendor_list = @market.vendors
end

describe "#initialize" do
it "must create an instance of Market" do
test_hash = {
id: 5,
name: "Kent Farmers Market",
address: "2nd Ave N",
city: "Kent",
county: "King",
state: "WA",
zip: 98032
}

test_market = FarMar::Market.new(test_hash)
test_market.must_be_instance_of(FarMar::Market)
end
end

describe "self.all" do
all_markets = FarMar::Market.all

it "must return a collection of Market instances" do
all_markets.first.must_be_instance_of(FarMar::Market)
all_markets.last.must_be_instance_of(FarMar::Market)
end
end

describe "self.find(id)" do

it "must take a Fixnum as an argument" do
bad_args = ["2", "2.4", 2.4, "hat"]

bad_args.each do |item|
proc { FarMar::Market.find(item) }.must_raise(ArgumentError)
end
end

it "must return nil if the id doesn't exist" do
invalid_id = FarMar::Market.find(5000)
invalid_id.must_equal(nil)
end

it "must return an instance of Market" do
@market.must_be_instance_of(FarMar::Market)
end

it "must return the right instance of Market" do
@market.id.must_equal(5)
end
end

describe "#vendors" do
it "must return a collection of Vendor instances" do
@vendor_list.first.must_be_instance_of(FarMar::Vendor)
end

it "must return Vendors matching Market id" do
@vendor_list.first.market_id.must_equal(@market.id)
@vendor_list.last.market_id.must_equal(@market.id)
end
end
end
Loading