diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..3b6ee585 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/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..85956530 --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,8 @@ +require 'csv' + +module FarMar; end + +require_relative './lib/market' +require_relative './lib/vendor' +require_relative './lib/product' +require_relative './lib/sale' diff --git a/lib/market.rb b/lib/market.rb new file mode 100644 index 00000000..64cea88b --- /dev/null +++ b/lib/market.rb @@ -0,0 +1,47 @@ +module FarMar + class Market + attr_reader :id, :name, :address, :city, :county, :state, :zip + + 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 + market_array = [] + CSV.read("/Users/yasminor/ada/Week_5/FarMar/support/markets.csv").each_with_index do |line, i| + market_array[i] = self.new(line[0], line[1], line[2], line[3], line[4], line[5], line[6]) + end + return market_array + end + + def self.find(id) + raise ArgumentError.new("invalid input type") unless id.is_a? (Fixnum) + object_array = FarMar::Market.all + object_array.map do |market| + if market.id.to_i == id + return market + end + end + end + + def vendors #returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field + FarMar::Vendor.by_market(@id) + # market_vendors_array = [] + # vendor_array = FarMar::Vendor.all #the id matches the market instance's id and the market_id of the vendors + # vendor_array.map do |vendor| + # if vendor.market_id == @id.to_s + # market_vendors_array << vendor + # end + # end + # return market_vendors_array + end + + end +end diff --git a/lib/product.rb b/lib/product.rb new file mode 100644 index 00000000..ff68c3eb --- /dev/null +++ b/lib/product.rb @@ -0,0 +1,87 @@ + +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 + product_array = [] + CSV.read("/Users/yasminor/ada/Week_5/FarMar/support/products.csv").each_with_index do |line, i| + product_array[i] = self.new(line[0].to_i, line[1], line[2].to_i) + end + return product_array + end + + def self.find(id) + raise ArgumentError.new("invalid input type") unless id.is_a? (Fixnum) + all_products = FarMar::Product.all + all_products.each do |product| + if product.id == id + return product + end + end + end + + def vendor # returns the Vendor instance that is associated with a Product instance + FarMar::Vendor.find(@vendor_id) + end + + def sales # returns a collection of Sale instances that are associated with a Product instance + product_sales_array = [] + all_sales = FarMar::Sale.all + all_sales.map do |sale| + if sale.product_id == @id + product_sales_array << sale + end + end + return product_sales_array + end + + def number_of_sales # returns the number of times this product has been sold + a_products_sales = [] + all_sales = FarMar::Sale.all + all_sales.each do |sale| + if sale.product_id == @id + a_products_sales << sale + end + end + return a_products_sales.length + end + + def self.by_vendor(vendor_id) # returns all of the products with the given vendor_id + vendor_products = [] + all_products = FarMar::Product.all + all_products.map do |product| + if product.vendor_id == vendor_id + vendor_products << product + end + return vendor_products + end + end + + + + + + + + + + + + + + + + + + + + + end +end diff --git a/lib/sale.rb b/lib/sale.rb new file mode 100644 index 00000000..2a70b23c --- /dev/null +++ b/lib/sale.rb @@ -0,0 +1,53 @@ +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 + sale_array = [] + CSV.read("/Users/yasminor/ada/Week_5/FarMar/support/sales.csv").each_with_index do |line, i| + sale_array[i] = self.new(line[0].to_i, line[1].to_i, DateTime.parse(line[2]), line[3].to_i, line[4].to_i) + end + return sale_array + end + + def self.find(id) + raise ArgumentError.new("invalid input type") unless id.is_a? (Fixnum) + all_sales = FarMar::Sale.all + all_sales.each do |sale| + if sale.id == id + return sale + end + end + end + + def vendor # returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field + #use the FarMar::Vendor.find method + FarMar::Vendor.find(@vendor_id) + end + + def product # returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field + FarMar::Product.find(@product_id) + end + + def 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 + sales_within_time_period = [] + all_sales = FarMar::Sale.all + all_sales.map do |sale| + if sale.purchase_time > beginning_time && sale.purchase_time < end_time + sales_within_time_period << sale + end + end + return sales_within_time_period + end + + + end +end diff --git a/lib/vendor.rb b/lib/vendor.rb new file mode 100644 index 00000000..9b2de08b --- /dev/null +++ b/lib/vendor.rb @@ -0,0 +1,78 @@ +module FarMar + class Vendor + attr_reader :id, :name, :no_of_employees, :market_id + + def initialize(id, name, no_of_employees, market_id) + @id = id + @name = name + @no_of_employees = no_of_employees + @market_id = market_id + end + + def self.all + vendor_array = [] + CSV.read("/Users/yasminor/ada/Week_5/FarMar/support/vendors.csv").each_with_index do |line, i| + vendor_array[i] = self.new(line[0].to_i, line[1], line[2].to_i, line[3].to_i) + end + return vendor_array + end + + def self.find(id) + raise ArgumentError.new("invalid input type") unless id.is_a? (Fixnum) + vendor_obj_array = FarMar::Vendor.all + vendor_obj_array.map do |vendor| + if vendor.id == id + return vendor + end + end + end + + def market # returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field + FarMar::Market.find(@market_id) + end + + def products #returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field + FarMar::Product.by_vendor(@id) + end + + def sales # returns a collection of FarMar::Sale instances that are associated by the vendor_id field + vendor_sales_array = [] + sales_list = FarMar::Sale.all + sales_list.map do |sale| + if sale.vendor_id == @id + vendor_sales_array << sale + end + end + return vendor_sales_array + end + + def revenue # returns the sum of all of a particular vendor's sales + vendor_sales_array = [] + sales_list = FarMar::Sale.all + sales_list.map do |sale| + if sale.vendor_id == @id + vendor_sales_array << sale + end + end + + amounts_array = [] + vendor_sales_array.map do |vendor_sale| + amounts_array << vendor_sale.amount + end + + sum = amounts_array.reduce(:+).to_i + end + + def self.by_market(market_id) # returns all the vendors with a given market_id + market_vendors = [] + all_vendors = FarMar::Vendor.all + all_vendors.map do |vendor| + if vendor.market_id == market_id + market_vendors << vendor + end + end + return market_vendors + end + + end +end diff --git a/specs/market_spec.rb b/specs/market_spec.rb new file mode 100644 index 00000000..637fd10a --- /dev/null +++ b/specs/market_spec.rb @@ -0,0 +1,114 @@ +require_relative 'spec_helper' + +describe FarMar::Market do + describe "#initialize" do + + it "should create a new instance of Market" do + pvd_market = FarMar::Market.new(101, "PVD", "139 Raindrop Road", "Providence", "PVD", "RI", "02888") + pvd_market.must_be_instance_of(FarMar::Market) + end + + # it "should have a name property" do + # sea_market = FarMar::Market.new + # sea_market.must_respond_to(:name) + # end + # + # it "should have an address property" do + # sea_market = FarMar::Market.new + # sea_market.must_respond_to(:address) + # end + # + # it "should have a city property" do + # sea_market = FarMar::Market.new + # sea_market.must_respond_to(:county) + # end + + # Tests for Market properties + it "should have an ID property" do + sea_market = FarMar::Market.new(1010, "SEA", "139 Raindrop Road", "Seattle", "X", "WA", "98109") + sea_market.must_respond_to(:id) + end + + it "should have an id that is the same value that we set" do + nyc_market = FarMar::Market.new(10101, "NYC", "139 Raindrop Road", "New York", "X", "NY", "23422") + nyc_market.id.must_equal(10101) + end + + it "should have a name that is the same value that we set" do + ist_market = FarMar::Market.new(101010, "IST", "139 Raindrop Road", "Istanbul", "BEY", "TU", "23429") + ist_market.name.must_equal("IST") + end + + it "should have an address that is the same value that we set" do + lax_market = FarMar::Market.new(1010101, "LAX", "139 Raindrop Road", "Los Angeles", "LA", "CA", "23429") + lax_market.address.must_equal("139 Raindrop Road") + end + + it "should have a city that is the same value that we set" do + mot_market = FarMar::Market.new(1010101, "MOT", "139 Raindrop Road", "Montreal", "X", "CN", "23429") + mot_market.city.must_equal("Montreal") + end + + it "should have a county that is the same value that we set" do + knx_market = FarMar::Market.new(1010101, "LAX", "139 Raindrop Road", "Knoxville", "Knox", "TN", "37922") + knx_market.county.must_equal("Knox") + end + + it "should have a state that is the same value that we set" do + knx_market = FarMar::Market.new(1010101, "LAX", "139 Raindrop Road", "Knoxville", "Knox", "TN", "37922") + knx_market.state.must_equal("TN") + end + + it "should have a zip that is the same value that we set" do + knx_market = FarMar::Market.new(1010101, "LAX", "139 Raindrop Road", "Knoxville", "Knox", "TN", "37922") + knx_market.zip.must_equal("37922") + end + end + + describe "self.all" do + + it "should return an array" do + FarMar::Market.all.must_be_kind_of(Array) + end + + it "should return an array of 500 elements" do + market_array = FarMar::Market.all + market_array.length.must_equal(500) + end + end + + describe "self.find" do + it "should raise an error if id parameter is not a fixnum" do + proc { FarMar::Market.find("107") }.must_raise(ArgumentError) + end + + it "should return an object" do + FarMar::Market.find(1).must_be_kind_of(FarMar::Market) + end + + it "should return an object whose id matches the id passed into the parameter" do + FarMar::Market.find(2).id.to_i.must_equal(2) + end + end + + describe "#vendors" do + before (:each) do + @market_one = FarMar::Market.new(1, "People's Co-op Farmers Market", "30th + and Burnside", "Portland", "Multnomah", "Oregon", 97202) + end + + it "should return an array" do + @market_one.vendors.must_be_kind_of(Array) + end + + it "should return an array of vendors whose market_ids match that of the market instance" do + @market_one.vendors.each do |vendor| + vendor.market_id.to_i.must_equal(1) + end + end + end + + + + +end diff --git a/specs/product_spec.rb b/specs/product_spec.rb new file mode 100644 index 00000000..aab4389a --- /dev/null +++ b/specs/product_spec.rb @@ -0,0 +1,146 @@ +require_relative 'spec_helper' + +describe FarMar::Product do + describe "#initialize" do + before (:each) do + @soap = FarMar::Product.new(9, "savory soap", 5) + end + + it "should be able to create a new instance of Product" do + @soap.must_be_instance_of(FarMar::Product) + end + + it "should have an id property" do + @soap.must_respond_to(:id) + end + + it "should have a name property" do + @soap.must_respond_to(:name) + end + + it "should have a vendor_id property" do + @soap.must_respond_to(:vendor_id) + end + + # it "should initialize with an id that is a fixnum" do + # + # end + # + # it "should initialize with a name that is a string" do + # + # end + # + # it "should initialize with a vendor_id that is fixnum" do + # + # end + end + + describe "self.all" do + # before (:each) do + # @bread = FarMar::Product.new(11, "gigantic bread", 6) + # end + + it "should return an array" do + FarMar::Product.all.must_be_kind_of(Array) + end + + it "should return an array comprised of only FarMar::Product instances" do + product_array = FarMar::Product.all + product_array.each do |product| + product.must_be_kind_of(FarMar::Product) + end + end + + it "should return an array of 8193 products" do + product_array = FarMar::Product.all + product_array.length.must_equal(8193) + end + end + + describe "self.find" do + it "should return a FarMar::Product instance" do + FarMar::Product.find(15).must_be_kind_of(FarMar::Product) + end + + it "should return an instance whose id matches that of the id passed into the the parameter" do + FarMar::Product.find(15).id.must_equal(15) + end + + it "should raise an error if id parameter is not a fixnum" do + proc { FarMar::Product.find("15") }.must_raise(ArgumentError) + end + end + + describe "#vendor" do + before (:each) do + @kale = FarMar::Product.new(14, "curly kale", 8) + end + + it "should return a FarMar::Vendor instance" do + @kale.vendor.must_be_kind_of(FarMar::Vendor) + end + + it "should return a Vendor instance whose id matches the vendor_id of the product the method was called on" do + @kale.vendor.id.must_equal(@kale.vendor_id) + end + end + + describe "#sales" do + before (:each) do + @collards = FarMar::Product.new(13, "collard greens", 50) + end + + it "should return an array" do + @collards.sales.must_be_kind_of(Array) + end + + it "should return a collection of FarMar::Sale objects" do + collard_sales = @collards.sales + + + collard_sales.each do |sale| + sale.must_be_instance_of(FarMar::Sale) + end + end + + it "should return a collection of Sale objects who product_ids match the id of the Product the method is called on" do + collard_sales = @collards.sales + collard_sales.each do |sale| + sale.product_id.must_equal(@collards.id) + end + end + + end + + describe "#number_of_sales" do + before (:each) do + @tomato = FarMar::Product.new(7, "tomato", 47) + end + + it "should return a number" do + @tomato.number_of_sales.must_be_kind_of(Fixnum) + end + end + + describe "self.by_vendor" do + it "should return an array" do + FarMar::Product.by_vendor(20).must_be_kind_of(Array) + end + + it "should return a collection of FarMar::Product instances" do + a_vendors_products = FarMar::Product.by_vendor(25) + a_vendors_products.each do |product| + product.must_be_instance_of(FarMar::Product) + end + end + + it "should return an array of products whose vendor ids match the vendor id passed in" do + a_vendors_products = FarMar::Product.by_vendor(20) + a_vendors_products.each do |product| + produce.vendor_id.must_equal(20) + end + end + + end + +end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb new file mode 100644 index 00000000..d7121ead --- /dev/null +++ b/specs/sale_spec.rb @@ -0,0 +1,121 @@ +require_relative 'spec_helper' + + +describe FarMar::Sale do + describe "#initialize" do + before (:each) do + @soap_sale = FarMar::Sale.new(33, 8559, "2013-11-11 06:26:33 -0800", 7, 14) + end + + it "should be able to create a new instance of" do + @soap_sale.must_be_instance_of(FarMar::Sale) + end + + it "should have an id property" do + @soap_sale.must_respond_to(:id) + end + + it "should have an amount property" do + @soap_sale.must_respond_to(:amount) + end + + it "should have an purchase_time property" do + @soap_sale.must_respond_to(:purchase_time) + end + + it "should have an vendor_id property" do + @soap_sale.must_respond_to(:vendor_id) + end + + it "should have an product_id property" do + @soap_sale.must_respond_to(:product_id) + end + + end + + describe "self.all" do + it "should return an array" do + FarMar::Sale.all.must_be_kind_of(Array) + end + + it "should return an array comprised of only FarMar::Sale instances" do + sale_array = FarMar::Sale.all + sale_array.each do |sale| + sale.must_be_kind_of(FarMar::Sale) + end + end + + it "should return an array of 12798 sales" do + product_array = FarMar::Sale.all + product_array.length.must_equal(12798) + end + end + + describe "self.find" do + it "should return a FarMar::Sale instance" do + FarMar::Sale.find(10).must_be_kind_of(FarMar::Sale) + end + + it "should return an instance whose id matches that of the id passed into the the parameter" do + FarMar::Sale.find(5).id.must_equal(5) + end + + it "should raise an error if id parameter is not a fixnum" do + proc { FarMar::Sale.find("15") }.must_raise(ArgumentError) + end + end + + describe "#vendor" do + before (:each) do + @incense_sale = FarMar::Sale.new(33, 8559, "2013-11-11 06:26:33 -0800", 7, 14) + end + + it "should return a FarMar::Vendor instance" do + @incense_sale.vendor.must_be_kind_of(FarMar::Vendor) + end + + it "should return a Vendor instance whose id matches the vendor_id of the sale the method was called on" do + @incense_sale.vendor.id.must_equal(@incense_sale.vendor_id) + end + end + + describe "#product" do + before (:each) do + @incense_sale = FarMar::Sale.new(33, 8559, "2013-11-11 06:26:33 -0800", 7, 14) + end + + it "should return a FarMar::Product instance" do + @incense_sale.product.must_be_kind_of(FarMar::Product) + end + + it "should return a Product instance whose id matches the product_id of the sale the method was called on" do + @incense_sale.product.id.must_equal(@incense_sale.product_id) + end + end + + describe "self.between" do + right_now = DateTime.now + past_datetime = DateTime.parse("2013-11-10T22:35:57-08:00") + + it "should return an array" do + FarMar::Sale.between(past_datetime, right_now).must_be_kind_of(Array) + end + + it "should return a collection of FarMar::Sale objects" do + afternoon_sales = FarMar::Sale.between(past_datetime, right_now) + afternoon_sales.each do |sale| + sale.must_be_instance_of(FarMar::Sale) + end + end + + it "should be made up only of Sale objects whose purchase times are between the beginning_time and end_time arguments" do + morning_sales = FarMar::Sale.between(past_datetime, right_now) + morning_sales.each do |sale| + sale.purchase_time.must_be(:>, past_datetime) + sale.purchase_time.must_be(:<, right_now) + end + end + + end + +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..e16cd3ae --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,12 @@ +require_relative '../far_mar' + +require 'simplecov' +SimpleCov.start + +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb new file mode 100644 index 00000000..abce9f7b --- /dev/null +++ b/specs/vendor_spec.rb @@ -0,0 +1,155 @@ +require_relative 'spec_helper' + +describe FarMar::Vendor do + describe "#initialize" do + before (:each) do + @vendor = FarMar::Vendor.new(13, "Gene Ric Farms", 4, 5) + end + + it "should be able to create a new instance of Vendor" do + @vendor.must_be_instance_of(FarMar::Vendor) + end + + it "should have an id property" do + @vendor.must_respond_to(:id) + end + + #test to see that an instance of FarMar::Vendor has name, no_of_employees, + #and market_id properties as well + end + + describe "self.all" do + it "should return an array" do + FarMar::Vendor.all.must_be_kind_of(Array) + end + + it "should return an array of FarMar::Vendor objects" do + vendors = FarMar::Vendor.all + vendors[0].must_be_kind_of(FarMar::Vendor) + end + + it "should return an array of 2690 instances" do + FarMar::Vendor.all.length.must_equal(2690) + end + end + + describe "self.find" do + it "should return a FarMar::Vendor object" do + FarMar::Vendor.find(300).must_be_kind_of(FarMar::Vendor) + end + + it "should return an instance whose id matches that of the id passed into the the parameter" do + FarMar::Vendor.find(46).id.must_equal(46) + end + + it "should raise an error if id parameter is not a fixnum" do + proc { FarMar::Vendor.find("27") }.must_raise(ArgumentError) + end + end + + describe "#market" do + before (:each) do + @vendor = FarMar::Vendor.new(13, "Gene Ric Farms", 4, 5) + end + + it "should return a FarMar::Market instance" do + @vendor.market.must_be_kind_of(FarMar::Market) + end + + it "should return a Market instance whose id matches the market_id of the vendor the method was called on" do + @vendor.market.id.to_i.must_equal(@vendor.market_id) + end + end + + describe "#products" do + before (:each) do + @windler = FarMar::Vendor.new(12, "Windler Inc", 4, 3) + end + + it "should return an array" do + @windler.products.must_be_kind_of(Array) + end + + it "should return an array made up of FarMar::Product instances" do + products_list = @windler.products + products_list.each do |product| + product.must_be_instance_of(FarMar::Product) + end + end + + it "should have only FarMar::Product instances whose vendor id matches the vendor the method is called on" do + products_list = @windler.products + products_list.each do |product| + product.vendor_id.must_equal(@windler.id) + end + end + end + + describe "#sales" do + before (:each) do + @bread_vendor = FarMar::Vendor.new(6, "Zulauf and Sons", 8, 1) + end + + it "should return an array" do + @bread_vendor.sales.must_be_kind_of(Array) + end + + it "should return a collection of FarMar::Sale instances" do + a_vendors_sales_list = @bread_vendor.sales + a_vendors_sales_list.each do |sale| + sale.must_be_instance_of(FarMar::Sale) + end + end + + # it "should return a collection of Sale instances whose vendor_ids match the id of the vendor the method is called on" do + # + # end + end + + describe "#revenue" do + before (:each) do + @bread_vendor = FarMar::Vendor.new(6, "Zulauf and Sons", 8, 1) + end + + it "should return a number" do + @bread_vendor.revenue.must_be_kind_of(Fixnum || Float) + end + + # it "should return a number equal to the sum of a vendor's sales" do + # a_vendors_sales_list = @bread_vendor.sales + # a_vendors_sales_list.each do |vendor_sale| + # amounts_array + # + # end + end + + describe "self.by_market" do + it "should return an array" do + FarMar::Vendor.by_market(20).must_be_kind_of(Array) + end + + it "should return an array of FarMar::Vendor instances" do + a_markets_vendors = FarMar::Vendor.by_market(25) + a_markets_vendors.each do |vendor| + vendor.must_be_instance_of(FarMar::Vendor) + end + end + + it "should return an array of vendors whose market ids match the market id passed in" do + a_markets_vendors = FarMar::Vendor.by_market(35) + a_markets_vendors.each do |vendor| + vendor.market_id.must_equal(35) + end + end + end + + + + + + + + + + +end