Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Auth.js
Original file line number Diff line number Diff line change
@@ -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;
43 changes: 43 additions & 0 deletions Cart.js
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 21 additions & 0 deletions Customer.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions Product.js
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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()




Expand Down