diff --git a/Auth.js b/Auth.js new file mode 100644 index 0000000..5ff6bb4 --- /dev/null +++ b/Auth.js @@ -0,0 +1,22 @@ +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) { + const customer = this.customers.find((customer) => customer.email == email); + + if (customer === undefined) return null; + + return customer; + } +} + +module.exports = Auth; \ No newline at end of file diff --git a/Cart.js b/Cart.js new file mode 100644 index 0000000..a9830e6 --- /dev/null +++ b/Cart.js @@ -0,0 +1,43 @@ +class Cart { + constructor(products = [], total = 0) { + this.products = products; + this.total = total; + } + + addProduct(product) { + this.total += product.price; + this.products.push(product) + } + + removeProduct(index) { + if (index >= 0 && index < this.products.length) { + const removed = this.products.splice(index, 1)[0] + this.total -= removed.price + } + } + + getTotal() { + console.log(this.total) + return this.total; + } + + clear(){ + this.products = [] + this.total = 0 + console.log(this.total) + console.log(this.products) + return this.total + } + + removeItemByName(name) { + const index = this.products.findIndex(product => product.name === name); + console.log(`PRODUCTS: ${this.products}, TOTAL: ${this.total}`) + if (index !== -1) { + const removed = this.products.splice(index, 1)[0] + this.total -= removed.price; + console.log(`PRODUCTS: ${this.products}, TOTAL: ${this.total}`) + } + } +} + +module.exports = Cart; \ No newline at end of file diff --git a/Customer.js b/Customer.js new file mode 100644 index 0000000..4601fe1 --- /dev/null +++ b/Customer.js @@ -0,0 +1,21 @@ +class Customer { + constructor(name, email, shippingAddress) { + this.name = name; + this.email = email; + this.shippingAddress = shippingAddress; + this.orderHistory = []; + this.rewardPoints = 0; + } + + addToOrderHistory(cart) { + this.rewardPoints += cart.products.reduce((acc, product) => acc + product.rewardPoints, 0); + this.orderHistory.push(cart); + } + + getRewardPoints() { + console.log(this.rewardPoints); + return this.rewardPoints; + } +} + +module.exports = Customer; \ No newline at end of file diff --git a/Product.js b/Product.js new file mode 100644 index 0000000..86a4988 --- /dev/null +++ b/Product.js @@ -0,0 +1,13 @@ +class Product{ + constructor(name, price, description, rewardPoints, inStock){ + this.name = name; + this.price = price; + this.description = description; + this.rewardPoints = rewardPoints; + this.inStock = true; + } + display(){ + return (`Name: ${this.name}, Price: $${this.price}, Description: ${this.description}`) + } +} +module.exports = Product \ No newline at end of file diff --git a/index.js b/index.js index efad601..fddba99 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,34 @@ // Import Classes Here +const Product = require("./Product") +const Cart = require("./Cart") +const Customer = require("./Customer"); +const Auth = require("./Auth"); + + +const cart1 = new Cart() +const newProd = new Product("Apple", 12, "An apple", 1.2); +const newProd2 = new Product("Mango", 20, "A mango", 2.0); +cart1.addProduct(newProd); +cart1.addProduct(newProd2); +cart1.getTotal() + +const cart2 = new Cart(); +const newProd3 = new Product("Pear", 15, "A pear", 1.5); +const newProd4 = new Product("Orange", 30, "An orange", 3.0); +cart2.addProduct(newProd3); +cart2.addProduct(newProd4); + + +const customer = new Customer("John", "John@gmail", "123 John Ln"); +customer.addToOrderHistory(cart1); +customer.addToOrderHistory(cart2); + +customer.getRewardPoints(); + +cart2.removeItemByName('Pear') + +// cart1.clear() +