forked from AdaGold/grocery-store
-
Notifications
You must be signed in to change notification settings - Fork 50
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
sun-alice
wants to merge
6
commits into
Ada-C12:master
Choose a base branch
from
sun-alice:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Leaves - Alice #32
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a866c00
completed wave 1
sun-alice 8efa498
added optional enhancement: remove product
sun-alice 4770b8c
updated self.all function
sun-alice 29f370b
added tests to order_test.rb
sun-alice aa8a5ac
added find_by_customer method and tests
sun-alice 20c6a9b
added csv writing functions
sun-alice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?