creditCard.js / Polymorphism / Method Overriding is introduced
1.
// This is the Parent class , the children classes will inherit it's attributes and methods
class CCAccount {
constructor(n, e, ph, UA) {
this.name = n;
this.email = e;
this.phoneNumber = ph;
this.usedAmount = UA;
}
getPendingBill() { // this method will return all the used up amount
return this.usedAmount
}
isTransactionAllowed(amount) { // this method will check if the customer is
exceeding the credit limit
if (this.usedAmount + amount > 200000) {
console.log("Limit Exceede");
}
else {
this.usedAmount += amount;
console.log("Transaction Success");
}
}
}
// GoldCCAccount class will have the same properties and methods as CCAccount
but the limit is 5 lakhs
class GoldCCAccount extends CCAccount {
constructor(n, e, ph, UA) {
super(n, e, ph, UA)
}
isTransactionAllowed(amount) {
if (this.usedAmount + amount > 500000) {
console.log("Limit Exceeded")
}
else {
this.usedAmount += amount;
console.log("Transaction Success")
}
}
}
// PlatinumCCAccount class will have same properties and methods as CCAccount
but the limit is 10 lakhs
class PlatinumCCAccount extends CCAccount {
constructor(n, e, ph, UA) {
super(n, e, ph, UA)
}
isTransactionAllowed(amount) {
if (this.usedAmount + amount > 1000000) {
console.log("Limit Exceeded");
}
else {
this.usedAmount += amount
console.log(" Transaction Success")
}
}
}
// Objects creation and calling the class and methods will take place here
let goldUser1 = new GoldCCAccount("Nobin", "abc@gmail.com", 574397332, 30000)
goldUser1.isTransactionAllowed(300000);
console.log(goldUser1.getPendingBill()); // 30,000 + 3,00,000
///////////////////////////////////////////////////////////////////////////////////////////////
2. Polymorphism , means the same method will behave differently, based on which
object/class makes call to it.
3. One way of achieving polymorphism is by method overriding, it is a way to implement a same function differently in the subclass and it is an instance of polymorphism.

Comments
Post a Comment