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
51 changes: 51 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

# Used by dotenv library to load environment variables.
# .env

## Specific to RubyMotion:
.dat*
.repl_history
build/
*.bridgesupport
build-iPhoneOS/
build-iPhoneSimulator/

## Specific to RubyMotion (use of CocoaPods):
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# vendor/Pods/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalization:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
.DS_Store
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
farmar
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib", "specs"]
t.warning = false
t.verbose = false
t.test_files = FileList['specs/*_spec.rb']
#convention: testfiles need to end with #_spec.rb
#old version: _test.rb
puts "Running TestTask"
end

task default: :test do #default test, run TestTask first(built in, knows test -->TestTask)
puts "Running my Rakefile"
end
68 changes: 68 additions & 0 deletions lib/far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# gems your project needs
require 'csv'

# our namespace module
module FarMar; end

# all of our data classes that live in the module
require_relative './utils'
require_relative './finder'
require_relative './far_mar/farmar_market'
require_relative './far_mar/farmar_product'
require_relative './far_mar/farmar_sale'
require_relative './far_mar/farmar_vendor'



# ...require all needed classes


# puts FarMar::Market.find("5").inspect
# puts FarMar::Market.new.vendors("3").inspect

# vendor = FarMar::Vendor.new("3", "Breitenberg Inc", "5", "1")
# puts vendor.revenue

# product = FarMar::Product.new("1","Dry Beets","1")
# puts product.sales
# product = FarMar::Product.new("1","Dry Beets","1")
# puts product.number_of_sales

# puts FarMar::Sale.between("2013-11-11 18:43:56 -0800", "2013-11-12 12:00:35 -0800").size
# #=> 1294

# market = FarMar::Market.new("2", "Silverdale Farmers Market", "98383", "Silverdale", "Kitsap", "Washington", "98383")
# puts market.products("2").size
# => 3

# puts FarMar::Market.search("school").size

# market = FarMar::Market.new("2","Silverdale Farmers Market","98383","Silverdale","Kitsap","Washington","98383")
# puts market.prefered_vendor_direct.inspect

# market = FarMar::Market.new("2", "Silverdale Farmers Market", "98383", "Silverdale", "Kitsap", "Washington", "98383")
# puts market.prefered_vendor("2013-11-07").inspect
# => #<FarMar::Vendor:0x007fc3d92ed390 @id="2590", @name="Swaniawski-Schmeler", @employees_num="11", @market_id="482">

# market = FarMar::Market.new("2","Silverdale Farmers Market","98383","Silverdale","Kitsap","Washington","98383")
# puts market.worst_vendor_direct(FarMar::Vendor.all).inspect
# #=> <FarMar::Vendor:0x007ff54c1d1480 @id="1160", @name="Maggio, Spencer and Bergstrom", @employees_num="9", @market_id="213">
#
# market = FarMar::Market.new("2", "Silverdale Farmers Market", "98383", "Silverdale", "Kitsap", "Washington", "98383")
# puts market.worst_vendor("2013-11-07").inspect
# #=> <FarMar::Vendor:0x007ff54d80f2d8 @id="1470", @name="Quitzon LLC", @employees_num="8", @market_id="271">

# puts FarMar::Vendor.most_revenue(3).inspect

# puts FarMar::Vendor.most_items(3).inspect

# puts FarMar::Vendor.revenue("2013-11-07")
# #=>8995506.0

# puts FarMar::Sale.find_by_vendor_id("3").inspect
# vendor = FarMar::Vendor.new("3", "Breitenberg Inc", "5", "1")
# puts vendor.revenue("2013-11-13")
# # =>9128.0

puts FarMar::Product.most_revenue(1).inspect
#=>7895
128 changes: 128 additions & 0 deletions lib/far_mar/farmar_market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# lib/farmar_market.rb
# require_relative '../far_mar'
require 'date'

class FarMar::Market < Finder
attr_reader :id, :name
def initialize(id, name, address, city, county, state, zip)
@id = id
@name = name
@address = address
@city = city
@county = county
@state = state
@zip = zip
end

# return an array of class Market objects
def self.all
market_csv_file = "/Users/mengyao/ADA_class/FarMar/support/markets.csv"

markets = []
CSV.foreach(market_csv_file) do |row|
id = row[0].to_s
name = row[1].to_s
address =row[2].to_s
city = row[3].to_s
county = row[4].to_s
state = row[5].to_s
zip = row[6].to_s
markets << FarMar::Market.new(id, name, address, city, county, state, zip)
end
return markets
end

# =========inherite .find(id) from Finder class===========
# return an Market object that corresponds to the given market id
# def self.find(id)
# found_market = nil
# all.each do |market|
# if market.id == id
# found_market = market
# break
# end
# end
# return found_market
# end
#============================================================

