Skip to content

Commit

Permalink
Create loyaltyProgram.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent 96a8482 commit 4110d38
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/loyalty/loyaltyProgram.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 4110d38

Please sign in to comment.