Java Program: Banking System using Interface
Source Code with Comments:
// Interface
interface BankAccount {
void deposit(double amount);
void withdraw(double amount);
void displayBalance();
}
// Savings Account class
class SavingsAccount implements BankAccount {
private double balance = 0;
public void deposit(double amount) {
balance += amount;
[Link]("Deposited Rs." + amount + " to Savings Account.");
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
[Link]("Withdrew Rs." + amount + " from Savings Account.");
} else {
[Link]("Insufficient balance in Savings Account!");
}
}
public void displayBalance() {
[Link]("Savings Account Balance: Rs." + balance);
}
}
// Current Account class
class CurrentAccount implements BankAccount {
private double balance = 0;
public void deposit(double amount) {
balance += amount;
[Link]("Deposited Rs." + amount + " to Current Account.");
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
[Link]("Withdrew Rs." + amount + " from Current Account.");
} else {
[Link]("Insufficient balance in Current Account!");
}
}
Java Program: Banking System using Interface
public void displayBalance() {
[Link]("Current Account Balance: Rs." + balance);
}
}
// Main class with menu
public class BankSystem {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
BankAccount account;
[Link]("Choose Account Type:");
[Link]("1. Savings Account");
[Link]("2. Current Account");
int choice = [Link]();
if (choice == 1) {
account = new SavingsAccount();
} else if (choice == 2) {
account = new CurrentAccount();
} else {
[Link]("Invalid choice!");
return;
}
int option;
do {
[Link]("\nMenu:");
[Link]("1. Deposit");
[Link]("2. Withdraw");
[Link]("3. Display Balance");
[Link]("4. Exit");
[Link]("Enter option: ");
option = [Link]();
switch (option) {
case 1:
[Link]("Enter amount to deposit: Rs.");
double dep = [Link]();
[Link](dep);
break;
case 2:
[Link]("Enter amount to withdraw: Rs.");
double wit = [Link]();
[Link](wit);
break;
case 3:
[Link]();
break;
case 4:
Java Program: Banking System using Interface
[Link]("Thank you for using the banking system.");
break;
default:
[Link]("Invalid option.");
}
} while (option != 4);
}
}
Sample Output:
Choose Account Type:
1. Savings Account
2. Current Account
1
Menu:
1. Deposit
2. Withdraw
3. Display Balance
4. Exit
Enter option: 1
Enter amount to deposit: Rs.1000
Deposited Rs.1000.0 to Savings Account.
Menu:
1. Deposit
2. Withdraw
3. Display Balance
4. Exit
Enter option: 2
Enter amount to withdraw: Rs.500
Withdrew Rs.500.0 from Savings Account.
Menu:
1. Deposit
2. Withdraw
3. Display Balance
4. Exit
Enter option: 3
Savings Account Balance: Rs.500.0