🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.
Related Java Projects
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment