Create 2 Java files: 1. Account.java - Base class definition for a bank account
ID: 3708727 • Letter: C
Question
Create 2 Java files: 1. Account.java - Base class definition for a bank account 2. AccountClient.java
Specifications for the Account class:
Specifications for the Client program:
The program will start with the prompt: Please create your account. Type a positive integer as account number. When the user enters a number, the account is created with a balance of zero dollar. Following prompt is displayed. Your account XXXX is created with zero balance. You can do the following operations. The menu choice is then displayed.
Required methods:
Class Name Account Instance Variables (fields) accountNumber: int ce: double Constructors + Account(int accountNumber) Instance Methods + getAccountNumber) : int + getBalance): double + credit(double amount): void + debit(double aExplanation / Answer
Account.java
public class Account {
//Declaring instance variables
private int accountNumber;
private double balance;
//Parameterized constructor
public Account(int accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0.0;
}
// getters
public int getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public void credit(double amount) {
balance += amount;
}
public void debit(double amount) {
if (amount > balance) {
System.out.println("Amount withdrawn exceeds the current balance");
} else
balance -= amount;
}
}
_________________
AccountClient.java
import java.util.Scanner;
public class AccountClient {
public static void main(String[] args) {
//Declaring variables
int accNum;
char choice;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner console = new Scanner(System.in);
//Getting the input entered by the user
System.out.println("Please create your account.Type a positive integer as account number.");
accNum = console.nextInt();
Account myAccount = new Account(accNum);
System.out.println("Your account " + accNum + " is created with zero balance. You can do the following operations.");
do {
displayMainMenu();
choice = getValidMenuChoice(console);
processMenuChoice(console, choice, myAccount);
} while (choice != 'Q' && choice != 'q');
}
private static void processMenuChoice(Scanner console, char choice,
Account myAccount) {
//getting the choice entered by the user
switch (choice) {
case 'A':
case 'a':
{
debit(console, myAccount);
break;
}
case 'T':
case 't':
{
credit(console, myAccount);
break;
}
case 'B':
case 'b':
{
displayAccount(myAccount);
break;
}
}
}
private static void displayAccount(Account myAccount) {
System.out.println("Your Balance :$" + myAccount.getBalance());
}
private static void credit(Scanner console, Account myAccount) {
System.out.print("Enter amount :$");
double amt = console.nextDouble();
myAccount.debit(amt);
}
private static void debit(Scanner console, Account myAccount) {
System.out.print("Enter amount :$");
double amt = console.nextDouble();
myAccount.credit(amt);
}
private static char getValidMenuChoice(Scanner console) {
char ch;
while (true) {
System.out.print("Enter Choice :");
ch = console.next(".").charAt(0);
if (ch == 'A' || ch == 'a' || ch == 'T' || ch == 't' || ch == 'B' || ch == 'b' || ch == 'Q' || ch == 'q') {
break;
} else {
System.out.println("** Invalid Choice **");
continue;
}
}
return ch;
}
//displaying the menu
private static void displayMainMenu() {
System.out.println(" Bank Account Operations");
System.out.println("A - Add money to account");
System.out.println("T - Take out money from account");
System.out.println("B - Show Balance");
System.out.println("Q - Quit");
}
}
_________________
Output:
Please create your account.Type a positive integer as account number.
12345
Your account 12345 is created with zero balance. You can do the following operations.
Bank Account Operations
A - Add money to account
T - Take out money from account
B - Show Balance
Q - Quit
Enter Choice :A
Enter amount :$50000.00
Bank Account Operations
A - Add money to account
T - Take out money from account
B - Show Balance
Q - Quit
Enter Choice :B
Your Balance :$50000.0
Bank Account Operations
A - Add money to account
T - Take out money from account
B - Show Balance
Q - Quit
Enter Choice :T
Enter amount :$23000.00
Bank Account Operations
A - Add money to account
T - Take out money from account
B - Show Balance
Q - Quit
Enter Choice :B
Your Balance :$27000.0
Bank Account Operations
A - Add money to account
T - Take out money from account
B - Show Balance
Q - Quit
Enter Choice :Q
_______________Thank You