From 359721642992204e0b672acaef83e3bc3526ef1b Mon Sep 17 00:00:00 2001 From: Sameer Date: Mon, 23 Oct 2023 12:40:52 +0100 Subject: [PATCH 1/9] init commit --- classes/Cart.js | 16 ++++++++++++++++ index.js | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/classes/Cart.js b/classes/Cart.js index e69de29..1645bf5 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -0,0 +1,16 @@ +class Cart{ + constructor(Products,total){ + this.Products = [] + this.total = 0 + } + addProduct(Pruduct){ + this.Products.push(Pruduct) + this.total +=1 + } + removeProduct(Product){ + this.Products.shift() + this.total -=1 + } +} + +module.exports = Cart \ No newline at end of file diff --git a/index.js b/index.js index efad601..41066f1 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ // Import Classes Here - +const Cart = require("./classes/Cart") From 698dd9039e0fb0f59d2ef14b7e1080dfd1e48273 Mon Sep 17 00:00:00 2001 From: ljenchik Date: Mon, 23 Oct 2023 12:44:31 +0100 Subject: [PATCH 2/9] Product class created --- classes/Product.js | 14 ++++++++++++++ index.js | 40 ++++++++++++---------------------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/classes/Product.js b/classes/Product.js index e69de29..4616810 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() { + 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..eb0ccb2 100644 --- a/index.js +++ b/index.js @@ -1,49 +1,33 @@ // Import Classes Here - - - - - - - - - +const Product = require("./classes/Product"); // 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) {} From af7a0bdca94205679f6224cb2c36e3fe664b6d04 Mon Sep 17 00:00:00 2001 From: Sameer Date: Mon, 23 Oct 2023 12:56:01 +0100 Subject: [PATCH 3/9] minor changes --- classes/Cart.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/classes/Cart.js b/classes/Cart.js index 1645bf5..0c81bcb 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -1,16 +1,22 @@ -class Cart{ - constructor(Products,total){ - this.Products = [] - this.total = 0 +class Cart { + constructor() { + this.products = []; + this.total = 0; } - addProduct(Pruduct){ - this.Products.push(Pruduct) - this.total +=1 + + addProduct(product) { + this.products.push(product); + this.total += product.price; } - removeProduct(Product){ - this.Products.shift() - this.total -=1 + + removeProduct(product) { + const index = this.products.indexOf(product); + if (index !== -1) { + this.products.splice(index, 1); + this.total -= product.price; + } } + } } module.exports = Cart \ No newline at end of file From a0803e631eca78b5d7665b306a5c6ae27b3bf183 Mon Sep 17 00:00:00 2001 From: Sameer Date: Mon, 23 Oct 2023 12:59:14 +0100 Subject: [PATCH 4/9] cart changes --- classes/Cart.js | 1 - 1 file changed, 1 deletion(-) diff --git a/classes/Cart.js b/classes/Cart.js index 0c81bcb..905bdba 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -16,7 +16,6 @@ class Cart { this.total -= product.price; } } - } } module.exports = Cart \ No newline at end of file From db6cb376872bf02b958bc207d9fecf6923d9d8d9 Mon Sep 17 00:00:00 2001 From: ljenchik Date: Mon, 23 Oct 2023 14:48:58 +0100 Subject: [PATCH 5/9] Tests passed --- classes/Auth.js | 21 +++++++++++++++++++++ classes/Cart.js | 22 ++++++++++------------ classes/Customer.js | 13 +++++++++++++ classes/Product.js | 2 +- index.js | 2 ++ package-lock.json | 3 ++- 6 files changed, 49 insertions(+), 14 deletions(-) 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 905bdba..1b6e368 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -1,21 +1,19 @@ class Cart { constructor() { - this.products = []; - this.total = 0; + this.products = []; + this.total = 0; } - + addProduct(product) { - this.products.push(product); - this.total += product.price; + this.products.push(product); + this.total += product.price; } - - removeProduct(product) { - const index = this.products.indexOf(product); - if (index !== -1) { - this.products.splice(index, 1); + + removeProduct(i) { + const product = this.products[i]; + this.products.splice(i, 1); this.total -= product.price; - } } } -module.exports = Cart \ No newline at end of file +module.exports = Cart; diff --git a/classes/Customer.js b/classes/Customer.js index e69de29..79016bf 100644 --- a/classes/Customer.js +++ b/classes/Customer.js @@ -0,0 +1,13 @@ +class Customer { + constructor(name, email, shippingAddress) { + this.name = name; + this.email = email; + this.shippingAddress = shippingAddress; + this.orderHistory = []; + } + addToOrderHistory(cart) { + this.orderHistory.push(cart); + } +} + +module.exports = Customer; diff --git a/classes/Product.js b/classes/Product.js index 4616810..e21bebd 100644 --- a/classes/Product.js +++ b/classes/Product.js @@ -7,7 +7,7 @@ class Product { } display() { const dollar = "$"; - return `Name: ${this.name}, Price: ${dollar} ${this.price}, Description: ${this.description}`; + return `Name: ${this.name}, Price: ${dollar}${this.price}, Description: ${this.description}`; } } diff --git a/index.js b/index.js index 05b15bb..5c076b6 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,8 @@ // 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 { 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 5d0beee15f767789a34d54234b4d9c4105aecc31 Mon Sep 17 00:00:00 2001 From: Sameer Date: Mon, 23 Oct 2023 15:07:24 +0100 Subject: [PATCH 6/9] cart extension --- classes/Cart.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/classes/Cart.js b/classes/Cart.js index 1b6e368..f4c6bbc 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -14,6 +14,17 @@ class Cart { this.products.splice(i, 1); this.total -= product.price; } + getTotal(){ + let sum = 0 + for(let i =0;i Date: Mon, 23 Oct 2023 15:21:17 +0100 Subject: [PATCH 7/9] Added reward points --- classes/Customer.js | 6 ++++++ classes/Product.js | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/classes/Customer.js b/classes/Customer.js index 79016bf..d34447c 100644 --- a/classes/Customer.js +++ b/classes/Customer.js @@ -4,10 +4,16 @@ class Customer { 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 e21bebd..7d0512a 100644 --- a/classes/Product.js +++ b/classes/Product.js @@ -1,9 +1,10 @@ class Product { - constructor(name, price, description) { + constructor(name, price, description, rewardPoints) { this.name = name; this.price = price; this.description = description; this.inStock = true; + this.rewardPoints = rewardPoints; } display() { const dollar = "$"; From a430e24fdab0442dd2254fa167993709c96468d9 Mon Sep 17 00:00:00 2001 From: Sameer Date: Mon, 23 Oct 2023 15:31:57 +0100 Subject: [PATCH 8/9] remove Items --- classes/Cart.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/classes/Cart.js b/classes/Cart.js index f4c6bbc..97dc053 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -25,6 +25,9 @@ class Cart { this.products = [] this.total = 0 } + removeItemByName(product){ + return this.products.filter((item)=>item.name !== product.name) + } } module.exports = Cart; From 0152c46d378dabaed39072b91638256d2ac60c5c Mon Sep 17 00:00:00 2001 From: ljenchik Date: Mon, 23 Oct 2023 15:53:31 +0100 Subject: [PATCH 9/9] Added quantity property --- classes/Cart.js | 43 +++++++++++++++++++++++++++---------------- classes/Product.js | 3 ++- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/classes/Cart.js b/classes/Cart.js index 97dc053..2ad45cf 100644 --- a/classes/Cart.js +++ b/classes/Cart.js @@ -4,29 +4,40 @@ class Cart { this.total = 0; } - addProduct(product) { - this.products.push(product); - this.total += product.price; + addProduct(product, quantity) { + if (quantity > 0) { + this.products.push(product); + this.total += product.price * quantity; + } } - removeProduct(i) { + removeProduct(i, quantity) { const product = this.products[i]; - this.products.splice(i, 1); - this.total -= product.price; + 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;iitem.name !== product.name) + removeItemByName(product) { + return this.products.filter((item) => item.name !== product.name); } } diff --git a/classes/Product.js b/classes/Product.js index 7d0512a..8640c18 100644 --- a/classes/Product.js +++ b/classes/Product.js @@ -1,10 +1,11 @@ class Product { - constructor(name, price, description, rewardPoints) { + 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 = "$";