In: Computer Science
Write a class called Account that contains: • Three private instance variables: name (String), account (String), account balance (double). • One constructor, which constructs all instances with the values given. • Getters and setters for all the instance variables. • Debit function that takes an amount from the user and subtract the balance (make sure the balance will not drop below zero after the operation, if this was the case, the function should return false else it should return true) • Credit function that takes an amount from the user and add it to the balance. Write a test program called TestAccount to test the constructor and public methods in the class Account. Ask the user to enter the information for four accounts and instantiate them, then allow the user to choose the operation that he wants on those accounts. Operations are Show balance or Debit or Credit, if he chose debit or credit ask him to specify the amount, in case of debit if the amount is not sufficient tell him “insufficient amount”.
Sample Output: Enter Name: Ali Moussa Enter Account Number: 11223344 Enter Balance: 1000
Enter Name: Mohammed Omran Enter Account Number: 22334455 Enter Balance: 1500
Enter Name: Hassan Youssef Enter Account Number: 33445566 Enter Balance: 200
Enter Name: Yasser Mustafa Enter Account Number: 44556677 Enter Balance: 2000 Which account you wish to process? 3 Choose your operation: 1. Show Balance 2. Debit 3. Credit 2 Enter Amount: 50 Your new balance is 150 Which account you wish to process? 3 Choose your operation: 1. Show Balance 2. Debit 3. Credit 2 Enter Amount: 170 Insufficient Balance Which account you wish to process? 3 Choose your operation: 1. Show Balance 2. Debit 3. Credit 1 Your balance is 150
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// Account.java
public class Account {
private String name;
private String account;
private double balance;
public Account(String name, String account, double
balance) {
this.name = name;
this.account = account;
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public boolean debit(double amt) {
if ((balance - amt) >= 0)
{
this.balance-=amt;
return
true;
}
else
return
false;
}
public void credit(double amt) {
this.balance += amt;
}
}
___________________________
// TestAccount.java
import java.util.Scanner;
public class TestAccount {
public static void main(String[] args) {
String name, accno;
double bal;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
Account accounts[] = new Account[4];
for (int i = 0; i <
accounts.length; i++) {
System.out.print("Enter Name:");
name =
sc.nextLine();
System.out.print("Enter Account Number:");
accno =
sc.nextLine();
System.out.print("Enter Balance::");
bal =
sc.nextDouble();
sc.nextLine();
Account acc =
new Account(name, accno, bal);
accounts[i] =
acc;
}
for(int i=0;i<3;)
{
System.out.print("\nWhich account you wish to process?");
int
num=sc.nextInt();
if(num<1 ||
num>accounts.length)
{
System.out.println("** Invalid Number
**");
}
System.out.print("Choose your operation: 1. Show Balance 2. Debit
3. Credit:");
int
choice=sc.nextInt();
switch(choice)
{
case 1:{
System.out.println("Your balance is
"+accounts[num-1].getBalance());
i++;
break;
}
case 2:{
System.out.print("Enter Amount:");
double
amt=sc.nextDouble();
if(!accounts[num-1].debit(amt))
{
System.out.println("insufficient amount");
}
else
{
System.out.println("Your new balance is
"+accounts[num-1].getBalance());
}
i++;
break;
}
case 3:{
System.out.print("Enter Amount:");
double
amt=sc.nextDouble();
accounts[num-1].credit(amt);
System.out.println("Your new balance is
"+accounts[num-1].getBalance());
i++;
break;
}
default:{
System.out.println("** invalid choice **");
break;
}
}
}
}
}
_________________________
Output:
Enter Name:Ali Moussa
Enter Account Number:11223344
Enter Balance::1000
Enter Name:Mohammed Omran
Enter Account Number:22334455
Enter Balance::1500
Enter Name:Hassan Youssef
Enter Account Number:33445566
Enter Balance::200
Enter Name:Yasser Mustafa
Enter Account Number:44556677
Enter Balance::2000
Which account you wish to process?3
Choose your operation: 1. Show Balance 2. Debit 3. Credit:2
Enter Amount:50
Your new balance is 150.0
Which account you wish to process?3
Choose your operation: 1. Show Balance 2. Debit 3. Credit:170
** invalid choice **
Which account you wish to process?3
Choose your operation: 1. Show Balance 2. Debit 3. Credit:2
Enter Amount:170
insufficient amount
Which account you wish to process?3
Choose your operation: 1. Show Balance 2. Debit 3. Credit:1
Your balance is 150.0