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
40 changes: 40 additions & 0 deletions lib/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Customer

require "csv"

attr_reader :id
attr_accessor :email, :address

def initialize(id, email, address)
@id = id
@email = email
@address = address
end


def self.all
customers = CSV.read('data/customers.csv')
new_cust_array = []
customers.each do |array|
id = array[0].to_i
email = array[1]
address = {:street => "#{array[2]}", :city => "#{array[3]}", :state => "#{array[4]}", :zip => "#{array[5]}"}
new_cust_array << Customer.new(id, email, address)
end
return new_cust_array
end


def self.find(id)
customer_array = self.all
customer_array.each do |object|
if object.id == id
return object
end
end
return nil
end

end


74 changes: 74 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class Order

attr_reader :id
attr_accessor :customer, :products, :fulfillment_status

def initialize(id, products, customer, fulfillment_status = :pending)
unless fulfillment_status == :pending || fulfillment_status == :paid || fulfillment_status == :processing || fulfillment_status == :shipped || fulfillment_status == :complete
raise ArgumentError, "Not a valid fulfillment status."
end
@id = id
@customer = customer
@products = products
@fulfillment_status = fulfillment_status
end


def total
bill = 0

@products.each do |food, price|
if @products == nil
bill = 0
else
bill += price
end
end
bill_with_tax = bill * 1.075
return bill_with_tax.round(2)
end


def add_product(product_name, price)
if @products.key?(product_name) == false
@products[product_name] = price
else
raise ArgumentError, "This item is already in your cart."
end
end


def self.all
orders = CSV.read("data/orders.csv")
new_order_array = []
orders.each do |one_order_object|
id = one_order_object[0].to_i

products = one_order_object[1].split(";")
product_hash = {}
products.each do |one_product|
key_and_value = one_product.split(":")
product_hash[key_and_value[0]] = key_and_value[1].to_f
end
products = product_hash

customer = Customer.find(one_order_object[2].to_i)
fulfillment_status = one_order_object[3].to_sym
new_order_array << Order.new(id, products, customer, fulfillment_status)
end
return new_order_array
end


def self.find(id)
order_array = Order.all

order_array.each do |object|
if object.id == id
return object
end
end
return nil
end

end
34 changes: 18 additions & 16 deletions test/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
require 'minitest/reporters'
require 'minitest/skip_dsl'

require 'pry'


require_relative '../lib/customer'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
Expand All @@ -15,53 +18,52 @@
state: "WA",
zip: "98101"
}.freeze

describe "#initialize" do
it "Takes an ID, email and address info" do
cust = Customer.new(ID, EMAIL, ADDRESS)

expect(cust).must_respond_to :id
expect(cust.id).must_equal ID

expect(cust).must_respond_to :email
expect(cust.email).must_equal EMAIL

expect(cust).must_respond_to :address
expect(cust.address).must_equal ADDRESS
end
end
end

# TODO: remove the 'x' in front of this block when you start wave 2
xdescribe "Customer Wave 2" do
describe "Customer Wave 2" do
describe "Customer.all" do
it "Returns an array of all customers" do
customers = Customer.all

expect(customers.length).must_equal 35
customers.each do |c|
expect(c).must_be_kind_of Customer

expect(c.id).must_be_kind_of Integer
expect(c.email).must_be_kind_of String
expect(c.address).must_be_kind_of Hash
end
end

it "Returns accurate information about the first customer" do
first = Customer.all.first

expect(first.id).must_equal 1
expect(first.email).must_equal "leonard.rogahn@hagenes.org"
expect(first.address[:street]).must_equal "71596 Eden Route"
expect(first.address[:city]).must_equal "Connellymouth"
expect(first.address[:state]).must_equal "LA"
expect(first.address[:zip]).must_equal "98872-9105"
end

it "Returns accurate information about the last customer" do
last = Customer.all.last

expect(last.id).must_equal 35
expect(last.email).must_equal "rogers_koelpin@oconnell.org"
expect(last.address[:street]).must_equal '7513 Kaylee Summit'
Expand All @@ -70,22 +72,22 @@
expect(last.address[:zip]).must_equal '64529-2614'
end
end

describe "Customer.find" do
it "Can find the first customer from the CSV" do
first = Customer.find(1)

expect(first).must_be_kind_of Customer
expect(first.id).must_equal 1
end

it "Can find the last customer from the CSV" do
last = Customer.find(35)

expect(last).must_be_kind_of Customer
expect(last.id).must_equal 35
end

it "Returns nil for a customer that doesn't exist" do
expect(Customer.find(53145)).must_be_nil
end
Expand Down
Loading