From 4b15806a3bfe764fd28790cbd0c44be1ae0a8cdb Mon Sep 17 00:00:00 2001 From: jamesmulero Date: Wed, 8 May 2024 09:58:52 +0100 Subject: [PATCH 1/2] solution addedJamesJuliet --- classes/Cart.js | 18 ++++++++++++++++++ classes/Customer.js | 14 ++++++++++++++ classes/Product.js | 13 +++++++++++++ index.js | 23 ++++++++++++++--------- 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/classes/Cart.js b/classes/Cart.js index e69de29..7b33dbb 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -0,0 +1,18 @@ +class Cart { + constructor(){ + this.products = [] + this.total = 0 +} +addProduct(product) { + this.products.push(product); + this.total += product.price; +} +removeProduct(product) { + const index = this.products.findIndex (p => p === product) + if (index !== -1) { + this.products.splice (index, 1) + this.total -= product.price + } +} +} +module.exports = Cart \ No newline at end of file diff --git a/classes/Customer.js b/classes/Customer.js index e69de29..eb519e3 100644 --- a/classes/Customer.js +++ b/classes/Customer.js @@ -0,0 +1,14 @@ +class Customer { + constructor(name, email, shippingAddress) { + this.name = name; + this.email = email; + this.shippingAddress = shippingAddress; + this.orderHistory = []; // Initializes as an empty array + } + + addToOrderHistory(cart) { + this.orderHistory.push(cart); + } + } + + module.exports = Customer; \ No newline at end of file diff --git a/classes/Product.js b/classes/Product.js index e69de29..b2926ad 100644 --- a/classes/Product.js +++ b/classes/Product.js @@ -0,0 +1,13 @@ +class Product { + constructor(name, price, description){ + this.name = name; + this.price = price; + this.description = description; + this.inStock = true; + } + + display(){ + return `Name: ${this.name}, Price: $${this.price}, Description: ${this.description}` + }; + } + module.exports = Product; diff --git a/index.js b/index.js index efad601..ede8b47 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,18 @@ // Import Classes Here - - - - - - - - - +const Product = require('./Product'); +// Example of creating a new Product instance and using the display method +let myProduct = new Product("Example Product", 19.99, "This is a sample description."); +console.log(myProduct.display()); +// +const Cart = require('./Cart'); +// Example usage +const cart = new Cart(); +console.log(cart); // Initially empty cart +// +const Customer = require('./Customer'); +// Example usage +let customer = new Customer("Jane Doe", "jane.doe@example.com", "123 Elm Street"); +console.log(customer); // DO NOT EDIT BELOW THIS LINE try { From 61a8fb3b10c36e951b0938605635a9d40822f013 Mon Sep 17 00:00:00 2001 From: Julesyasaby <166526374+Julesyasaby@users.noreply.github.com> Date: Mon, 13 May 2024 17:33:40 +0100 Subject: [PATCH 2/2] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ede8b47..d07576c 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ // Import Classes Here const Product = require('./Product'); // Example of creating a new Product instance and using the display method -let myProduct = new Product("Example Product", 19.99, "This is a sample description."); +let myProduct = new Product('Example Product', 19.99, 'This is a sample description.'); console.log(myProduct.display()); // const Cart = require('./Cart'); @@ -11,7 +11,7 @@ console.log(cart); // Initially empty cart // const Customer = require('./Customer'); // Example usage -let customer = new Customer("Jane Doe", "jane.doe@example.com", "123 Elm Street"); +let customer = new Customer('Jane Doe', 'jane.doe@example.com', '123 Elm Street'); console.log(customer); // DO NOT EDIT BELOW THIS LINE