From 4110d38068a3e0651b65eaadbb0d623d431c2df0 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Tue, 3 Dec 2024 15:16:55 +0700 Subject: [PATCH] Create loyaltyProgram.js --- src/loyalty/loyaltyProgram.js | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/loyalty/loyaltyProgram.js diff --git a/src/loyalty/loyaltyProgram.js b/src/loyalty/loyaltyProgram.js new file mode 100644 index 000000000..2ede877c8 --- /dev/null +++ b/src/loyalty/loyaltyProgram.js @@ -0,0 +1,50 @@ +// loyalty/loyaltyProgram.js + +class LoyaltyProgram { + constructor(name, description, pointsPerPurchase) { + this.name = name; + this.description = description; + this.pointsPerPurchase = pointsPerPurchase; + this.members = new Map(); // Store userId and their points + } + + addMember(userId) { + if (!this.members.has(userId)) { + this.members.set(userId, 0); // Initialize points to 0 + } + } + + removeMember(userId) { + this.members.delete(userId); + } + + earnPoints(userId, purchaseAmount) { + if (!this.members.has(userId)) { + throw new Error('User is not a member of this loyalty program.'); + } + const pointsEarned = Math.floor(purchaseAmount * this.pointsPerPurchase); + this.members.set(userId, this.members.get(userId) + pointsEarned); + return pointsEarned; + } + + getPoints(userId) { + if (!this.members.has(userId)) { + throw new Error('User is not a member of this loyalty program.'); + } + return this.members.get(userId); + } + + redeemPoints(userId, points) { + if (!this.members.has(userId)) { + throw new Error('User is not a member of this loyalty program.'); + } + const currentPoints = this.members.get(userId); + if (currentPoints < points) { + throw new Error('Insufficient points to redeem.'); + } + this.members.set(userId, currentPoints - points); + return true; // Redemption successful + } +} + +module.exports = LoyaltyProgram;