Simulate a basic bank account with deposit, withdrawal, and balance check features using Java programming language.
Introduction
The Bank Account Simulator is a simple Java program that allows users to interact with a virtual bank account. This program enables users to deposit money, withdraw money, and check the balance of their account. It is an excellent way to learn the basics of object-oriented programming, classes, methods, and user input in Java.
Objective
The objective of this project is to simulate the functionality of a bank account. The program will allow the user to perform the following operations:
- Deposit money into the account
- Withdraw money from the account
- Check the current balance
Java Code for Bank Account Simulator
import java.util.Scanner;
// BankAccount class
class BankAccount {
private double balance;
// Constructor
public BankAccount(double initialBalance) {
if (initialBalance > 0) {
balance = initialBalance;
} else {
balance = 0;
}
}
// Deposit method
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: $" + amount);
} else {
System.out.println("Deposit amount must be positive.");
}
}
// Withdraw method
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) { balance -= amount; System.out.println("Withdrew: $" + amount); } else if (amount > balance) {
System.out.println("Insufficient balance.");
} else {
System.out.println("Withdrawal amount must be positive.");
}
}
// Check balance method
public void checkBalance() {
System.out.println("Current Balance: $" + balance);
}
}
// Main class to run the program
public class BankAccountSimulator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter initial balance for your account: ");
double initialBalance = scanner.nextDouble();
// Create BankAccount object
BankAccount account = new BankAccount(initialBalance);
while (true) {
System.out.println("\nChoose an operation:");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Check Balance");
System.out.println("4. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter amount to deposit: ");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
break;
case 3:
account.checkBalance();
break;
case 4:
System.out.println("Exiting program...");
scanner.close();
return;
default:
System.out.println("Invalid choice! Please try again.");
}
}
}
}
Explanation of the Program
This Java program simulates a simple bank account with features for depositing, withdrawing, and checking the balance. Here’s a breakdown of how the program works:
- BankAccount Class: This class contains methods for performing operations such as deposit, withdrawal, and checking the balance. It also has a constructor that initializes the balance with a starting value.
- Deposit Method: Allows the user to deposit a specified amount into the account. The deposit must be a positive number.
- Withdraw Method: Allows the user to withdraw a specified amount from the account, provided they have enough balance.
- Check Balance Method: Displays the current balance of the account.
- Main Method: The program runs in an infinite loop, allowing the user to select options like deposit, withdraw, check balance, or exit. It uses a scanner to get input from the user.
How to Run the Program
To run the Bank Account Simulator program:
- Ensure that you have Java installed on your system.
- Create a new file named
BankAccountSimulator.java
and copy the Java code into this file. - Compile the program using the command
javac BankAccountSimulator.java
. - Run the compiled program using
java BankAccountSimulator
. - Follow the on-screen prompts to deposit, withdraw, and check your account balance.
Seeking the top online casino in Australia? **WFGaming** offers fantastic **no deposit bonuses** for new players.
If you’re looking for **WFGaming Free** or **Free Deposit 365**,
this is definitely the platform to check out.
With **WFGaming Free 365**, you’ll find exclusive deals that make your gaming experience even more exciting.
**WF Gaming Australia** is a trusted platform that
offers a range of bonuses, including **WFGaming Free**, so you can enjoy your favorite games without
any initial investment. Plus, **Free Deposit 365** ensures that you get access to the best bonuses and deals on the market.
Whether you’re into slots or virtual casino options, **WFGaming** has something for everyone.
**Join WFGaming Free** at **WF Gaming** now and experience
special bonuses and promotions. Don’t miss out on the
top gaming opportunities in Australia with **WFGaming Free** and **Free Deposit 365**.
Begin your journey with **WF Gaming** and get your **free deposit bonus** today!
You won’t regret it!