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 - Erika #39

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



class Customer

attr_reader :id, :customer
attr_accessor :email, :address, :customers



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


def self.all
csv_data = CSV.read("data/customers.csv")
customer_data = []

csv_data.each do |index|
id = index[0].to_i
email = index[1]
address = {
street: index[2],
city: index[3],
state: index[4],
zip: index[5]
}
individual_customer = Customer.new(id, email, address)
customer_data << individual_customer
end

return customer_data
end

def self.find(id)

customer_database = self.all
return customer_database.find { |customer| customer.id == id }
end
end

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

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


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

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

if id.is_a?(Integer) == false
raise ArgumentError
elsif
products.is_a?(Hash) == false
raise ArgumentError
# elsif
# customer.is_a?(Integer) == false
# raise ArgumentError
elsif
!(status_array.include?(fulfillment_status))
raise ArgumentError
end

end

def add_product(product_name, price)
if @products.keys.include?(product_name)
raise ArgumentError
else
@products[product_name] = price
end
return @products
end

def total
cost_array = []
@products.each do |product_name, cost|
cost_array << cost
end

sub_total = cost_array.sum
taxed = sub_total.to_f * 1.075
return taxed.round(2)
end

# def self.all(id, products, customer, fulfillment_status)




# orders = csv_data.map do |order|

def self.all
total_orders = []

csv_data = CSV.read("data/orders.csv")
csv_data.each do |order|
isolate_order_products = []
product_hash = {}
id = order[0].to_i
isolate_order_products = order.slice(1)
customer_id = order[2].to_i
fulfillment_status = order[3].to_sym

single_product = isolate_order_products.split(";")
single_product.each do |index|
isolate_value = index.split(":")
product_hash.store(
isolate_value[0], isolate_value[1].to_f
)
end
customer = Customer.find(customer_id)

single_order = Order.new(id, product_hash, customer, fulfillment_status)
total_orders << single_order


end
return total_orders
end


def self.find(id)
order_database = self.all
return order_database.find { |order| order.id == id }
end

end









123 changes: 123 additions & 0 deletions lib/order_prying.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
require 'pry'
require 'csv'
require 'awesome_print'

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

# instance of Customer, the person who placed the order
# fulfillment_status, a symbol of :pending, :paid, :processing, :shipped, :complete
# if there is not fulfilment_status, default to :pending
# otherwise, ArgumentError should be raised

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

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

if id.is_a?(Integer) == false
raise ArgumentError
elsif
products.is_a?(Hash) == false
raise ArgumentError
elsif
customer.is_a?(Customer) == false
raise ArgumentError
elsif
!(status_array.include?(fulfillment_status))
raise ArgumentError
end

end

def add_product(product_name, price)
if @products.keys.include?(product_name)
raise ArgumentError
else
@products[product_name] = price
end
return @products
end

def total
cost_array = []
@products.each do |product_name, cost|
cost_array << cost
end

sub_total = cost_array.sum
taxed = sub_total.to_f * 1.075
return taxed.round(2)
end

# def self.all(id, products, customer, fulfillment_status)




# orders = csv_data.map do |order|

def self.all

csv_data = CSV.read("data/orders.csv").map(&:to_a)
csv_data.each do |order|

isolate_order_products = []
organized_products = []

id = order[0].to_i
isolate_order_products = order.slice(1)
customer_id = order[2].to_i
fulfillment_status = order[3].to_sym

single_product = isolate_order_products.split(";")

single_product.each do |index|
isolate_value = index.split(":")
item_hash = {
isolate_value[0] => isolate_value[1].to_f
}
organized_products << item_hash
end

single_order = Order.new(id, organized_products, customer_id, fulfillment_status)

total_orders << single_order

end
return total_orders
end

# id = order[0].to_i
# products = {}
# products = order[1]
# customer = order[2].to_i
# fulfullment_status = order[3]
# end

# end
end

# instance = Order.all
# should have method called total
# summing up the products
# adding a 7.5% tax
# rounding the result to two decimal places














64 changes: 32 additions & 32 deletions test/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,58 @@
ID = 123
EMAIL = "[email protected]"
ADDRESS = {
street: "123 Main",
city: "Seattle",
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
street: "123 Main",
city: "Seattle",
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