import java.util.
Scanner;
public class BankSystem {
public static void main(String[] args) {
Bank bank = new Bank();
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("\nBanking System Menu:");
System.out.println("1. Create Account");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Check Balance");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter account ID: ");
String accountId = scanner.nextLine();
bank.createAccount(accountId);
break;
case 2:
System.out.print("Enter account ID: ");
accountId = scanner.nextLine();
System.out.print("Enter amount to deposit: ");
double depositAmount = scanner.nextDouble();
try {
bank.deposit(accountId, depositAmount);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 3:
System.out.print("Enter account ID: ");
accountId = scanner.nextLine();
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = scanner.nextDouble();
try {
bank.withdraw(accountId, withdrawAmount);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 4:
System.out.print("Enter account ID: ");
accountId = scanner.nextLine();
try {
double balance = bank.checkBalance(accountId);
System.out.println("Balance: " + balance);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 5:
exit = true;
break;
default:
System.out.println("Invalid option. Please try again.");
}
}
scanner.close();
}
}
class Bank {
private static final int MAX_ACCOUNTS = 100;
private Account[] accounts;
private int accountCount;
public Bank() {
accounts = new Account[MAX_ACCOUNTS];
accountCount = 0;
}
public void createAccount(String accountId) {
if (findAccount(accountId) != null) {
System.out.println("Account with ID " + accountId + " already
exists.");
} else if (accountCount >= MAX_ACCOUNTS) {
System.out.println("Cannot create more accounts. Maximum limit
reached.");
} else {
accounts[accountCount++] = new Account(accountId);
System.out.println("Account created successfully.");
}
}
public void deposit(String accountId, double amount) throws Exception {
Account account = findAccount(accountId);
if (account == null) {
throw new Exception("Account with ID " + accountId + " does not
exist.");
}
if (amount <= 0) {
throw new Exception("Deposit amount must be positive.");
}
account.deposit(amount);
System.out.println("Deposited " + amount + " to account " + accountId);
}
public void withdraw(String accountId, double amount) throws Exception {
Account account = findAccount(accountId);
if (account == null) {
throw new Exception("Account with ID " + accountId + " does not
exist.");
}
if (amount <= 0) {
throw new Exception("Withdrawal amount must be positive.");
}
if (amount > account.getBalance()) {
throw new Exception("Insufficient balance.");
}
account.withdraw(amount);
System.out.println("Withdrew " + amount + " from account " + accountId);
}
public double checkBalance(String accountId) throws Exception {
Account account = findAccount(accountId);
if (account == null) {
throw new Exception("Account with ID " + accountId + " does not
exist.");
}
return account.getBalance();
}
private Account findAccount(String accountId) {
for (int i = 0; i < accountCount; i++) {
if (accounts[i].getAccountId().equals(accountId)) {
return accounts[i];
}
}
return null;
}
}
class Account {
private String accountId;
private double balance;
public Account(String accountId) {
this.accountId = accountId;
this.balance = 0.0;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public double getBalance() {
return balance;
}
public String getAccountId() {
return accountId;
}
}