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

FarMar Primary Requirements - Carmen #68

Open
wants to merge 2 commits into
base: 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
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
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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']
puts "Running TestTask"
end

task default: :test do # Running before the default task, run the test task. TestTask first then default task. If command line is "test task" instead of "rake", only the test task is run.
puts "Running my Rakefile"
end
7 changes: 7 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# September 6th, 2016
#FarMar Module file


module FarMar

end
10 changes: 10 additions & 0 deletions lib/farmar_market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# September 6th, 2016
# lib/farmar_market.rb

require_relative '../far_mar'

require 'csv'

class FarMar::Market

end
42 changes: 42 additions & 0 deletions lib/file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# September 6th, 2016
# file.rb
# This program reads a file and returns a collection of instances and an instance of object with the matching id

require 'csv'
require_relative '../far_mar'

class FarMar::ReadingFile

#returns a collection of the file
def self.import_all_file(filename)
collection = []

CSV.open(filename, 'r').each do |line|
collection << line
end

return collection
end

#returns the line with the matching id
def self.find_instance(filename, id)
CSV.foreach(filename, 'r') do |line|
if line[0] == id.to_s
return line
end
end
end

#returns an array of instances that are associated the an id
def array_of_instances(an_array_of_instances, instance_variable, id)
return_array = []

an_array_of_instances.each do |instance|
if instance.send(instance_variable) == id
return_array << instance
end
end

return return_array
end
end
52 changes: 52 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# September 6th, 2016
# market.rb
# This program reads a market file and returns the instances within the file
# This program also returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field.

require 'csv'
require_relative '../far_mar'
require_relative './file'
require_relative './vendor'


class FarMar::Market < FarMar::ReadingFile
attr_reader :market_id, :name, :address, :city, :county, :state, :zip

FILENAME = "./support/markets.csv"

def initialize(info)
@market_id = info[0]
@name = info[1]
@address = info[2]
@city = info[3]
@county = info[4]
@state = info[5]
@zip = info[6]
end


#returns a collection of instances - inherit from parent class FarMar::ReadingFile
def self.all
return import_all_file(FILENAME).map {|row| self.new(row)}
end

#returns an instance of the object with the matching id - inherit from parent class FarMar::ReadingFile
def self.find(mark_id)
return self.new(find_instance(FILENAME, mark_id))
end

#returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field.
def vendor
return array_of_instances(FarMar::Vendor.all, "market_id", market_id)
end

end


########## TEST ##############
# puts FarMar::Market.all[1].name #=> expected value Silverdale Farmers Market
# puts FarMar::Market.find(5).name #=> expected value Quincy Farmers Market
#
# mart = FarMar::Market.new(["1", "People's Co-op Farmers Market", "30th and Burnside", "Portland", "Multnomah", "Oregon", "97202"])
#
# puts mart.vendor(mart.market_id).length #=> expected value 6
100 changes: 100 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# September 7th, 2016
#product.rb
#This program reads a product file and returns the instances within the file.
#This program also returns the top n product instances ranked by total revenue

require 'csv'
require_relative '../far_mar'
require_relative './file'
require_relative './vendor'
require_relative './market'
require_relative './sale'


class FarMar::Product < FarMar::ReadingFile
attr_reader :product_id, :name, :vendor_id

FILENAME = "./support/products.csv"

def initialize(info)
@product_id = info[0]
@name = info[1]
@vendor_id = info[2]
end

# returns a collection of instances - inherit from parent class FarMar::ReadingFile
def self.all
return import_all_file(FILENAME).map {|row| self.new(row)}
end


#returns an instance of the object - inherit from parent class FarMar::ReadingFile
def self.find(prod_id)
return self.new(find_instance(FILENAME, prod_id))
end

#returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field
def vendor
return array_of_instances(FarMar::Vendor.all, "vendor_id", vendor_id).first
end

# returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field.
def sales
return array_of_instances(FarMar::Sale.all, "product_id", product_id)
end

# returns the number of times this product has been sold.
def number_of_sales
product_array = array_of_instances(FarMar::Sale.all, "product_id", product_id)

return product_array.length
end

# returns all of the products with the given vendor_id
def self.by_vendor(ven_id)
return_array = []

FarMar::Product.all.each do |product|
if product.vendor_id == ven_id.to_s
return_array << product
end
end

return return_array
end

#returns the top n product instances ranked by total revenue
def self.most_revenue(n)
return_array = []

#group_by (product_id) method returns a hash with product_id keys and an array of sale instances values
sorted_sales_hash = FarMar::Sale.all.group_by {|obj| obj.product_id}

sorted_sales_hash.each do |prod_id, products|
sum = 0
products.each do |prod|
sum += prod.amount.to_i
end

#sorted_sales_hash contains product_id keys and the revenue sum as the values
sorted_sales_hash[prod_id] = sum
end

#sort_by method returns an array of arrays with the first element as the keys and the second element as the value in the hash.
#descending_array ([[product_id, sum], [product_id, sum], etc.]) contains the product_id and the revenue sum in descending order
descending_array = sorted_sales_hash.sort_by {|id, sum| sum}.reverse

#descending_array[i][0] returns the product_id
n.times do |i|
return_array << self.find(descending_array[i][0])
end

return return_array
end
end


############### TESTING ###############
# puts FarMar::Product.most_revenue(2)[0].name
# puts FarMar::Product.most_revenue(2)[1].name
# puts FarMar::Product.by_vendor(2)
71 changes: 71 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# September 7th, 2016
#sale.rb
#This program reads a sales file and returns the instances within the file.
#This program also returns the following:
#vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field
#product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field
# self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments

require 'awesome_print'
require 'csv'
require_relative '../far_mar'
require_relative './file'
require_relative './vendor'
require_relative './market'
require_relative './product'


class FarMar::Sale < FarMar::ReadingFile
attr_reader :sale_id, :amount, :vendor_id, :product_id

attr_accessor :purchase_time

FILENAME = "./support/sales.csv"

def initialize(info)
@sale_id = info[0]
@amount = info[1]
@purchase_time = DateTime.parse(info[2])
@vendor_id = info[3]
@product_id = info[4]
end

# returns a collection of instances - inherit from parent class FarMar::ReadingFile
def self.all
return import_all_file(FILENAME).map {|row| self.new(row)}
end

#returns an instance of the object - inherit from parent class FarMar::ReadingFile
def self.find(sal_id)
return self.new(find_instance(FILENAME, sal_id))
end

#returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field
def vendor
return array_of_instances(FarMar::Vendor.all, "vendor_id", vendor_id).first
end

#returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field
def product
return array_of_instances(FarMar::Product.all, "product_id", product_id).first
end

#returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments
def self.between (beginning_time, end_time)
return_array = []

self.all.each do |sale|
if sale.purchase_time >= beginning_time && sale.purchase_time <= end_time
return_array << sale
end
end

return return_array
end
end

################## TESTING ################
#
# FarMar::Sale.between(DateTime.parse("2013-11-06 08:38:38 -0800"),DateTime.parse("2013-11-06 08:43:15 -0800")).each do |item|
# puts item.name
# end
Loading