Write a menu driven application to perform the banking operations using JAVA.
Your application must contain the following functionalities. Use constructors, getter
and setter functions wherever required.
In the menu give options for Account Creation, Balance Enquiry,
Deposit and
Withdrawal
Do not allow to withdraw money if the balance is < = 500
Initial balance must be a minimum of 500.
public class Account {
private double balance;
public Account(double balance) {
if (balance < 500) {
this.balance = 500;
} else {
this.balance = balance;
}
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public boolean withdraw(double amount) {
if (balance > 500) {
balance -= amount;
return true;
}
return false;
}
public boolean deposit(double amount) {
balance += amount;
return true;
}
}
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Account account = null;
int choice;
double amount;
while (true) {
System.out.println("\n1. Account Creation");
System.out.println("2. Balance Enquiry");
System.out.println("3. Deposit");
System.out.println("4. Withdrawal");
System.out.println("0. Exit");
System.out.print("Select option: ");
choice = in.nextInt();
switch (choice) {
case 1:
account = new Account(1000);
System.out.println("Account created.");
break;
case 2:
if (account != null) {
System.out.println("Balance is " + account.getBalance());
} else {
System.out.println("Create account.");
}
break;
case 3:
if (account != null) {
System.out.print("Enter the amount to deposit: ");
amount = in.nextDouble();
System.out.println(account.deposit(amount) ? "Success" : "Failure");
} else {
System.out.println("Create account.");
}
break;
case 4:
if (account != null) {
System.out.print("Enter the amount to withdraw: ");
amount = in.nextDouble();
System.out.println(account.withdraw(amount) ? "Success" : "Failure");
} else {
System.out.println("Create account.");
}
break;
case 0:
return;
}
}
}
}
Comments
Leave a comment