Created
March 5, 2025 12:17
-
-
Save pkavoo/b6f421c05ba0f71c65faeea1f6a9cc35 to your computer and use it in GitHub Desktop.
BankingSystem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Scanner; | |
import java.util.InputMismatchException; | |
// Account class representing a bank account | |
class Account { | |
private int accountNumber; | |
private String accountHolder; | |
private double balance; | |
public Account(int accountNumber, String accountHolder, double initialBalance) { | |
this.accountNumber = accountNumber; | |
this.accountHolder = accountHolder; | |
this.balance = initialBalance; | |
} | |
public int getAccountNumber() { | |
return accountNumber; | |
} | |
public String getAccountHolder() { | |
return accountHolder; | |
} | |
public double getBalance() { | |
return balance; | |
} | |
// Deposit funds into the account | |
public void deposit(double amount) { | |
if (amount > 0) { | |
balance += amount; | |
System.out.println("Deposit successful. New balance: $" + balance); | |
} else { | |
System.out.println("Invalid deposit amount."); | |
} | |
} | |
// Withdraw funds from the account | |
public boolean withdraw(double amount) { | |
if (amount > 0 && amount <= balance) { | |
balance -= amount; | |
System.out.println("Withdrawal successful. New balance: $" + balance); | |
return true; | |
} else { | |
System.out.println("Invalid withdrawal amount or insufficient funds."); | |
return false; | |
} | |
} | |
} | |
// Bank class representing the bank's core functionality | |
class Bank { | |
private String bankName; | |
private Map<Integer, Account> accounts; | |
public Bank(String bankName) { | |
this.bankName = bankName; | |
accounts = new HashMap<>(); | |
} | |
// Create a new account | |
public void createAccount(int accountNumber, String accountHolder, double initialBalance) { | |
if (accounts.containsKey(accountNumber)) { | |
System.out.println("Account already exists with this number."); | |
} else { | |
Account newAccount = new Account(accountNumber, accountHolder, initialBalance); | |
accounts.put(accountNumber, newAccount); | |
System.out.println("Account created successfully for " + accountHolder); | |
} | |
} | |
// Get an account by account number | |
public Account getAccount(int accountNumber) { | |
return accounts.get(accountNumber); | |
} | |
// Transfer funds between two accounts with added validation | |
public void transferFunds(int fromAccountNumber, int toAccountNumber, double amount) { | |
if (fromAccountNumber == toAccountNumber) { | |
System.out.println("Transfer failed: Cannot transfer funds to the same account."); | |
return; | |
} | |
Account fromAccount = accounts.get(fromAccountNumber); | |
Account toAccount = accounts.get(toAccountNumber); | |
if (fromAccount == null || toAccount == null) { | |
System.out.println("Invalid account number(s) provided for transfer."); | |
return; | |
} | |
if (fromAccount.withdraw(amount)) { | |
toAccount.deposit(amount); | |
System.out.println("Transfer of $" + amount + " from account " | |
+ fromAccountNumber + " to account " + toAccountNumber + " successful."); | |
} else { | |
System.out.println("Transfer failed due to insufficient funds."); | |
} | |
} | |
} | |
// Main class to run the banking system application | |
public class BankingSystem { | |
public static void main(String[] args) { | |
Bank pgb = new Bank("Prudent Global Bank"); | |
Scanner scanner = new Scanner(System.in); | |
boolean running = true; | |
// Sample accounts for demonstration | |
pgb.createAccount(1001, "Alice Johnson", 5000); | |
pgb.createAccount(1002, "Bob Smith", 3000); | |
while (running) { | |
System.out.println("\n--- Prudent Global Bank Menu ---"); | |
System.out.println("1. Create Account"); | |
System.out.println("2. Deposit"); | |
System.out.println("3. Withdraw"); | |
System.out.println("4. Transfer Funds"); | |
System.out.println("5. Check Balance"); | |
System.out.println("6. Exit"); | |
System.out.print("Enter your choice: "); | |
int choice = 0; | |
try { | |
choice = scanner.nextInt(); | |
} catch (InputMismatchException e) { | |
System.out.println("Invalid input. Please enter a number for the choice."); | |
scanner.next(); // clear invalid input | |
continue; | |
} | |
switch (choice) { | |
case 1: | |
try { | |
System.out.print("Enter account number: "); | |
int newAccNum = scanner.nextInt(); | |
scanner.nextLine(); // consume newline | |
System.out.print("Enter account holder name: "); | |
String name = scanner.nextLine(); | |
System.out.print("Enter initial deposit: "); | |
double initialDeposit = scanner.nextDouble(); | |
pgb.createAccount(newAccNum, name, initialDeposit); | |
} catch (InputMismatchException e) { | |
System.out.println("Invalid input. Please enter valid numeric values for account number and initial deposit."); | |
scanner.nextLine(); // clear invalid input | |
} | |
break; | |
case 2: | |
try { | |
System.out.print("Enter account number for deposit: "); | |
int depAccNum = scanner.nextInt(); | |
System.out.print("Enter deposit amount: "); | |
double depositAmount = scanner.nextDouble(); | |
Account depAccount = pgb.getAccount(depAccNum); | |
if (depAccount != null) { | |
depAccount.deposit(depositAmount); | |
} else { | |
System.out.println("Account not found."); | |
} | |
} catch (InputMismatchException e) { | |
System.out.println("Invalid input. Please enter valid numeric values."); | |
scanner.nextLine(); // clear invalid input | |
} | |
break; | |
case 3: | |
try { | |
System.out.print("Enter account number for withdrawal: "); | |
int witAccNum = scanner.nextInt(); | |
System.out.print("Enter withdrawal amount: "); | |
double withdrawalAmount = scanner.nextDouble(); | |
Account witAccount = pgb.getAccount(witAccNum); | |
if (witAccount != null) { | |
witAccount.withdraw(withdrawalAmount); | |
} else { | |
System.out.println("Account not found."); | |
} | |
} catch (InputMismatchException e) { | |
System.out.println("Invalid input. Please enter valid numeric values."); | |
scanner.nextLine(); // clear invalid input | |
} | |
break; | |
case 4: | |
try { | |
System.out.print("Enter source account number: "); | |
int srcAcc = scanner.nextInt(); | |
System.out.print("Enter destination account number: "); | |
int destAcc = scanner.nextInt(); | |
System.out.print("Enter transfer amount: "); | |
double transferAmount = scanner.nextDouble(); | |
pgb.transferFunds(srcAcc, destAcc, transferAmount); | |
} catch (InputMismatchException e) { | |
System.out.println("Invalid input. Please enter valid numeric values."); | |
scanner.nextLine(); // clear invalid input | |
} | |
break; | |
case 5: | |
try { | |
System.out.print("Enter account number to check balance: "); | |
int balAcc = scanner.nextInt(); | |
Account balAccount = pgb.getAccount(balAcc); | |
if (balAccount != null) { | |
System.out.println("Account " + balAcc + " balance: $" + balAccount.getBalance()); | |
} else { | |
System.out.println("Account not found."); | |
} | |
} catch (InputMismatchException e) { | |
System.out.println("Invalid input. Please enter a valid account number."); | |
scanner.nextLine(); // clear invalid input | |
} | |
break; | |
case 6: | |
running = false; | |
System.out.println("Exiting the system. Thank you."); | |
break; | |
default: | |
System.out.println("Invalid choice. Please select again."); | |
} | |
} | |
scanner.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment