You are a startup founder building a new online marketplace that connects small, independent farmers with consumers who are passionate about buying local and supporting sustainable agriculture. In this marketplace, farmers will be able to create profiles and list their products, and consumers will be able to browse and purchase products directly from the farmers.
- You can run any of the files with the command
node PATH_TO_FILE
. - To run the tests, do the following in the root folder of this project:
npm install
npm test
- In
Product.js
, create a class calledProduct
using the following class diagram above.Product
should have the following properties and methods:- Properties
name
,price
, anddescription
are set by the constructor method.inStock
is set totrue
when the instance of the class is created.
- Methods
display()
returns a string in the following format"Name: <NAME>, Price: $<PRICE>, Description: <DESCRIPTION>"
.
- Properties
- Export the class using
module.exports
. - Import the class into
index.js
with the nameProduct
. - Save and run
npm test
. The first 3 tests should now pass.
const carrots = new Product("Carrots", 4, "Bushel of carrots that have been freshly harvested for you");
carrots.inStock; // true
carrots.display(); // "Name: Carrots, Price: $4, Description: Bushel of carrots that have been freshly harvested for you"
As your online marketplace grows, you realize that users need a way to save products they want to buy for later. To address this, you decide to build a Cart
object that will allow users to add and remove products.
- In
Cart.js
, create a class calledCart
using the class diagram above.Cart
should have the following properties and methods:- Properties
products
: An array that will store instances ofProduct
that have been added to the cart. Starts as an empty array.total
: A number representing the total cost of all products in the cart. Starts with a value of 0.
- Methods
addProduct
: A method that adds aProduct
instance to the end of theproducts
array and updates thetotal
property accordingly.removeProduct(i)
: A method that removes aProduct
instance from theproducts
array at a specified indexi
and updates thetotal
property accordingly.
- Properties
- Export the class using
module.exports
. - Import the class into
index.js
with the nameCart
. - Save and run
npm test
. 6 tests should now pass.
const strawberries = new Product("Strawberries", 5, "The freshest fresas on the market");
const carrots = new Product("Carrots", 2, "Perfect for an afternoon snack");
const mangos = new Product("Mangos", 3, "The tastiest fruit you can buy");
const myCart = new Cart();
myCart.addProduct(strawberries);
myCart.addProduct(mangos);
myCart.products; // [Product { ... }, Product { ... }]
myCart.total; // 8
myCart.removeProduct(1);
myCart.products; // [Product { ... }]
myCart.total; // 5
With the Product
and Cart
classes in place, you've got the basic building blocks for a fully functional online marketplace. However, you're missing a key piece of the puzzle: customers!
-
Every
Customer
can has aCart
that can contain manyProduct
items. InCustomer.js
, create a class calledCustomer
using the class diagram above.Customer
should have the following properties and methods:- Properties
name
,email
, andshippingAddress
are set by the constructor method.orderHistory
contains an array of pastCart
items. It initializes as an empty array.
- Methods
addToOrderHistory(cart)
: adds aCart
instance to the end of theorderHistory
array.
- Properties
-
In
Customer.js
, export theCustomer
class usingmodule.exports
. -
Import the class into
index.js
with the nameCustomer
. -
Save and run
npm test
. 9 tests should now pass.
const melanie = new Customer("Melanie", "melanie@gmail.com", "22 Main St");
const strawberries = new Product("Strawberries", 5, "The freshest fresas on the market");
const carrots = new Product("Carrots", 2, "Perfect for an afternoon snack");
const mangos = new Product("Mangos", 3, "The tastiest fruit you can buy");
const myFirstOrder = new Cart();
myFirstOrder.addProduct(mangos);
myFirstOrder.addProduct(carrots);
const mySecondOrder = new Cart();
mySecondOrder.addProduct(strawberries);
melanie.addToOrderHistory(myFirstOrder);
melanie.addToOrderHistory(mySecondOrder);
melanie.orderHistory;
/*
[
Cart { products: [ [Product], [Product] ], total: 5 },
Cart { products: [ [Product] ], total: 5 },
]
*/
With the Product
, Cart
, and Customer
classes in place, your online marketplace is starting to take shape. However, we need to verify the customers are who they say they are. We will do this with an Auth
class.
- In
Auth.js
, create a class calledAuth
using the class diagram above. Create a new classAuth
with the following properties and methods:- Properties
customers
: an array that will store instances of theCustomer
class representing all registered customers. Initializes as an empty array.
- Methods
register(name, email, shippingAddress)
: a method that creates a newCustomer
instance with the provided information and adds it to thecustomers
arraylogin(email)
: a method that takes anemail
as an argument and returns the correspondingCustomer
instance from thecustomers
array.
- Properties
- Export the
Auth
class usingmodule.exports
. - Import the class into
index.js
with the nameAuth
. - Save and run
npm test
. All tests should now pass.
let auth = new Auth();
auth.register("Kaiya", "Kaiya@example.com", '121 Main St');
auth.register("Nina", "Nina@example.com", '22 Broadway St');
console.log(auth.login("Kaiya@example.com"));
/*
{ name: 'Kaiya', email: 'jKaiya@example.com', shippingAddress: '121 Main St' }
}
*/
console.log(auth.login("benny@example.com")); // null
Business is booming! Here are few extensions you can add to your existing code:
- Add a
getTotal
method to theCart
class that returns the total price of all items in the cart. - Add a
clear
method to theCart
class that clears allproducts
from the cart and setstotal
back to0
. - Add a
rewardPoints
property to theProduct
class constructor that denotes how many reward points each item gets. In theCustomer
class:- Create a
rewardPoints
property that initializes with a value of zero. - Create a
getRewardPoints
method that goes through theorderHistory
and updatesrewardPoints
with the number ofrewardsPoints
that the customer has earned based on their order history.
- Create a
- Add a
removeItemByName
method to theCart
class that removes a specified item from the cart. This method should take in aProduct
name as an argument and remove that item from the cart. quantity
: We have more than 1 of many items in stock. Do the following:- Update
Product
constructor to accept a property ofquantity
. - Update the
Cart
methodaddProduct(product, quantity)
to accept aquantity
argument. The method should:- Check that the
product
has enough quantity to complete the command. If the amount requested is greater than the amount available returnI'm sorry there are only QUANTITY of this product left
. - If there is enough of the
quantity
, add the item to the cart, increase thetotal
, and then decrease the products quantity. - Check if
product
now has a quantity of0
. If it does, setinStock
tofalse
. - Return the updated
Product
.
- Check that the
- Update