diff --git a/classes/Auth.js b/classes/Auth.js index e69de29..ae9009b 100644 --- a/classes/Auth.js +++ b/classes/Auth.js @@ -0,0 +1,21 @@ +const Customer = require("./Customer"); + +class Auth { + constructor() { + this.customers = []; + } + register(name, email, shippingAddress) { + const newCustomer = new Customer(name, email, shippingAddress); + this.customers.push(newCustomer); + } + login(email) { + for (const person of this.customers) { + if (person.email === email) { + return person; + } + } + return null; + } +} + +module.exports = Auth; diff --git a/classes/Cart.js b/classes/Cart.js index e69de29..2ad45cf 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -0,0 +1,44 @@ +class Cart { + constructor() { + this.products = []; + this.total = 0; + } + + addProduct(product, quantity) { + if (quantity > 0) { + this.products.push(product); + this.total += product.price * quantity; + } + } + + removeProduct(i, quantity) { + const product = this.products[i]; + if (quantity >= product.quantity) { + //this.products.splice(i, 1); + this.total -= product.price * quantity; + product.quantity -= quantity; + if (product.quantity === 0) { + product.inStock = false; + } + return product; + } else { + return `I'm sorry there are only ${product.quantity} of this product left`; + } + } + getTotal() { + let sum = 0; + for (let i = 0; i < this.products.length; i++) { + sum += this.products[i].price; + } + return sum; + } + clear() { + this.products = []; + this.total = 0; + } + removeItemByName(product) { + return this.products.filter((item) => item.name !== product.name); + } +} + +module.exports = Cart; diff --git a/classes/Customer.js b/classes/Customer.js index e69de29..d34447c 100644 --- a/classes/Customer.js +++ b/classes/Customer.js @@ -0,0 +1,19 @@ +class Customer { + constructor(name, email, shippingAddress) { + this.name = name; + this.email = email; + this.shippingAddress = shippingAddress; + this.orderHistory = []; + this.rewardsPoints = 0; + } + addToOrderHistory(cart) { + this.orderHistory.push(cart); + } + getRewardPoints() { + for (const order of this.orderHistory) { + this.rewardsPoints += order.rewardPoints; + } + } +} + +module.exports = Customer; diff --git a/classes/Product.js b/classes/Product.js index e69de29..8640c18 100644 --- a/classes/Product.js +++ b/classes/Product.js @@ -0,0 +1,16 @@ +class Product { + constructor(name, price, description, rewardPoints, quantity) { + this.name = name; + this.price = price; + this.description = description; + this.inStock = true; + this.rewardPoints = rewardPoints; + this.quantity = quantity; + } + display() { + const dollar = "$"; + return `Name: ${this.name}, Price: ${dollar}${this.price}, Description: ${this.description}`; + } +} + +module.exports = Product; diff --git a/index.js b/index.js index efad601..5c076b6 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){ - -} + }; +} catch (e) {} try { module.exports = { Product, - Cart - } -} catch(e){ - -} + Cart, + }; +} catch (e) {} try { module.exports = { Product, Cart, - Customer - } -} catch(e){ - -} + Customer, + }; +} catch (e) {} try { module.exports = { Product, Cart, Customer, - Auth - } -} catch(e){ - -} \ No newline at end of file + 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",