# return an array of FarMar::Market objects where the market name or vendor name contain the search_term
def self.search(search_term)
term = search_term.downcase
# return an array of Market objects found with the matched market name or vendor name
return FarMar::Market.all.select do |market|
# a Market obejct's market name
market_name = market.name
# a Market object's associated (an array of) Vendor objects
vendors = FarMar::Vendor.by_market(market.id)
# get an array of each Vendor objects' vendor name
vendor_names = vendors.map!{|vendor| vendor.name}
# find search_term in market name(a string) and in each vendor name in an array of vendor names string
market_name.downcase.include?(term) || vendor_names.any?{ |vendor_name| vendor_name.downcase.include?(term) }
end
end

# return an array of FarMar::Product objects that are associated to the market through the FarMar::Vendor class.
# In another word, input a market_id and return the Product objects that associated with that market_id.
def products(market_id)
# return an array of its associated Vendor objects
vendors = FarMar::Vendor.by_market(market_id)
# iterate over all vendor_ids. for each vendor_id, return Product objects that associated with this vendor_id.
return vendors.map do |vendor|
FarMar::Product.all.select { |product| product.vendor_id == vendor.id }
end.flatten
# last step return array of array, call method flatten over return result
end

# return the Vendor object with the highest revenue among a group of vendors
def prefered_vendor_direct(vendors)
# vendors => FarMar::Vendor.all can put this in method call
# get an array Vendor objects sorted by vendor revenue in accending order
vendors_by_revenue = vendors.sort_by { |vendor| vendor.revenue }
# get the vendor that has the highest revenue(the last element in the array)
return vendors_by_revenue.last
end

# return an array of FarMar::Vendor objects that are associated with the market by the market_id field
def vendors
return FarMar::Vendor.all.select { |vendor| @id == vendor.market_id }
end

# ====================about MIXIN:=========================
# I access Utils here directly and call get_day_range method.
# I can use mixin, put "extend" on top and outside the Market class.
# I did not do it that way as here we do not want to expose the method to this class. But I got the concept.
# =========================================================

# return the Vendor object with the highest revenue for the given date
def prefered_vendor(date)
beginning_time, end_time = Utils.get_day_range(date)
# get an array of Sale objects in the given date
sales = FarMar::Sale.between(beginning_time, end_time)
# get an array of Vendors associated with the Sale objects
vendors = sales.map{|sale|sale.vendor}
# get the prefered vendor associate with the Sale object on that date
return self.prefered_vendor_direct(vendors)
end

# returnsthe vendor with the lowest revenue
def worst_vendor_direct(vendors)
# vendors => FarMar::Vendor.all can put this in method call
# get an array Vendor objects sorted by vendor revenue in accending order
vendors_by_revenue = vendors.sort_by { |vendor| vendor.revenue }
# get the vendor that has the lowest revenue(the last element in the array)
return vendors_by_revenue.first
end

# return the vendor with the lowest revenue on the given date
def worst_vendor(date)
beginning_time, end_time = Utils.get_day_range(date)
# get an array of Sale objects in the given date
sales = FarMar::Sale.between(beginning_time, end_time)
# get an array of Vendors associated with the Sale objects
vendors = sales.map{|sale| sale.vendor}
# get the prefered vendor associate with the Sale object on that date
return self.worst_vendor_direct(vendors)
end

end
78 changes: 78 additions & 0 deletions lib/far_mar/farmar_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# lib/farmar_product.rb
# require_relative '../far_mar'

class FarMar::Product < Finder
attr_reader :id, :vendor_id
def initialize(id, name, vendor_id)
@id = id
@name = name
@vendor_id = vendor_id
end

# return an array of class product objects
def self.all
if @products != nil
return @products
end

product_csv_file = "/Users/mengyao/ADA_class/FarMar/support/products.csv"

products = []
CSV.foreach(product_csv_file) do |row|
id = row[0].to_s
name = row[1].to_s
vendor_id =row[2].to_s

products << FarMar::Product.new(id, name, vendor_id)
end

@products = products
return @products
end

# =========inherite .find(id) from Finder class===========
# # return an product object that corresponds to the given product id
# def self.find(id)
# found_product = nil
# all.each do |product|
# if id == product.id
# found_product = product
# break
# end
# end
# return found_product
# end
#============================================================

# returns all of the FarMar::Product objects with the given vendor_id
def self.by_vendor(vendor_id)
return self.all.select {|product| product.vendor_id == vendor_id }
end

# returns the top n product instances ranked by total revenue
def self.most_revenue(n)
# get an array of Product objects sorted by revenue in descending order
products_by_revenue = FarMar::Product.all.sort_by {|product| product.vendor.revenue}.reverse
# get the top n Vendor objecthighest revenue
return products_by_revenue.slice(0...n)

end

# returns an array of FarMar::Vendor objects that is associated with Product's vendor_id
def vendor
return FarMar::Vendor.find(@vendor_id)
end

# return an array of FarMar::Sale objects that are associated with the product_id
def sales
return FarMar::Sale.find_by_product_id(@id)
end

# returns the number of times this product has been sold(fixnum)
def number_of_sales
# means return the size of array of Sale objects with the same product_id
return self.sales.size
end


end
Loading