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
43 changes: 42 additions & 1 deletion lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
require "pry"
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


def self.all
market_array = []
farmar_data = CSV.read("./support/markets.csv")
farmar_data.each do |csv|
@m = Market.new(csv[0], csv[1], csv[2], csv[3], csv[4], csv[5], csv[6])
market_array.push(@m)
end
return market_array
end

def self.find(id)
farmar_data = Market.all
farmar_data.find do |data|
if data.id == id
return data
end
end
end

def get_vendor
FarMar::Vendor.all.find_all do |vendor|
vendor.market_id == @id
end
end
end
end
58 changes: 58 additions & 0 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
require "pry"
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

def self.all
product_array =[]
product_data = CSV.read("./support/products.csv")
product_data.each do |csv|
@p = Product.new(csv[0], csv[1],csv[2])
product_array.push(@p)
end
return product_array
end

def self.find(id)
product_data = Product.all
product_data.find do |data|
if data.id == id
return data
end
end
end

def get_vendor
FarMar::Vendor.all.find_all do |vendor|
vendor.id == @vendor_id
end
end

def get_sales
FarMar::Sale.all.find_all do |sale|
sale.vendor_id == @id
end
end

def number_of_sales
total = 0
FarMar::Sale.all.each do |each_sale|
if each_sale.product_id == id
total = total + 1
end
end
return total
end

def self.by_vendor(vendor_id)
self.all.find_all do |product|
product.vendor_id == vendor_id
end

end

end
end
53 changes: 53 additions & 0 deletions lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
require "pry"
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

def self.all
sale_array = []
sale_data = CSV.read("./support/sales.csv")
sale_data.each do |csv|
@s = Sale.new(csv[0], csv[1], csv[2], csv[3], csv[4])
sale_array.push(@s)
end
return sale_array
end

def self.find(id)
sale_data = Sale.all
sale_data.find do |data|
if data.id == id
return data
end
end
end
def get_vendor
FarMar::Vendor.all.find_all do |vendor|
vendor.id == @vendor_id
end
end

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

def self.between(beginning_time, end_time)
array = []
self.all.find_all do |sale_times|
if sale_times.purchase_time >= beginning_time && sale_times.purchase_time <= end_time
array.push(sale_times)
end
end
return array
end
end
end
64 changes: 64 additions & 0 deletions lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
require "pry"
require "csv"

module FarMar
class Vendor
attr_accessor :id, :name, :no_of_employees, :market_id

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

def self.all
vendor_array = []
vendor_data = CSV.read("./support/vendors.csv")
vendor_data.each do |csv|
@v = Vendor.new(csv[0], csv[1], csv[2], csv[3])
vendor_array.push(@v)
end
return vendor_array
end

def self.find(id)
vendor_data = Vendor.all
vendor_data.find do |data|
if data.id == id
return data
end
end
end

def get_market
FarMar::Market.all.find do |market|
market.id == @market_id
end
end

def get_products
FarMar::Product.all.find_all do |product|
product.vendor_id == @id
end
end

def get_sales
FarMar::Sale.all.find_all do |sale|
sale.vendor_id == @id
end
end

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

def self.by_market(market_id)
FarMar::Vendor.all.find_all do |vendor|
vendor.market_id == market_id

end


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

describe FarMar::Market do
before :each do
@market = FarMar::Market.new
@market = FarMar::Market.new("id","name","city","county","state","zip","address")
end

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



describe "return a collection of market instances described in CVS" do
it "returns all of the accounts" do
expect(FarMar::Market.all).to be_an_instance_of Array
end
end

describe "return one account" do
it "returns one account" do
expect(FarMar::Market.find(1).id).to eq 1
end
end

describe ".get_vendor" do
it "returns all accounts with same market id" do
expect(@market.get_vendor).to be_an Array
end
end

end
37 changes: 35 additions & 2 deletions spec/far_mar/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,45 @@

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

describe "new product instance; .new" do

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


describe "return a collection of product instances described in CVS" do
it "returns all of the accounts" do
expect(FarMar::Product.all).to be_an_instance_of Array
end
end

describe "return one account" do
it "returns one account" do
expect(FarMar::Product.find(1).id).to eq 1
end
end

describe "get_vendor" do
it "returns all account with the same vendor id" do
expect(@product.get_vendor).to be_an Array
end
end

describe "number_of_sales" do
it "returns the number of the product has been sold" do
expect(@product.number_of_sales).to eq 7
end
end

describe "self.by_vendor" do
it "returns returns all of the products with the given vendor_id" do
expect(FarMar::Product.all.find_all)
end
end

end
26 changes: 25 additions & 1 deletion spec/far_mar/sale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,36 @@

describe FarMar::Sale do
before :each do
@sale = FarMar::Sale.new
@sale = FarMar::Sale.new("id", "amount", "purchase_time", "vendor_id", "product_id")
end

describe "new sale instance" do
it "creats a new sale instance" do
expect(@sale).to be_an_instance_of FarMar::Sale
end
end
describe "return a collection of sale instances described in CVS" do
it "returns all of the accounts" do
expect(FarMar::Sale.all).to be_an_instance_of Array
end
end

describe "return one account" do
it "returns one account" do
expect(FarMar::Sale.find(1).id).to eq 1
end
end

describe "get_vendor" do
it "returns vendor instance ralated to sale" do
expect(@sale.get_vendor).to be_an Array
end
end

describe "get_product" do
it "returns product instance related to sale" do
expect(@sale.get_product). to be_an Array
end
end

end
Loading