In this blog post, we will dive into the creation of a simple banking system in Java. This system will allow you to perform basic operations like creating, reading, updating, and deleting account holders. Furthermore, account holders will have the capacity to check their balance, view their transaction history, and make deposits and withdrawals.
1. Account Class
The core entity of our banking system is the Account class:
public class Account {
private long accountNumber;
private String holderName;
private double balance;
private List<String> transactionHistory = new ArrayList<>();
// Constructor, getters, setters, and other necessary methods
}
2. Banking System Implementation
public class BankingSystem {
private List<Account> accounts = new ArrayList<>();
public void createAccount(Account account) {
accounts.add(account);
account.getTransactionHistory().add("Account created.");
}
public Account getAccount(long accountNumber) {
for (Account account : accounts) {
if (account.getAccountNumber() == accountNumber) {
return account;
}
}
return null;
}
public void updateAccount(Account account, String newHolderName) {
account.setHolderName(newHolderName);
account.getTransactionHistory().add("Account holder name updated to " + newHolderName + ".");
}
public void deleteAccount(long accountNumber) {
Account account = getAccount(accountNumber);
if (account != null) {
accounts.remove(account);
}
}
public void deposit(Account account, double amount) {
account.setBalance(account.getBalance() + amount);
account.getTransactionHistory().add("Deposited $" + amount + ". New Balance: $" + account.getBalance());
}
public void withdraw(Account account, double amount) {
if (account.getBalance() >= amount) {
account.setBalance(account.getBalance() - amount);
account.getTransactionHistory().add("Withdrew $" + amount + ". New Balance: $" + account.getBalance());
} else {
System.out.println("Insufficient funds.");
}
}
public void printTransactionHistory(Account account) {
System.out.println("Transaction History for Account " + account.getAccountNumber() + ":");
for (String transaction : account.getTransactionHistory()) {
System.out.println(transaction);
}
}
}
3. Main Execution
Let's put our system into action:
public class Main {
public static void main(String[] args) {
BankingSystem bank = new BankingSystem();
// Create a new account
Account john = new Account(12345, "John Doe", 1000.00);
bank.createAccount(john);
// Deposit and withdrawal operations
bank.deposit(john, 500.00);
bank.withdraw(john, 200.00);
// Update account holder's name
bank.updateAccount(john, "Johnathan Doe");
// Print transaction history
bank.printTransactionHistory(john);
}
}
The above class demonstrates the working of the banking system, from creating a new account to performing transactions and viewing transaction history.Output:
Deposited $500.0. New Balance: $1500.0
Withdrew $200.0. New Balance: $1300.0
Transaction History for Account 12345:
Account created.
Deposited $500.0. New Balance: $1500.0
Withdrew $200.0. New Balance: $1300.0
Account holder name updated to Johnathan Doe.
With the base established, the banking system can be further enhanced by integrating with databases, adding more comprehensive error handling, and introducing features like funds transfer between accounts, interest calculations, or even loan processing. This simple system is a starting point to illustrate basic banking functionalities.
Comments
Post a Comment
Leave Comment