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 - Mariya #44
Open
M-Burr
wants to merge
5
commits into
Ada-C12:master
Choose a base branch
from
M-Burr: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 - Mariya #44
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b0a7cb5
added (first draft) working wave 1 of grocery store project
M-Burr 6b10728
completed class methods for customer class
M-Burr fa55293
added completed wave 2 & wave 3
M-Burr 303e577
finished testing and some optional assignment components
M-Burr ac6a4be
add some comments
M-Burr 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,27 @@ | ||
require 'csv' | ||
|
||
class Customer | ||
attr_accessor :email, :address | ||
attr_reader :id | ||
|
||
def self.all | ||
customer_list = [] | ||
CSV.read('data/customers.csv').each do |customer| | ||
customer_object = Customer.new(customer[0].to_i, customer[1], {street: customer[2], city: customer[3], state: customer[4], zip: customer[5]}) | ||
customer_list << customer_object | ||
end | ||
return customer_list | ||
end | ||
|
||
def self.find(id) | ||
Customer.all.find do |customer_object| | ||
customer_object.id == id ? customer_object : nil | ||
end | ||
end | ||
|
||
def initialize(id, email, address) | ||
@id = id | ||
@email = email | ||
@address = address | ||
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,93 @@ | ||
require_relative 'customer' | ||
require 'csv' | ||
|
||
|
||
class Order | ||
attr_reader :id, :products | ||
attr_accessor :customer, :fulfillment_status | ||
|
||
def initialize(id, products, customer, fulfillment_status = :pending) | ||
@id = id | ||
@products = products | ||
@customer = customer | ||
@fulfillment_status = fulfillment_status | ||
|
||
#raises Argument Error for invalid status | ||
valid_statuses = %i[pending paid processing shipped complete] | ||
if !(valid_statuses.include?(@fulfillment_status)) | ||
raise ArgumentError.new("Invalid status.") | ||
end | ||
end | ||
|
||
def self.all | ||
orders_list = [] | ||
#splits string at the ";" to seperate each product/cost pair | ||
CSV.read('data/orders.csv').each do |order| | ||
food_price_pairs = order[1].split(";") | ||
|
||
products = {} | ||
food_price_pairs.each do |food| | ||
food_price = food.split(":") #splits the product & cost so that the price so that it can be added into a hash & the price can be converted into a float | ||
products["#{food_price[0]}"] = food_price[1].to_f | ||
end | ||
|
||
order_object = Order.new(order[0].to_i, products, Customer.find(order[2].to_i), order[3].to_sym) | ||
orders_list << order_object | ||
end | ||
return orders_list | ||
end | ||
|
||
def self.find(id) | ||
Order.all.each do |order| | ||
if order.id == (id) | ||
return order | ||
end | ||
end | ||
return nil | ||
end | ||
|
||
def self.find_by_customer(customer_id) | ||
customer_orders = [] | ||
Order.all.each do |order| | ||
if order.customer.id == customer_id | ||
customer_orders << order | ||
end | ||
end | ||
|
||
if customer_orders.length == 0 | ||
raise ArgumentError.new("Invalid customer ID.") | ||
end | ||
return customer_orders | ||
end | ||
|
||
|
||
def total | ||
total = 0 | ||
@products.each do |product, amount| | ||
total += amount | ||
end | ||
|
||
tax_amount = total * 0.075 | ||
total = total + tax_amount | ||
return total.round(2) | ||
end | ||
|
||
def add_product(product_name, product_price) | ||
if @products.has_key?(product_name) | ||
raise ArgumentError.new("This product has already been added.") | ||
else | ||
@products[product_name] = product_price | ||
end | ||
|
||
return @products | ||
end | ||
|
||
def remove_products(product_name, product_price) | ||
if @products.has_key?(product_name) | ||
@products.delete(product_name) | ||
else | ||
raise ArgumentError.new("This product is not in our inventory.") | ||
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
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 |
---|---|---|
|
@@ -113,13 +113,55 @@ | |
end | ||
end | ||
|
||
# TODO: change 'xdescribe' to 'describe' to run these tests | ||
xdescribe "Order Wave 2" do | ||
# test for optional remove_products | ||
describe "#remove_products" do | ||
it "Decreases the number of products" do | ||
products = { "Eggs" => 84.23, "Watermelon" => 11.16, "Cherries" => 10.4 } | ||
before_count = products.length | ||
customer = Customer.find(7) | ||
order = Order.new(7, products, customer) | ||
|
||
order.remove_products("Eggs", 84.23) | ||
after_count = before_count - 1 | ||
expect(order.products.include?("Eggs")).must_equal false | ||
|
||
end | ||
|
||
it "Raises an ArgumentError if the product is not present" do | ||
products = { "Eggs" => 84.23, "Watermelon" => 11.16, "Cherries" => 10.4 } | ||
|
||
customer = Customer.find(7) | ||
order = Order.new(7, products, customer) | ||
|
||
expect { | ||
order.remove_products("banana", 4.25) | ||
}.must_raise ArgumentError | ||
|
||
# The list of products should not have been modified | ||
expect(order.total).must_equal 113.72 | ||
end | ||
end | ||
|
||
describe "Order Wave 2" do | ||
describe "Order.all" do | ||
it "Returns an array of all orders" do | ||
# TODO: Your test code here! | ||
end | ||
# get all instances of orders | ||
orders = Order.all | ||
# there should be 100 orders | ||
expect(orders.length).must_equal 100 | ||
# confirm data types of attributes | ||
|
||
orders.each do |order| | ||
expect(order).must_be_kind_of Order | ||
expect(order.id).must_be_kind_of Integer | ||
expect(order.products).must_be_kind_of Hash | ||
expect(order.customer).must_be_kind_of Customer | ||
expect(order.fulfillment_status).must_be_kind_of Symbol | ||
end | ||
|
||
|
||
end | ||
|
||
it "Returns accurate information about the first order" do | ||
id = 1 | ||
products = { | ||
|
@@ -141,21 +183,53 @@ | |
end | ||
|
||
it "Returns accurate information about the last order" do | ||
# TODO: Your test code here! | ||
# assign the last order object to value last_order | ||
last_order = Order.all.last | ||
|
||
# check that all data returned was matched to the last line (array) of the CSV file. | ||
expect(last_order.id).must_equal 100 | ||
expect(last_order.products).must_equal ({"Amaranth" => 83.81, "Smoked Trout" => 70.6, "Cheddar" => 5.63}) | ||
expect(last_order.customer.id).must_equal 20 | ||
expect(last_order.fulfillment_status).must_equal :pending | ||
end | ||
end | ||
|
||
describe "Order.find" do | ||
it "Can find the first order from the CSV" do | ||
# TODO: Your test code here! | ||
#first is assigned the value of calling on Order.find | ||
first = Order.find(1) | ||
|
||
# check expected | ||
expect(first.id).must_equal 1 | ||
expect(first.products).must_equal ({"Lobster" => 17.18, "Annatto seed" => 58.38, "Camomile"=> 83.21}) | ||
expect(first.customer.id).must_equal 25 | ||
expect(first.fulfillment_status).must_equal :complete | ||
end | ||
|
||
it "Can find the last order from the CSV" do | ||
# TODO: Your test code here! | ||
# last is assigned to the result of calling Order.find | ||
last = Order.find(100) | ||
|
||
# check expected values vs. actual values | ||
expect(last.id).must_equal 100 | ||
expect(last.products).must_equal ({"Amaranth" => 83.81, "Smoked Trout" => 70.6, "Cheddar" => 5.63}) | ||
expect(last.customer.id).must_equal 20 | ||
expect(last.fulfillment_status).must_equal :pending | ||
end | ||
|
||
it "Returns nil for an order that doesn't exist" do | ||
# TODO: Your test code here! | ||
|
||
# optional enhancement | ||
it "Can find the customer from the customer id" do | ||
customer_orders = Order.find_by_customer(24) | ||
|
||
expect((customer_orders.all? { |orders| orders.customer.id == 24 })).must_equal true | ||
end | ||
|
||
|
||
it "Returns nil for an order that doesn't exist" do | ||
no_order = Order.find(101) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nice to give a name to the value |
||
|
||
expect(no_order).must_equal nil | ||
end | ||
end | ||
end | ||
|
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.
This looks good! In case you want something shorter that does exactly what you implemented, you might want to check out: https://ruby-doc.org/core-2.6.1/Enumerable.html#method-i-find