From 375f27650391f57a54328aa846607e1c80480425 Mon Sep 17 00:00:00 2001 From: Joshua Benavides Date: Mon, 5 May 2025 11:54:50 -0500 Subject: [PATCH 1/8] first part of farmers --- Product.js | 12 ++++++++++++ index.js | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Product.js diff --git a/Product.js b/Product.js new file mode 100644 index 0000000..59dd1c8 --- /dev/null +++ b/Product.js @@ -0,0 +1,12 @@ +class Product{ + constructor(name, price, description, inStock){ + 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 \ No newline at end of file diff --git a/index.js b/index.js index efad601..624cbc2 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ // Import Classes Here - +const Product = require("./Product") +console.log(Product) +console.log(Product.display) From 618657794e0c6c5b4925277e4a41cccc34d02250 Mon Sep 17 00:00:00 2001 From: LaneLR Date: Mon, 5 May 2025 12:21:35 -0500 Subject: [PATCH 2/8] finished Cart.js --- Cart.js | 21 +++++++++++++++++++++ index.js | 10 ++-------- 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 Cart.js diff --git a/Cart.js b/Cart.js new file mode 100644 index 0000000..caa679e --- /dev/null +++ b/Cart.js @@ -0,0 +1,21 @@ +class Cart { + constructor(products = [], total = 0) { + this.products = products; + this.total = total; + } + + addProduct(product) { + this.total += product.price; + this.products.push(product) + console.log(this.products) + } + + removeProduct(index) { + if (index >= 0 && index < this.products.length) { + const removed = this.products.splice(index, 1)[0] + this.total -= removed.price + } + } +} + +module.exports = Cart; \ No newline at end of file diff --git a/index.js b/index.js index 624cbc2..68c992e 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,9 @@ // Import Classes Here const Product = require("./Product") -console.log(Product) -console.log(Product.display) - - - - - - +const Cart = require("./Cart") +// const cart1 = new Cart() // DO NOT EDIT BELOW THIS LINE try { From 564c6da99c7671d4394fb34c063926b2de7ba3cb Mon Sep 17 00:00:00 2001 From: Emmanuel Luis Date: Mon, 5 May 2025 13:39:37 -0500 Subject: [PATCH 3/8] Finished Customer Class --- Customer.js | 14 ++++++++++++++ index.js | 1 + 2 files changed, 15 insertions(+) create mode 100644 Customer.js diff --git a/Customer.js b/Customer.js new file mode 100644 index 0000000..87e868c --- /dev/null +++ b/Customer.js @@ -0,0 +1,14 @@ +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; \ No newline at end of file diff --git a/index.js b/index.js index 68c992e..cdab07d 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ // Import Classes Here const Product = require("./Product") const Cart = require("./Cart") +const Customer = require("./Customer"); // const cart1 = new Cart() From e760bb02817a6a809a6d8d471e5ac2115091ae96 Mon Sep 17 00:00:00 2001 From: Emmanuel Luis Date: Mon, 5 May 2025 13:48:06 -0500 Subject: [PATCH 4/8] Added Auth Class --- Auth.js | 22 ++++++++++++++++++++++ index.js | 1 + 2 files changed, 23 insertions(+) create mode 100644 Auth.js 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/index.js b/index.js index cdab07d..812ca87 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const Product = require("./Product") const Cart = require("./Cart") const Customer = require("./Customer"); +const Auth = require("./Auth"); // const cart1 = new Cart() From 1f0c4f8031dbbdab6c2d7af69b7b72d59ec78195 Mon Sep 17 00:00:00 2001 From: LaneLR Date: Mon, 5 May 2025 13:58:17 -0500 Subject: [PATCH 5/8] added getTotal() method to Cart --- Cart.js | 6 +++++- index.js | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Cart.js b/Cart.js index caa679e..a298271 100644 --- a/Cart.js +++ b/Cart.js @@ -7,7 +7,6 @@ class Cart { addProduct(product) { this.total += product.price; this.products.push(product) - console.log(this.products) } removeProduct(index) { @@ -16,6 +15,11 @@ class Cart { this.total -= removed.price } } + + getTotal() { + console.log(this.total) + return this.total; + } } module.exports = Cart; \ No newline at end of file diff --git a/index.js b/index.js index 812ca87..9f768d5 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,22 @@ const Customer = require("./Customer"); const Auth = require("./Auth"); -// const cart1 = new Cart() +const cart1 = new Cart() +const newProd = new Product("Apple", 12, "An apple", true) +const newProd2 = new Product("Mango", 20, "A mango", true) +cart1.addProduct(newProd) +cart1.addProduct(newProd2) +cart1.getTotal() + + + + + + + + + + // DO NOT EDIT BELOW THIS LINE try { From 5563e58a735478ad57e7a282d6efd461660507f6 Mon Sep 17 00:00:00 2001 From: Joshua Benavides Date: Mon, 5 May 2025 14:04:22 -0500 Subject: [PATCH 6/8] added the clear method --- Cart.js | 8 ++++++++ index.js | 2 ++ 2 files changed, 10 insertions(+) diff --git a/Cart.js b/Cart.js index a298271..3553f15 100644 --- a/Cart.js +++ b/Cart.js @@ -20,6 +20,14 @@ class Cart { console.log(this.total) return this.total; } + + clear(){ + this.products = [] + this.total = 0 + console.log(this.total) + console.log(this.products) + return this.total + } } module.exports = Cart; \ No newline at end of file diff --git a/index.js b/index.js index 9f768d5..5b990ad 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,8 @@ cart1.addProduct(newProd) cart1.addProduct(newProd2) cart1.getTotal() +cart1.clear() + From 3c610ee9532719a6c4f0e05bcfb2c90477ffd57d Mon Sep 17 00:00:00 2001 From: Emmanuel Luis Date: Mon, 5 May 2025 14:20:18 -0500 Subject: [PATCH 7/8] Added rewards points --- Customer.js | 7 +++++++ Product.js | 3 ++- index.js | 23 +++++++++++++++++++---- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Customer.js b/Customer.js index 87e868c..4601fe1 100644 --- a/Customer.js +++ b/Customer.js @@ -4,11 +4,18 @@ class Customer { 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 index 59dd1c8..86a4988 100644 --- a/Product.js +++ b/Product.js @@ -1,8 +1,9 @@ class Product{ - constructor(name, price, description, inStock){ + constructor(name, price, description, rewardPoints, inStock){ this.name = name; this.price = price; this.description = description; + this.rewardPoints = rewardPoints; this.inStock = true; } display(){ diff --git a/index.js b/index.js index 5b990ad..734cbc1 100644 --- a/index.js +++ b/index.js @@ -6,12 +6,27 @@ const Auth = require("./Auth"); const cart1 = new Cart() -const newProd = new Product("Apple", 12, "An apple", true) -const newProd2 = new Product("Mango", 20, "A mango", true) -cart1.addProduct(newProd) -cart1.addProduct(newProd2) +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(); + + + cart1.clear() From ae904cb3ce79f7fd1efe813d56f7f37d1f8c39c9 Mon Sep 17 00:00:00 2001 From: LaneLR Date: Mon, 5 May 2025 14:28:47 -0500 Subject: [PATCH 8/8] added removeItemByName method to Cart --- Cart.js | 10 ++++++++++ index.js | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Cart.js b/Cart.js index 3553f15..a9830e6 100644 --- a/Cart.js +++ b/Cart.js @@ -28,6 +28,16 @@ class Cart { 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/index.js b/index.js index 734cbc1..fddba99 100644 --- a/index.js +++ b/index.js @@ -25,9 +25,9 @@ customer.addToOrderHistory(cart2); customer.getRewardPoints(); +cart2.removeItemByName('Pear') - -cart1.clear() +// cart1.clear()