From 62ecfac4c935566a165df9cd945c828b7748e8cf Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 9 Sep 2024 16:35:11 -0400 Subject: [PATCH 1/2] doozy --- classes/Auth.js | 19 +++++++++++++ classes/Cart.js | 26 ++++++++++++++++++ classes/Customer.js | 18 +++++++++++++ classes/Product.js | 14 ++++++++++ index.js | 65 ++++++++++++++++++--------------------------- package-lock.json | 3 ++- 6 files changed, 105 insertions(+), 40 deletions(-) diff --git a/classes/Auth.js b/classes/Auth.js index e69de29..4170e3c 100644 --- a/classes/Auth.js +++ b/classes/Auth.js @@ -0,0 +1,19 @@ +const Customer = require("./Customer"); + +class Auth { + constructor() { + this.customers = []; + } + + register(name, email, shippingAddress) { + const newCustomer = new Customer(name, email, shippingAddress); + this.customers.push(newCustomer); + return newCustomer; + } + + login(email) { + return this.customers.find((customer) => customer.email === email) || null; + } +} + +module.exports = Auth; diff --git a/classes/Cart.js b/classes/Cart.js index e69de29..14e8085 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -0,0 +1,26 @@ +const Product = require("./Product"); + +class Cart { + constructor() { + this.products = []; + this.total = 0; + } + + addProduct(product) { + if (product instanceof Product) { + this.products.push(product); + this.total += product.price; + } + } + + removeProduct(product) { + const index = this.products.indexOf(product); + if (index > 0) { + this.total -= this.products[index].price; + this.products.splice(index, 1); + } + console.log(index); + } +} + +module.exports = Cart; diff --git a/classes/Customer.js b/classes/Customer.js index e69de29..629ad8a 100644 --- a/classes/Customer.js +++ b/classes/Customer.js @@ -0,0 +1,18 @@ +const Cart = require("./Cart"); + +class Customer { + constructor(name, email, shippingAddress) { + this.name = name; + this.email = email; + this.shippingAddress = shippingAddress; + this.orderHistory = []; + } + + addToOrderHistory(cart) { + if (cart instanceof Cart) { + this.orderHistory.push(cart); + } + } +} + +module.exports = Customer; diff --git a/classes/Product.js b/classes/Product.js index e69de29..1236d56 100644 --- a/classes/Product.js +++ b/classes/Product.js @@ -0,0 +1,14 @@ +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..495418d 100644 --- a/index.js +++ b/index.js @@ -1,49 +1,36 @@ // Import Classes Here - - - - - - - - - +const Product = require("./classes/Product"); +const Cart = require("./classes/Cart"); +const Customer = require("./classes/Customer"); +const Auth = require("./classes/Auth"); // DO NOT EDIT BELOW THIS LINE try { - module.exports = { - Product, - } -} catch(e){ - -} + module.exports = { + Product, + }; +} catch (e) {} try { - module.exports = { - Product, - Cart - } -} catch(e){ - -} + module.exports = { + Product, + Cart, + }; +} catch (e) {} try { - module.exports = { - Product, - Cart, - Customer - } -} catch(e){ - -} + module.exports = { + Product, + Cart, + Customer, + }; +} catch (e) {} try { - module.exports = { - Product, - Cart, - Customer, - Auth - } -} catch(e){ - -} \ No newline at end of file + module.exports = { + Product, + Cart, + Customer, + Auth, + }; +} catch (e) {} diff --git a/package-lock.json b/package-lock.json index 9b68881..5713249 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4717,7 +4717,8 @@ "jest-pnp-resolver": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz", - "integrity": "sha512-+3NpwQEnRoIBtx4fyhblQDPgJI0H1IEIkX7ShLUjPGA7TtUTvI1oiKi3SR4oBR0hQhQR80l4WAe5RrXBwWMA8w==" + "integrity": "sha512-+3NpwQEnRoIBtx4fyhblQDPgJI0H1IEIkX7ShLUjPGA7TtUTvI1oiKi3SR4oBR0hQhQR80l4WAe5RrXBwWMA8w==", + "requires": {} }, "jest-regex-util": { "version": "29.4.3", From 42501f859763c51483628925b2a0322d8444896f Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 10 Sep 2024 10:44:10 -0400 Subject: [PATCH 2/2] removeProduct works --- classes/Cart.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/classes/Cart.js b/classes/Cart.js index 14e8085..00a7b62 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -13,13 +13,10 @@ class Cart { } } - removeProduct(product) { - const index = this.products.indexOf(product); - if (index > 0) { - this.total -= this.products[index].price; - this.products.splice(index, 1); - } - console.log(index); + removeProduct(i) { + if (i > this.products.length) return "Product is no longer in stock"; + this.total -= this.products[i].price; + this.products.splice(i, 1); } }