diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..35fd14cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store/ +coverage/ diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..82e5a661 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.test_files = FileList['specs/*_spec.rb'] + end + +task default: :test diff --git a/far_mar.rb b/far_mar.rb new file mode 100644 index 00000000..d67786d0 --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,10 @@ +require 'csv' +require 'date' +require 'time' + +module FarMar; end + +require_relative 'lib/market' +require_relative 'lib/product' +require_relative 'lib/sale' +require_relative'lib/vendor' diff --git a/lib/market.rb b/lib/market.rb new file mode 100644 index 00000000..1b79a93f --- /dev/null +++ b/lib/market.rb @@ -0,0 +1,44 @@ +module FarMar + class Market + attr_reader :id, :name, :address, :city, :county, :state, :zip + attr_accessor :list_of_markets + + def initialize(id, name, address, city, county, state, zip) + @id = id + @name = name + @address = address + @city = city + @county = county + @state = state + @zip = zip + end + + def self.all + list_of_markets = [] + CSV.read("/Users/YNaka/Ada/project_forks/FarMar/support/markets.csv", "r").each do |line| + list_of_markets << self.new(line[0].to_i, line[1], line[2], line[3], line[4], line[5], line[6]) + end + list_of_markets + end + + def self.find(market_id) + Market.all.each do |m| + if market_id == m.id + return m + end + end + return "That is not an existing market ID" + end + + def vendors + this_markets_vendors = [] + FarMar::Vendor.all.each do |v| + if @id == v.market_id + this_markets_vendors << v.name + end + end + return this_markets_vendors + end + + end +end diff --git a/lib/product.rb b/lib/product.rb new file mode 100644 index 00000000..b80aae62 --- /dev/null +++ b/lib/product.rb @@ -0,0 +1,62 @@ +module FarMar + class Product + attr_reader :id, :name, :vendor_id + + def initialize(id, name, vendor_id) + @id = id + @name = name + @vendor_id = vendor_id + end + + def self.all + list_of_products = [] + CSV.read("/Users/YNaka/Ada/project_forks/FarMar/support/products.csv", "r").each do |line| + list_of_products << self.new(line[0].to_i, line[1], line[2].to_i) + end + list_of_products + end + + def self.find(product_id) + Product.all.each do |p| + if product_id == p.id + return p + end + end + return "That is not an existing product ID" + end + + def self.by_vendor(vendor_id) + products_for_this_vendor = [] + Product.all.each do |p| + if vendor_id == p.vendor_id + products_for_this_vendor << p.name + end + end + return products_for_this_vendor + end + + def vendor + FarMar::Vendor.all.each do |v| + if @vendor_id == v.id + return v + end + end + end + + def sales + @this_products_sales = [] + FarMar::Sale.all.each do |s| + if @id == s.product_id + @this_products_sales << s.id + end + end + return @this_products_sales + end + + def number_of_sales + sales + return @this_products_sales.length + end + + end +end diff --git a/lib/sale.rb b/lib/sale.rb new file mode 100644 index 00000000..6ddc5567 --- /dev/null +++ b/lib/sale.rb @@ -0,0 +1,59 @@ +module FarMar + class Sale + attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id + + def initialize(id, amount, purchase_time, vendor_id, product_id) + @id = id + @amount = amount + @purchase_time = purchase_time + @vendor_id = vendor_id + @product_id = product_id + end + + def self.all + list_of_sales = [] + CSV.read("/Users/YNaka/Ada/project_forks/FarMar/support/sales.csv", "r").each do |line| + list_of_sales << self.new(line[0].to_i, line[1].to_i, DateTime.parse(line[2]), line[3].to_i, line[4].to_i) + end + list_of_sales + end + + def self.find(sale_id) + Sale.all.each do |s| + if sale_id == s.id + return s + end + end + return "That does not match any existing sale IDs" + end + + + def self.between(begin_time, end_time) + begin_time = DateTime.parse(begin_time) + end_time = DateTime.parse(end_time) + sales_between_these_times = [] + Sale.all.each do |s| + if (s.purchase_time >= begin_time && s.purchase_time <= end_time) + sales_between_these_times << s + end + end + return sales_between_these_times + end + + def vendor + FarMar::Vendor.all.each do |v| + if @vendor_id == v.id + return v + end + end + end + + def product + FarMar::Product.all.each do |p| + if @product_id == p.id + return p + end + end + end + end +end diff --git a/lib/vendor.rb b/lib/vendor.rb new file mode 100644 index 00000000..13fdfcb5 --- /dev/null +++ b/lib/vendor.rb @@ -0,0 +1,74 @@ +module FarMar + class Vendor + attr_reader :id, :name, :num_employees, :market_id + + def initialize(id, name, num_employees, market_id) + @id = id + @name = name + @num_employees = num_employees + @market_id = market_id + end + + def self.all + list_of_vendors = [] + CSV.read("/Users/YNaka/Ada/project_forks/FarMar/support/vendors.csv", "r").each do |line| + list_of_vendors << self.new(line[0].to_i, line[1], line[2].to_i, line[3].to_i) + end + list_of_vendors + end + + def self.find(vendor_id) + Vendor.all.each do |v| + if vendor_id == v.id + return v + end + end + return "That is not an existing vendor ID" + end + + def self.by_market(market_id) + vendors_at_this_market = [] + Vendor.all.each do |v| + if market_id == v.market_id + vendors_at_this_market << v.name + end + end + return vendors_at_this_market + end + + def market + FarMar::Market.all.each do |m| + if @market_id == m.id + return m + end + end + end + + def products + this_vendors_products = [] + FarMar::Product.all.each do |p| + if @id == p.vendor_id + this_vendors_products << p.name + end + end + return this_vendors_products + end + + def sales + @this_vendors_sales = [] + FarMar::Sale.all.each do |s| + if @id == s.vendor_id + @this_vendors_sales << s.amount + end + end + return @this_vendors_sales + end + + def revenue + sales + r = @this_vendors_sales.reduce(:+).to_f + r = r/100 + return "This vendor's total revenue is $#{r} " + end + end +end diff --git a/specs/market_spec.rb b/specs/market_spec.rb new file mode 100644 index 00000000..5cd88ef3 --- /dev/null +++ b/specs/market_spec.rb @@ -0,0 +1,36 @@ +require_relative 'spec_helper' + +describe FarMar::Market do + describe "#initialize" do + + it "can create a new instance of Market" do + FarMar::Market.new(1, "People's Co-op Farmers Market", "30th and Burnside", "Portland", "Multnomah", "Oregon", "Oregon").must_be_instance_of(FarMar::Market) + end + + end + + describe "all" do + it "returns all instances of Market" do + FarMar::Market.all.length.must_equal(500) + end + end + + describe "find" do + it "returns the instance of Market whose market id matches the argument's market id" do + this_market = FarMar::Market.find(20) + this_market.name.must_equal("Scottdale Farmers Market") + end + + # it "returns that market id does not exist if argument's market id does not match any existing market ids" do + # + # end + + end + + describe "#vendors" do + it "returns collection of vendors associated with that instance of Market" do + market1 = FarMar::Market.find(3) + market1.vendors.must_equal(["Kertzmann LLC", "Donnelly-Quigley", "Windler Inc"]) + end + end +end diff --git a/specs/product_spec.rb b/specs/product_spec.rb new file mode 100644 index 00000000..33192623 --- /dev/null +++ b/specs/product_spec.rb @@ -0,0 +1,51 @@ +require_relative 'spec_helper' + +describe FarMar::Product do + + describe "#initialize" do + it "can create a new instance of Product" do + FarMar::Product.new(8090, "Sour Beets",2657).must_be_instance_of(FarMar::Product) + end + end + + describe "all" do + it "returns all instances of Product" do + FarMar::Product.all.length.must_equal(8193) + end + end + + describe "find" do + it "returns the instance of Product whose product id matches the argument's product id" do + this_product = FarMar::Product.find(8162) + this_product.name.must_equal("Long Chicken") + end + end + + describe "by_vendor" do + it "returns all names of Product instances associated with the argument's vendor id" do + FarMar::Product.by_vendor(10).must_equal(["Calm Carrots", "Fierce Beef", "Helpless Bread", "Yummy Bread", "Broken Beets"]) + end + end + + describe "#vendor" do + it "returns the instance of Vendor associated with that instance of Product" do + this_product = FarMar::Product.find(250) + this_product.vendor.id.must_be_same_as(FarMar::Vendor.find(78).id) + end + end + + describe "#sales" do + it "returns the sale ids of all instances of Sale associated with that instance of Product" do + my_product = FarMar::Product.find(378) + my_product.sales.must_equal([560, 562, 564, 566, 568]) + end + end + + describe "#number_of_sales" do + it "returns the total number of instances of Sale associated with that instance of Product" do + product = FarMar::Product.find(450) + product.number_of_sales.must_equal(2) + end + end + +end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb new file mode 100644 index 00000000..401f67e6 --- /dev/null +++ b/specs/sale_spec.rb @@ -0,0 +1,43 @@ +require_relative 'spec_helper' + +describe FarMar::Sale do + describe "#initialize" do + + it "can create a new instance of Sale" do + FarMar::Sale.new(11976, 6028, "2013-11-08T08:08:48-08:00", 2684, 8170).must_be_instance_of(FarMar::Sale) + end + end + + describe "all" do + it "returns all instances of Sale" do + FarMar::Sale.all.length.must_equal(12798) + end + end + + describe "find" do + it "returns the instance of Sale whose sale id matches the argument sale id" do + this_sale = FarMar::Sale.find(35) + this_sale.amount.must_equal(8375) + end + end + + describe "between" do + it "returns all instances of Sale within the DateTimes given in the argument" do + FarMar::Sale.between("2013-11-10 04:30:00", "2013-11-10 05:00:00").length.must_equal(37) + end + end + + describe "#vendor" do + it "returns the instance of Vendor associated with that instance of Sale" do + s = FarMar::Sale.find(250) + s.vendor.id.must_equal(52) + end + end + + describe "#product" do + it "returns the instance of Product associated with that instance of Sale" do + this_sale = FarMar::Sale.find(121) + this_sale.product.id.must_equal(70) + end + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..1d9d0e3d --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,13 @@ +require 'simplecov' +SimpleCov.start + + +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' +#These are all the things about mini test that we are using +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +require_relative '../far_mar' diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb new file mode 100644 index 00000000..ff472238 --- /dev/null +++ b/specs/vendor_spec.rb @@ -0,0 +1,58 @@ +require_relative 'spec_helper' + +describe FarMar::Vendor do + describe "#initialize" do + + it "can create a new instance of Vendor" do + FarMar::Vendor.new(1584, "Quitzon-Jacobi", 3, 296).must_be_instance_of(FarMar::Vendor) + end + + end + + describe "all" do + it "returns all instances of Vendor" do + FarMar::Vendor.all.length.must_equal(2690) + end + end + + describe "find" do + it "returns the instance of Vendor whose vendor id matches the argument's vendor id" do + this_vendor = FarMar::Vendor.find(2599) + this_vendor.name.must_equal("Mills and Sons") + end + end + + describe "by_market" do + it "returns all names of Vendor instances associated with the argument's market id" do + FarMar::Vendor.by_market(4).must_equal(["Grady, Hudson and Olson", "Stracke Group", "Hyatt-King", "Homenick-Kuhn"]) + end + end + + describe "#market" do + it "returns the instance of Market associated with that instance of Vendor" do + vendor = FarMar::Vendor.find(5) + vendor.market.id.must_be_same_as(FarMar::Market.find(1).id) + end + end + + describe "#products" do + it "returns all instances of Product associated with that instance of Vendor" do + vendor1 = FarMar::Vendor.find(100) + vendor1.products.must_equal(["Combative Carrots", "Fair Fruit", "Cool Fish", "Bad Pretzel", "Hurt Burrito"]) + end + end + + describe "#sales"do + it "returns all instances of Sale associated with that instance of Vendor" do + this_vendor = FarMar::Vendor.find(100) + this_vendor.sales.must_equal([1966, 6470, 8721]) + end + end + + describe "#revenue" do + it "returns the sum of all amounts of Sale instances associated with that instance of Vendor" do + dwight = FarMar::Vendor.find(10) + dwight.revenue.must_equal("This vendor's total revenue is $326.28 ") + end + end +end