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

completed FarMar #49

Open
wants to merge 8 commits into
base: rmt/master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion lib/far_mar.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'csv'
require 'time'
require './lib/far_mar/market'
require './lib/far_mar/vendor'
require_relative('far_mar/vendor.rb')
require './lib/far_mar/sale'
require './lib/far_mar/product'
48 changes: 47 additions & 1 deletion lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
require "csv"

module FarMar
class Market


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

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

# Returns an array of Market instances representing all of the markets
# Not sure I fully understand how this method works.
def self.all
csv_info = CSV.read("./support/markets.csv")
markets = []
csv_info.each do |line|
markets.push(Market.new(line[0], line[1], line[2], line[3], line[4], line[5], line[6]))
end
return markets
end

# Returns an instance of Market where the value of the id in the csv
# matches the passed parameter.
def self.find(market_id)
all.each do |market|
if market.id == market_id
return market # the instance of the market.
end
end
end

# Given the ID of the market, RETURN vendors with the same market_id.
def vendors
vendor_array = []
FarMar::Vendor.all.find_all do |v|
if v.market_id == id
vendor_array.push(v)
end
end
return vendor_array
end

end
end
71 changes: 71 additions & 0 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,76 @@
require "csv"

module FarMar
class Product

attr_accessor :id, :name, :vendor_id

def initialize(id, name, vendor_id)
@id = id.to_i
@name = name
@vendor_id = vendor_id.to_i
end

# Returns a collection of product instances
# representing all of the products described in the CSV
def self.all
csv_info = CSV.read("./support/products.csv")
products = []
csv_info.each do |line|
products.push(Product.new(line[0], line[1], line[2]))
end
return products
end

# Returns an instance of product where the value of the id in the csv
# matches the passed parameter.
def self.find(product_id)
all.each do |product|
if product.id == product_id
return product # the instance of the vendor.
end
end
end

# Returns the instance of the Vendor by the product vendor_id.
def vendor
FarMar::Vendor.all.each do |vend|
if vend.id == vendor_id
return vend
end
end
end

# Returns array of sales where the product_id equals the Product ID
def sales
sales_array = []
FarMar::Sale.all.find_all do |sales|
if sales.product_id == id
sales_array.push(sales)
end
end
end

# Returns the number of times this product has been sold.
def number_of_sales
count = 0
FarMar::Sale.all.each do |s|
if s.product_id == id
count += 1
end
end
return count
end

# Returns all of the products matching the vendor_id
def self.by_vendor(vend_id)
product_array = []
self.all.find_all do |prod|
if prod.vendor_id == vend_id
product_array.push(prod)
end
end
return product_array
end
end
end
64 changes: 64 additions & 0 deletions lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
require "csv"

module FarMar
class Sale

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

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

# Returns a collection of sale instances
# representing all of the sales described in the CSV
def self.all
csv_info = CSV.read("./support/sales.csv")
sales = []
csv_info.each do |line|
sales.push(Sale.new(line[0], line[1], line[2], line[3], line[4]))
end
return sales
## for optimization, say, if @sales has stuff in it, don't run it again, just return it.
end

# Returns an instance of sale where the value of the id in Sale
# matches the passed parameter.
def self.find(sale_id)
all.each do |sales|
if sales.id == sale_id
return sales # the instance of the sale.
end
end
end

# Returns the instance of the vendor associated with the Sale vendor_id
def vendor
FarMar::Vendor.all.each do |v|
if v.id == vendor_id
return v
end
end
end

# Returns the one product ID associated with this sale using the product_id
def product
FarMar::Product.all.each do |prod|
if prod.id == product_id
return prod
end
end
end

# Returns an array of sale objects where the purchase time is between the
# two times given as arguments.
def self.between(beginning_time, end_time)
sales_array = []
self.all.find_all do |sales|
if sales.purchase_time >= beginning_time && sales.purchase_time <= end_time
sales_array.push(sales)
end
end
return sales_array
end

end
end
87 changes: 87 additions & 0 deletions lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,92 @@
require "csv"

module FarMar
class Vendor

attr_accessor :id, :name, :employees, :market_id, :vendors

