Page 146 - CTS - CSA TP - Volume 2
P. 146
COMPUTER SOFTWARE APPLICATION - CITS
balance -= amount;
}
void displayBalance() {
System.out.println(“Balance: “ + balance);
}
}
class SavingsAccount extends BankAccount {
// Overriding the withdraw method of the superclass
@Override
void withdraw(double amount) {
if (balance - amount >= 1000) {
balance -= amount;
} else {
System.out.println(“Insufficient balance”);
}
}
}
class CurrentAccount extends BankAccount {
// Overriding the withdraw method of the superclass
@Override
void withdraw(double amount) {
if (balance - amount >= 0) {
balance -= amount;
} else {
System.out.println(“Insufficient balance”);
}
}
}
public class BankAccountDemo {
public static void main(String[] args) {
BankAccount account1 = new SavingsAccount();
account1.deposit(5000);
account1.withdraw(3000);
account1.displayBalance(); // Output: Balance: 2000
BankAccount account2 = new CurrentAccount();
account2.deposit(3000);
account2.withdraw(5000);
account2.displayBalance(); // Output: Insufficient balance
131
CITS : IT & ITES - Computer Software Application - Exercise 99 CITS : IT & ITES - Computer Software Application - Exercise 99