Simple Banking Application Using Java With Source Code
Introduction:
Simple Banking Application is a simple Java project for beginners to start their career in coding. You’ll learn about Scanner class to take inputs, and the basics of strings, loops, methods, and conditional statements. Here, simple banking operations like deposit, withdrawal, checking balance, exit, etc.
Explanation:
Java is a powerful programming language that is commonly used for building enterprise-level applications. One of the most popular application domains for Java is banking. Java provides a robust set of libraries and frameworks that make it easy to develop a banking application.
In this article, we will take a look at how to build a simple banking application using Java. Our application will have the following basic functionality:
Check balance
Deposit money
Withdraw money
Exit the application
The first step in building our application is to create a new Java project. We will use the command-line interface to create a new project and set up the project structure. Once the project is set up, we can start writing the code for our application.
The main class of our application will be called “Bank”. In this class, we will define the main method and the basic functionality of our application. The main method will be responsible for displaying the menu to the user and handling the user’s input.
The menu will consist of four options:
Check balance
Deposit money
Withdraw money
Exit the application
The user will be prompted to select an option by entering a number between 1 and 4. If the user enters an invalid option, the application will display an error message and prompt the user to enter a valid option.
Source Code:
import java.util.Scanner;
public class Bank {
static double balance = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int option = 0;
while (option != 4) {
System.out.println("Welcome to the Bank of Java");
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 an option: ");
option = scanner.nextInt();
switch (option) {
case 1:
checkBalance();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
exit();
break;
default:
System.out.println("Invalid option. Try again.");
break;
}
}
}
public static void checkBalance() {
System.out.println("Your current balance is $" + balance);
}
public static void deposit() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the amount to deposit: $");
double amount = scanner.nextDouble();
balance += amount;
System.out.println("$" + amount + " has been deposited to your account.");
checkBalance();
}
public static void withdraw() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the amount to withdraw: $");
double amount = scanner.nextDouble();
if (amount > balance) {
System.out.println("Insufficient funds.");
} else {
balance -= amount;
System.out.println("$" + amount + " has been withdrawn from your account.");
}
checkBalance();
}
public static void exit() {
System.out.println("Thank you for banking with us. Have a great day!");
}
}
The check balance option will display the current balance of the user’s account. The deposit and withdraw options will prompt the user to enter the amount of money they want to deposit or withdraw. If the user tries to withdraw more money than they have in their account, the application will display an error message.
The exit option will close the application and display a farewell message to the user.
Output:
Find More Projects
Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …
Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …
Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …
Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …
Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …
Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …