0% found this document useful (0 votes)
17 views

Simple Banking Application Code

Java

Uploaded by

anil uppati
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Simple Banking Application Code

Java

Uploaded by

anil uppati
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner; // This line imports the Scanner class


from the java.util package, allowing the program to read input from the user.

public class SimpleBankingApplication { // This line begins the definition of the


class SimpleBankingApplication.

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in); //This line declares a
variable scanner of type Scanner and initializes it to take input from the system.
It also initializes the variable balance to 0.
int balance = 0;

// Display Menu
while (true) { //This line starts a while loop that runs
indefinitely until a break statement is encountered.
System.out.println("1. Check Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt(); // These lines print the menu
options for the user and prompt the user to enter their choice, which is stored in
the variable choice.

// Check Balance
if (choice == 1) {
System.out.println("Your balance is: " + balance); // If the user
chooses option 1, this block prints the current balance.
}
// Deposit Money
else if (choice == 2) {
System.out.print("Enter the amount to deposit: ");
int amount = scanner.nextInt();
balance += amount;
System.out.println("Amount deposited successfully. Your new balance
is: " + balance); // If the user chooses option 2, this block prompts the user to
enter the amount to deposit, adds the amount to the balance, and prints the updated
balance.
}
// Withdraw Money
else if (choice == 3) {
System.out.print("Enter the amount to withdraw: ");
int amount = scanner.nextInt();
if (amount <= balance) {
balance -= amount;
System.out.println("Amount withdrawn successfully. Your new
balance is: " + balance); // If the user chooses option 3, this block prompts the
user to enter the amount to withdraw. It checks if the amount is less than or equal
to the current balance. If it is, it deducts the amount from the balance and prints
the updated balance.
} else {
System.out.println("Insufficient balance. Withdrawal failed.");
// If the amount exceeds the balance, it prints an insufficient balance message.
}
}
// Exit Application
else if (choice == 4) {
System.out.println("Thank you for using the Simple Banking
Application"); // If the user chooses option 4, this block prints a closing
message and breaks out of the while loop, effectively ending the program.
break;
}
// Invalid Choice Handling
else {
System.out.println("Invalid choice. Please choose a valid
option."); // If the user selects an option other than 1, 2, 3, or 4, this block
prints an error message indicating an invalid choice.
}
}
scanner.close(); // This line closes the Scanner object to release system
resources.
}
}

These Java statements together create a simple banking application that allows
users to check their balance, deposit and withdraw money, and exit the application.

1. Import Statement: The code begins with an import statement that brings in the
necessary Scanner class from the java.util package to facilitate user input.

2. Class Definition: The class SimpleBankingApplication is defined, which contains


the main method, the entry point for the application.

3. Variable Declaration and Initialization: Inside the main method, a Scanner


object scanner is created to read user input, and the balance variable is
initialized to 0 to represent the initial account balance.

4. Display Menu (while Loop): The application enters a while loop that repeatedly
displays the menu options and awaits the user's choice.

5. Check Balance: If the user selects option 1, the current balance is displayed
using System.out.println.

6. Deposit Money: If the user selects option 2, the program prompts the user to
enter the amount they want to deposit. The entered amount is added to the existing
balance, and the updated balance is displayed.

7.Withdraw Money: If the user selects option 3, the program prompts the user to
enter the amount they want to withdraw. It checks whether the requested amount is
less than or equal to the current balance. If it is, the requested amount is
subtracted from the balance, and the updated balance is displayed. If the requested
amount exceeds the balance, an appropriate message is displayed.

8. Exit Application: If the user selects option 4, the program displays a closing
message and breaks out of the while loop, effectively ending the program.

9. Invalid Choice Handling: If the user selects an option other than 1, 2, 3, or 4,


the program displays an error message indicating an invalid choice.

10. Closing Operations: After the loop is exited, the Scanner object is closed to
release the resources.

The provided Java code demonstrates the steps involved in building a simple banking
application, including menu display, user input, balance check, deposit,
withdrawal, and error handling for invalid choices.1.

You might also like