def initialize(id, name, employees, market_id)
@id = id.to_i
@name = name
@employees = employees.to_i
@market_id = market_id.to_i
end

# Returns an array of Vendor instances
# representing all of the vendors in the CSV
def self.all
csv_info = CSV.read("./support/vendors.csv")
vendors = []
csv_info.each do |line|
vendors.push(Vendor.new(line[0], line[1], line[2], line[3]))
end
return vendors
end

# Returns an instance of Vendor where the value of the id
# matches the passed parameter.
def self.find(vendor_id)
all.each do |vendor|
if vendor.id == vendor_id
return vendor # the instance of the vendor.
end
end
end

# Checking the market_id (of the vendor), what market do I belong to?
def market
FarMar::Market.all.each do |market|
if market.id == market_id
return market
end
end
end

# Returns an array of products with a vendor_id that matches the Vendor id
def products
product_array = []
FarMar::Product.all.find_all do |product|
if product.vendor_id == id
product_array.push(product)
end
end
return product_array
end

# Returns array of sales with vendor_id that match the vendor's ID
def sales
sales_array = []
FarMar::Sale.all.find_all do |sale|
if sale.vendor_id == id
sales_array.push(sale)
end
end
return sales_array
end

# Add all sales for vendor. Return sum in cents.
# If the vendor_id in Sale matches the Vendor ID, add them. Start a count.
def revenue
sale_cents = 0
FarMar::Sale.all.each do |sale|
if sale.vendor_id == id
sale_cents += sale.amount
end
end
return sale_cents
end

# Returns all of the vendors with the given market_id.
# (check the Vendors and if market_id matches market_id, array them.)
def self.by_market(market_id)
vendor_by_market_array = []
all.each do |vendors|
if vendors.market_id == market_id
vendor_by_market_array.push(vendors)
end
end
return vendor_by_market_array
end

end
end
28 changes: 27 additions & 1 deletion spec/far_mar/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@

describe FarMar::Market do
before :each do
@market = FarMar::Market.new
@market = FarMar::Market.new(1, 2, 3, 4, 5, 6, 7)
end

describe "Market.new" do
it "creates a new instance of a market" do
expect(@market).to be_an_instance_of FarMar::Market
end
end

describe ".all" do
it "returns an array" do
expect(FarMar::Market.all).to be_an Array
end
end

describe ".find" do
it "returns a single market" do
expect(FarMar::Market.find(1)).to be_an_instance_of FarMar::Market
expect(FarMar::Market.find(14).id).to eq 14
end
end

describe "#vendors" do
before :each do
@vendors = FarMar::Vendor.new(1,2,3,4)
end
it "returns an array of vendors" do
expect(@market.vendors).to be_an Array
end

it "has elements in the array" do
expect(@market.vendors.length).to eq 6
end
end
end
43 changes: 42 additions & 1 deletion spec/far_mar/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,53 @@

describe FarMar::Product do
before :each do
@product = FarMar::Product.new
@product = FarMar::Product.new(1,2,3)
end

describe "Product.new" do
it "creates a new instance of a market" do
expect(@product).to be_an_instance_of FarMar::Product
end
end

describe ".all" do
it "returns an array" do
expect(FarMar::Product.all).to be_an Array
end
end

describe ".find" do
it "returns a single vendor" do
expect(FarMar::Product.find(2)).to be_an_instance_of FarMar::Product
expect(FarMar::Product.find(2).id).to eq 2
end
end

describe "#vendor" do
it "returns a single vendor" do
expect(@product.vendor).to be_an_instance_of FarMar::Vendor
end
end

describe "#sales" do
it "returns an array of sales" do
expect(@product.sales).to be_an Array
end
end

describe "#number_of_sales" do
it "returns number of times the product has been sold" do
expect(@product.number_of_sales).to be >= 0
end
end

describe ".by_vendor" do
it "returns an array" do
expect(FarMar::Product.by_vendor(1)).to be_an Array
end
it "returns a certain number of instances" do
expect(FarMar::Product.by_vendor(5).length).to eq 3
end
end

end
Loading