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 calledProductusing the following class diagram above.Productshould have the following properties and methods:- Properties
name,price, anddescriptionare set by the constructor method.inStockis set totruewhen 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.jswith 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 calledCartusing the class diagram above.Cartshould have the following properties and methods:- Properties
products: An array that will store instances ofProductthat 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 aProductinstance to the end of theproductsarray and updates thetotalproperty accordingly.removeProduct(i): A method that removes aProductinstance from theproductsarray at a specified indexiand updates thetotalproperty accordingly.
- Properties
- Export the class using
module.exports. - Import the class into
index.jswith 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; // 5With 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
Customercan has aCartthat can contain manyProductitems. InCustomer.js, create a class calledCustomerusing the class diagram above.Customershould have the following properties and methods:- Properties
name,email, andshippingAddressare set by the constructor method.orderHistorycontains an array of pastCartitems. It initializes as an empty array.
- Methods
addToOrderHistory(cart): adds aCartinstance to the end of theorderHistoryarray.
- Properties
-
In
Customer.js, export theCustomerclass usingmodule.exports. -
Import the class into
index.jswith 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 calledAuthusing the class diagram above. Create a new classAuthwith the following properties and methods:- Properties
customers: an array that will store instances of theCustomerclass representing all registered customers. Initializes as an empty array.
- Methods
register(name, email, shippingAddress): a method that creates a newCustomerinstance with the provided information and adds it to thecustomersarraylogin(email): a method that takes anemailas an argument and returns the correspondingCustomerinstance from thecustomersarray.
- Properties
- Export the
Authclass usingmodule.exports. - Import the class into
index.jswith 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")); // nullBusiness is booming! Here are few extensions you can add to your existing code:
- Add a
getTotalmethod to theCartclass that returns the total price of all items in the cart. - Add a
clearmethod to theCartclass that clears allproductsfrom the cart and setstotalback to0. - Add a
rewardPointsproperty to theProductclass constructor that denotes how many reward points each item gets. In theCustomerclass:- Create a
rewardPointsproperty that initializes with a value of zero. - Create a
getRewardPointsmethod that goes through theorderHistoryand updatesrewardPointswith the number ofrewardsPointsthat the customer has earned based on their order history.
- Create a
- Add a
removeItemByNamemethod to theCartclass that removes a specified item from the cart. This method should take in aProductname 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
Productconstructor to accept a property ofquantity. - Update the
CartmethodaddProduct(product, quantity)to accept aquantityargument. The method should:- Check that the
producthas 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
productnow has a quantity of0. If it does, setinStocktofalse. - Return the updated
Product.
- Check that the
- Update




