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

Leaves - Alice #32

Open
wants to merge 6 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
45 changes: 45 additions & 0 deletions lib/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'csv'

class Customer
attr_reader :id
attr_accessor :email, :address

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

def self.all
customer_list = CSV.open("data/customers.csv", "r").map do |customer|
address = {
street: "#{customer[2]}",
city: "#{customer[3]}",
state: "#{customer[4]}",
zip: "#{customer[5]}"
}
Customer.new(customer[0].to_i, "#{customer[1]}", address)
end
return customer_list
end

def self.find(id)
customer_array = self.all
found_customer = customer_array.select {|customer| customer.id == id}

return found_customer[0]
end

def self.save(filename)
total_customers = (self.all)

CSV.open(filename, "wb") do |csv|
total_customers.each do |customer|
csv << [customer.id, customer.email, customer.address[:street], customer.address[:city], customer.address[:state], customer.address[:zip]]
end
end
end

end


97 changes: 97 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
require 'csv'
require_relative 'customer'

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do all of these need to be accessors?


FULFILLMENT_STATUSES = [:pending, :paid, :processing, :shipped, :complete]

def initialize(id, products, customer, fulfillment_status = :pending)
until FULFILLMENT_STATUSES.include? fulfillment_status
raise ArgumentError.new "That fulfillment status doesn't exist."
end

@id = id
@products = products
@customer = customer
@fulfillment_status = fulfillment_status

end

def total
total_cost = (@products.values.sum)*1.075

return total_cost.round(2)
end

def add_product(product_name, product_price)
if @products.has_key? product_name
raise ArgumentError.new "That product already exists."
else
@products[product_name] = product_price
end
end

def remove_product(product_name, product_price)
if @products.has_key? product_name
@products.delete(product_name)
else
raise ArgumentError.new "That product does not exist."
end
end

def self.hash
order_hash = CSV.open("data/orders.csv", "r").map do |order|
product_array = (order[1].split(/[:;]/))

#changing number strings to floats -- is there a better way to do this
i = 1

until i > product_array.length
product_array[i] = product_array[i].to_f
i+=2
end

product_hash = Hash[*product_array]

{id: order[0].to_i, products: product_hash, customerID: Customer.find(order[2].to_i), status: "#{order[3]}"}
end

return order_hash
end

def self.all
order_list = self.hash.map do |order|
Order.new(order[:id], order[:products], order[:customerID], order[:status].to_sym)
end

return order_list
end

def self.find(id)
order_array = self.all
found_order = order_array.select {|order| order.id == id}

return found_order[0]
end

def self.find_by_customer(customer_id)
order_array = self.all
customer_orders = order_array.select {|order| order.customer.id == customer_id}

return customer_orders
end

def self.save(filename)
total_orders = self.all

CSV.open(filename, "wb") do |csv|
total_orders.each do |order|
csv << [order.id, order.products, order.customer, order.fulfillment_status]
end
end
end

end

32 changes: 16 additions & 16 deletions test/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,53 @@
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 "[email protected]"
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 "[email protected]"
expect(last.address[:street]).must_equal '7513 Kaylee Summit'
Expand All @@ -70,22 +70,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