Project 2
Answers:
package admin;
import java.util.Scanner;
abstract class RBI {
abstract double getInterestRate();
abstract double getWithdrawalLimit();
}
class Account {
protected int accountNumber;
protected double balance;
public Account(int accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0.0;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Amount deposited: " + amount);
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Amount withdrawn: " + amount);
} else {
System.out.println("Insufficient balance!");
}
}
public double getBalance() {
return balance;
}
}
class SavingsAccount extends Account {
public SavingsAccount(int accountNumber) {
super(accountNumber);
}
@Override
public void deposit(double amount) {
super.deposit(amount);
System.out.println("Interest earned: " + (amount * getInterestRate()));
}
public double getInterestRate() {
return 0.05; // 5% annually
}
}
class CurrentAccount extends Account {
public CurrentAccount(int accountNumber) {
super(accountNumber);
}
@Override
public void deposit(double amount) {
super.deposit(amount);
System.out.println("Interest earned: " + (amount * getInterestRate()));
}
public double getInterestRate() {
return 0.08; // 8% annually
}
}
class Customer {
private String name;
private String accountType;
private String bank;
private Account account;
public Customer(String name, String accountType, String bank) {
this.name = name;
this.accountType = accountType;
this.bank = bank;
this.account = createAccount();
}
private Account createAccount() {
int accountNumber = (int) (Math.random() * 10000) + 1;
if (accountType.equalsIgnoreCase("saving")) {
return new SavingsAccount(accountNumber);
} else if (accountType.equalsIgnoreCase("current")) {
return new CurrentAccount(accountNumber);
}
return null;
}
public void deposit(double amount) {
account.deposit(amount);
}
public void withdraw(double amount) {
account.withdraw(amount);
}
public double getBalance() {
return account.getBalance();
}
public double getInterestRate() {
// TODO Auto-generated method stub
return 0;
}
}
class SBIBank extends RBI {
@Override
double getInterestRate() {
return 0.04; // 4% annually
}
@Override
double getWithdrawalLimit() {
return 100000.0;
}
}
class ICICIBank extends RBI {
@Override
double getInterestRate() {
return 0.04; // 4% annually
}
@Override
double getWithdrawalLimit() {
return 150000.0;
}
}
public class BankManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Want to open an account? (yes/no)");
String openAccount = scanner.nextLine();
if (openAccount.equalsIgnoreCase("yes")) {
System.out.print("Insert name: ");
String name = scanner.nextLine();
System.out.print("Type of account (saving/current): ");
String accountType = scanner.nextLine();
System.out.print("In which bank you want (SBI/ICICI): ");
String bankName = scanner.nextLine();
Customer customer;
if (bankName.equalsIgnoreCase("SBI")) {
customer = new Customer(name, accountType, bankName);
SBIBank sbiBank = new SBIBank();
System.out.println("Account has been opened and account no is: " + customer.getBalance());
System.out.println("Interest rate: " + sbiBank.getInterestRate());
System.out.println("Withdrawal limit: " + sbiBank.getWithdrawalLimit());
} else if (bankName.equalsIgnoreCase("ICICI")) {
customer = new Customer(name, accountType, bankName);
ICICIBank iciciBank = new ICICIBank();
System.out.println("Account has been opened and account no is: " + customer.getBalance());
System.out.println("Interest rate: " + iciciBank.getInterestRate());
System.out.println("Withdrawal limit: " + iciciBank.getWithdrawalLimit());
} else {
System.out.println("Invalid bank name!");
return;
}
System.out.println("Want to print mini statement? (yes/no)");
String printStatement = scanner.nextLine();
if (printStatement.equalsIgnoreCase("yes")) {
System.out.println("Saving mini statement in .txt file...");
// Write mini statement to a file
}
System.out.println("Calculate interest? (yes/no)");
String calculateInterest = scanner.nextLine();
if (calculateInterest.equalsIgnoreCase("yes")) {
System.out.print("Enter number of years: ");
int years = scanner.nextInt();
scanner.nextLine(); // Consume newline character
double interest = customer.getBalance() * customer.getInterestRate() * years;
double totalAmount = customer.getBalance() + interest;
System.out.println("Total interest: " + interest);
System.out.println("Total amount: " + totalAmount);
}
} else {
System.out.println("Thank you for using our banking management system!");
}
}
}