// BankAccount.java
public class BankAccount {
private String name;
private double balance;
public void deposit(double amount){
balance += amount;
System.out.println(amount + " is successfully Deposited");
}
public void withdraw(double amount){
if(balance >= amount){
balance -= amount;
System.out.println(amount + " is successfully Withdrawn");
}
else{
System.out.println("Insufficient funds");
}
}
public double getBalance(){
return balance;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//BankAccountTest.java
import java.util.Scanner;
public class BankAccountTest{
public static void main(String args[]){
BankAccount account = new BankAccount();
Scanner input = new Scanner(System.in);
System.out.println("Enter account holder name: ");
String name = input.nextLine();
int choice;
do{
System.out.println("Press 1: To Deposit an amount\nPress 2: To Withdraw an amount\nPress 3: To View the current balance\nPress 4: To Close the program");
choice = input.nextInt();
switch(choice){
case 1:
System.out.println("Enter the amount you want to Deposite");
double depositeAmount = input.nextDouble();
account.deposit(depositeAmount);
break;
case 2:
System.out.println("Enter the amount you want to withdraw");
double withdrawAmount = input.nextDouble();
account.withdraw(withdrawAmount);
break;
case 3:
System.out.println("Your current balance is " + account.getBalance());
break;
case 4:
System.out.println("The program is terminated");
break;
default:
System.out.println("Incorrect choice. Please try again!");
break;
}
}while(choice!=4);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter