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

Branches - Brianna K #40

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
43 changes: 43 additions & 0 deletions lib/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
all_customers = []
customer_data = CSV.open('data/customers.csv', headers:false).map(&:to_a)
customer_data.each do |customer|
parameter1 = customer[0].to_i

Choose a reason for hiding this comment

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

This is another example where I'd suggest coming up with more descriptive variable names.

parameter2 = customer[1]
parameter3 = {}
parameter3[:street] = customer[2]
parameter3[:city] = customer[3]
parameter3[:state] = customer[4]
parameter3[:zip] = customer[5]
temp = Customer.new(parameter1, parameter2, parameter3)
all_customers.push(temp)
end
return all_customers
end

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


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

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

def initialize(id, products, customer, fulfillment_status = :pending)

raise ArgumentError.new("Not a valid status") unless [:pending, :paid, :processing, :shipped, :complete].include? fulfillment_status

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

end

def total()
cost_of_items = @products.values.sum
cost_of_items *= 1.075
return cost_of_items.round(2)
end

def add_product(product_name, price)
if @products.key?(product_name)
raise ArgumentError.new("This item has already been included")
else
@products[product_name]= price
end
end

def remove_product(product_name, price)
if @products.key?(product_name) == false
raise ArgumentError.new("This item has not yet been included")
else
@products.delete(product_name)
end
end

def self.all
all_orders = []
order_data = CSV.open('data/orders.csv', headers:false).map(&:to_a)
order_data.each do |order|
parameter1 = order[0].to_i
parameter3 = order[2].to_i
parameter4 = order[3].to_sym

product_string = order[1].split(';')
parameter2 = {}
product_string.each do |row|
temp = row.split(':')
parameter2[temp[0]] = temp[1].to_f
end
temp = Order.new(parameter1, parameter2, parameter3, parameter4)
all_orders.push(temp)
end
return all_orders
end

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

def self.find_by_customer(customer_id)
orders = self.all
customers_orders = []
orders.each do |order|
if order.customer == customer_id
customers_orders.push(order)
end
end
return customers_orders
end


def self.save(filename)
result = self.all
CSV.open(filename, "w") do |csv|
result.each do |row|
current_order = {}
current_order[:id] = row.id
current_order_products = row.products
product_string = ""
count = 1
current_order_products.each do |item, cost|
length = current_order_products.length
product = item + ':' + cost.to_s
product_string.concat(product)
if count < length
product_string.concat(';')
count += 1
end
end
current_order[:products] = product_string
current_order[:customer] = row.customer
current_order[:fulfillment_status] = row.fulfillment_status
csv << current_order.values
end
end
end

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

require_relative '../lib/customer'
require_relative '../lib/customer'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

Expand All @@ -15,53 +15,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 "[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 +69,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