This example will include basic functionalities such as creating an account, depositing funds, withdrawing funds, and checking the balance. Keep in mind that this is a basic tutorial and doesn’t cover all aspects of a real-world banking application (e.g., security, concurrency, database integration).
import java.util.Scanner;
// Define a class representing a bank account
class BankAccount {
// Define instance variables
private String accountNumber; // Stores the account number
private String holderName; // Stores the name of the account holder
private double balance; // Stores the account balance
// Constructor to initialize the account with account number and holder name
public BankAccount(String accountNumber, String holderName) {
this.accountNumber = accountNumber; // Initialize account number
this.holderName = holderName; // Initialize holder name
this.balance = 0.0; // Initialize balance to 0
}
// Method to deposit funds into the account
public void deposit(double amount) {
balance += amount; // Add the deposited amount to the balance
System.out.println(amount + " deposited successfully."); // Print a success message
}
// Method to withdraw funds from the account
public void withdraw(double amount) {
if (balance >= amount) { // Check if the balance is sufficient for withdrawal
balance -= amount; // Subtract the withdrawal amount from the balance
System.out.println(amount + " withdrawn successfully."); // Print a success message
} else {
System.out.println("Insufficient funds."); // Print an error message if balance is insufficient
}
}
// Method to get the current balance of the account
public double getBalance() {
return balance; // Return the current balance
}
// Method to get the account number
public String getAccountNumber() {
return accountNumber; // Return the account number
}
// Method to get the holder name
public String getHolderName() {
return holderName; // Return the holder name
}
}
// Main class for the banking application
public class BankingApplication {
// Main method, entry point of the program
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create a Scanner object for user input
System.out.println("Welcome to Our Bank!"); // Display a welcome message
System.out.print("Enter your account number: "); // Prompt user to enter account number
String accountNumber = scanner.nextLine(); // Read the account number entered by the user
System.out.print("Enter your name: "); // Prompt user to enter their name
String holderName = scanner.nextLine(); // Read the name entered by the user
BankAccount account = new BankAccount(accountNumber, holderName); // Create a BankAccount object
// Display a menu for banking operations
while (true) {
System.out.println("\n1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Check Balance");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt(); // Read user's choice
switch (choice) {
case 1: // If user chooses deposit
System.out.print("Enter deposit amount: ");
double depositAmount = scanner.nextDouble(); // Read deposit amount
account.deposit(depositAmount); // Call deposit method
break;
case 2: // If user chooses withdraw
System.out.print("Enter withdrawal amount: ");
double withdrawAmount = scanner.nextDouble(); // Read withdrawal amount
account.withdraw(withdrawAmount); // Call withdraw method
break;
case 3: // If user chooses check balance
System.out.println("Account Holder: " + account.getHolderName());
System.out.println("Account Number: " + account.getAccountNumber());
System.out.println("Balance: " + account.getBalance());
break;
case 4: // If user chooses exit
System.out.println("Thank you for using our bank!");
System.exit(0); // Exit the program
default: // If user enters an invalid choice
System.out.println("Invalid choice. Please try again.");
}
}
}
}
This code sets up a basic banking application where users can deposit, withdraw, and check their balance. The BankAccount
class represents a bank account with attributes such as account number, holder name, and balance. The main
method in the BankingApplication
class serves as the entry point for the application. It prompts the user for their account number and name, creates a BankAccount
object, and then presents a menu for performing banking operations.
Welcome to DevTechTutor.com, your ultimate resource for mastering web development and technology! Whether you're a beginner eager to dive into coding or an experienced developer looking to sharpen your skills, DevTechTutor.com is here to guide you every step of the way. Our mission is to make learning web development accessible, engaging, and effective.