Project 2
import java.util.*;
abstract class RBI {
protected final double minSavingsBalance = 2000;
protected final double minCurrentBalance = 5000;
public abstract double getSavingsInterestRate();
public abstract double getCurrentInterestRate();
public double calculateSimpleInterest(double principal, double rate, int years) {
return principal * (1 + (rate / 100) * years);
}
public double calculateCompoundInterest(double principal, double rate, int years, int n) {
return principal * Math.pow(1 + (rate / (100 * n)), n * years);
}
}
class SBI extends RBI {
public double getSavingsInterestRate() {
return 5.0;
}
public double getCurrentInterestRate() {
return 8.0;
}
}
class ICICI extends RBI {
public double getSavingsInterestRate() {
return 5.5;
}
public double getCurrentInterestRate() {
return 8.5;
}
}
class PNB extends RBI {
public double getSavingsInterestRate() {
return 4.5;
}
public double getCurrentInterestRate() {
return 7.5;
}
}
class Customer {
private String name;
private String dob;
private int accountNumber;
public Customer(String name, String dob, int accountNumber) {
this.name = name;
this.dob = dob;
this.accountNumber = accountNumber;
}
public String getName() {
return name;
}
public int getAccountNumber() {
return accountNumber;
}
public void printDetails() {
System.out.println("Name: " + name);
System.out.println("Date of Birth: " + dob);
System.out.println("Account Number: " + accountNumber);
}
}
class Account {
private String accountType;
private double balance;
private Customer customer;
public Account(String accountType, double balance, Customer customer) {
this.accountType = accountType;
this.balance = balance;
this.customer = customer;
}
public String getAccountType() {
return accountType;
}
public double getBalance() {
return balance;
}
public Customer getCustomer() {
return customer;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount + ". New Balance: " + balance);
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount + ". New Balance: " + balance);
} else {
System.out.println("Insufficient balance!");
}
}
}
public class BankingSystem {
private static int accountCounter = 1000;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Banking System");
Map<Integer, Account> accounts = new HashMap<>();
RBI selectedBank = null;
System.out.println("Choose your Bank: 1. SBI 2. ICICI 3. PNB");
int bankChoice = scanner.nextInt();
switch (bankChoice) {
case 1:
selectedBank = new SBI();
break;
case 2:
selectedBank = new ICICI();
break;
case 3:
selectedBank = new PNB();
break;
default:
System.out.println("Invalid choice!");
System.exit(0);
}
System.out.println("Do you want to open an account? (yes/no)");
if (scanner.next().equalsIgnoreCase("yes")) {
System.out.println("Enter Name, DOB:");
String name = scanner.next();
String dob = scanner.next();
System.out.println("Enter Account Type (savings/current):");
String accountType = scanner.next();
System.out.println("Enter Initial Deposit:");
double initialDeposit = scanner.nextDouble();
if ((accountType.equalsIgnoreCase("savings") && initialDeposit < selectedBank.minSavingsBalance) ||
(accountType.equalsIgnoreCase("current") && initialDeposit < selectedBank.minCurrentBalance)) {
System.out.println("Initial deposit is less than the minimum required balance.");
System.exit(0);
}
Customer customer = new Customer(name, dob, ++accountCounter);
Account account = new Account(accountType, initialDeposit, customer);
accounts.put(customer.getAccountNumber(), account);
System.out.println("Account created successfully! Account Number: " + customer.getAccountNumber());
}
System.out.println("Do you want to calculate interest? (yes/no)");
if (scanner.next().equalsIgnoreCase("yes")) {
System.out.println("Enter Account Number:");
int accountNumber = scanner.nextInt();
if (accounts.containsKey(accountNumber)) {
Account account = accounts.get(accountNumber);
System.out.println("Enter number of years:");
int years = scanner.nextInt();
double rate = account.getAccountType().equalsIgnoreCase("savings")
? selectedBank.getSavingsInterestRate()
: selectedBank.getCurrentInterestRate();
double finalAmount;
if (account.getAccountType().equalsIgnoreCase("savings")) {
finalAmount = selectedBank.calculateSimpleInterest(account.getBalance(), rate, years);
} else {
finalAmount = selectedBank.calculateCompoundInterest(account.getBalance(), rate, years, 2);
}
System.out.println("Final Amount after " + years + " years: " + finalAmount);
} else {
System.out.println("Account not found!");
}
}
scanner.close();
}
}
Explanation of OOP Concepts:
Abstraction:
The RBI class provides abstract methods for interest rates, hiding implementation details from derived classes.
Inheritance:
SBI, ICICI, and PNB inherit from RBI, reusing and extending its functionality.
Encapsulation:
Classes like Customer and Account encapsulate data (e.g., account balance) and provide controlled access through methods.
Polymorphism:
Runtime polymorphism is used to call methods like getSavingsInterestRate() on the RBI reference, which invokes the specific bank’s implementation.