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

Th/master #38

Open
wants to merge 23 commits into
base: th/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
90 changes: 86 additions & 4 deletions lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,92 @@
require 'pry'
module FarMar

class Market
attr_accessor :name

def initialize
@name =""
attr_accessor :name, :id, :address, :county, :state, :zip, :city

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



def self.all
@@market_all ||= []
if @@market_all == []
market_csv = CSV.read("./support/markets.csv")
market_csv.each do |row|
new_market = Market.new(row[0].to_i, row[1], row[2], row[3], row[4], row[5], row[6])
@@market_all.push(new_market)
# binding.pry
end
end
return @@market_all
end

def self.find(id)
@@market_all= self.all
@@market_all.find do |market|
market.id == id
end
end

def vendors
return FarMar::Vendor.market(self.id)
end

def products
# return FarMar::Product.by_market(self.id)
return FarMar::Vendor.market(self.id, true)
end

def prefered_vendor
return FarMar::Vendor.best_revenue(self.id)
end

def worst_vendor
return FarMar::Vendor.worst_revenue(self.id)
end

def self.search(search_term)
include_array =[]
vendor_array = FarMar::Vendor.all
vendor_array.each do |vendor|
vendor_name = vendor.name
if vendor_name.include?(search_term)
market = self.find(vendor.market_id)
if !(check_include(include_array, market))
# !include_array.iclude?(market)
include_array.push(market)
end
end
end
@@market_all = self.all
@@market_all.each do |market|
market_name = market.name
if market_name.include?(search_term) #working
# binding.pry
if (check_include(include_array, market) == false)
include_array.push(market)
end
end
end
return include_array
end

def self.check_include (include_array , market)
include_array.each do |mar|
if mar.name == market.name
return true
end
end
return false
end

end
end
55 changes: 52 additions & 3 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
module FarMar

class Product
attr_accessor :name
def initialize
@name = ""
attr_accessor :name , :id, :vendor_id

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

def self.all
@@product_all ||= []
if @@product_all == []
product_csv = CSV.read("./support/products.csv")
product_csv.each do |product|
new_product = Product.new(product[0].to_i, product[1], product[2].to_i)
@@product_all.push(new_product)
end
end
return @@product_all
end

def self.find(id)
@@product_all = self.all
@@product_all.find do |product|
product.id == id
end
end

def self.by_vendor(vendor_id)
@@product_all = self.all
@@product_all.find_all do |product|
product.vendor_id == vendor_id
end
end
#
# def self.by_market(market_id)
# @@product_all = self.all
# @@product_all.find_all do |product|
# product.market_id == market_id
# end
# end


def sales
sales_array = FarMar::Sale.all
sales_array.find_all do |sale|
sale.product_id == self.id
end
end

def number_of_sales
return self.sales.length
end

end
end
58 changes: 55 additions & 3 deletions lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,61 @@
require 'pry'
module FarMar

class Sale
attr_accessor :amount
def initialize
@amount = 3
attr_accessor :amount , :id, :purchase_time, :vendor_id , :product_id
def initialize (id, amount, purchase_time, vendor_id, product_id)
@amount = amount
@id = id
@purchase_time = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z" )
@vendor_id = vendor_id
@product_id = product_id
# @sale_array = self.all -check it later

end

def self.all
@@sale_all ||= []
if @@sale_all == []
sale_csv = CSV.read("./support/sales.csv")
sale_csv.each do |sale|
new_sale = Sale.new(sale[0].to_i, sale[1].to_i, sale[2], sale[3].to_i, sale[4].to_i)
@@sale_all.push(new_sale)
end
end
return @@sale_all
end

def self.find(id)
@@sale_all = self.all
@@sale_all.find do |sale|
sale.id == id
end
end

def self.by_vendor(id)
@@sale_all = self.all
@@sale_all.find_all do |sale|
sale.vendor_id == id
end
end

def self.between(beginnig_time, end_time)
@@sale_all = self.all
array_sales = []
@@sale_all.map do |sale|
check = sale.purchase_time.between?(beginnig_time, end_time)
array_sales.push(sale) if check
end
return array_sales
end

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

def product
return FarMar::Product.find(self.product_id)
end

end
end
101 changes: 98 additions & 3 deletions lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,103 @@
require 'pry'
module FarMar
class Vendor
attr_accessor :name
def initialize
@name =""
attr_accessor :name, :id, :no_employess, :market_id

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

def self.all
@@array_all ||= []
if @@array_all == []
vendor_csv = CSV.read("./support/vendors.csv")
vendor_csv.each do |sale|
new_vendor = Vendor.new(sale[0].to_i, sale[1], sale[2], sale[3].to_i)
@@array_all.push(new_vendor)
end
end
return @@array_all
end

def self.find(id)
@@array_all = self.all
@@array_all.find do |vendor|
vendor.id == id
end
end

# found all the vendors in the market - with default of salfe.
# if the test = true => it return all the products in that market.
def self.market(id, extra_test = false)
@@array_all = self.all
if extra_test == false
@@array_all.find_all do |vendor|
vendor.market_id == id
end
else
final_array = []
products_array = FarMar::Product.all
@@array_all.each do |vendor|
products_array.each do |product|
final_array.push(product) if (product.vendor_id == vendor.id) && (vendor.id == id)
end
end
return final_array
end
end

def products
return products_array = FarMar::Product.by_vendor(self.id)
end

def sales
return Farmar::Sale.by_vendor(self.id)
end


def revenue
sale_array = FarMar::Sale.by_vendor(self.id)
return sale_array.inject(0){|sum, sale| sum + sale.amount}
end

def self.best_revenue(market_id)
vendors = self.by_market(market_id)
max = 0
winner = nil
vendors.each do |vendor|
revenue = vendor.revenue.to_i
if revenue > max
max = revenue
winner = vendor
end
end
return winner
end

def self.worst_revenue(market_id)
vendors = self.by_market(market_id)
min = 10000000000
looser = nil
vendors.each do |vendor|
revenue = vendor.revenue.to_i
if revenue < min
min = revenue
looser = vendor
end
end
return looser
end

def self.by_market(market_id)
@@array_all = self.all
@@array_all.find_all do |vendor|
vendor.market_id == market_id
end
end


end
end
Loading