Question: File Account.java contains a definition for a simple bank account class with methods to withdraw, deposit, get the balanceand account number, and return a String
File Account.java contains a definition for a simple bank account class with methods to withdraw, deposit, get the balanceand account number, and return a String representation. Note that the constructor for this class creates a random accountnumber. Save this class to your directory and study it to see how it works. Then modify it as follows: 1. Overload the constructor as follows: public Account (double initBal, String owner, long number) - initializes the balance, owner, and account
number as specified public Account (double initBal, String owner) - initializes the balance and owner as specified; randomlygenerates the account number. public Account (String owner) - initializes the owner as specified; sets the initial balance to 0 and randomlygenerates the account number.
2. Overload the withdraw method with one that also takes a fee and deducts that fee from the account. File TestAccount.java contains a simple program that exercises these methods. Save it to your directory, study it to see whatit does, and use it to test your modified Account class.
public class Account
{
private double balance;
private String name;
private long acctNum; //-------------------------------------------------//Constructor -- initializes balance, owner, and account number//-------------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
} //-------------------------------------------------// Checks to see if balance is sufficient for withdrawal.// If so, decrements balance by amount; if not, prints message.//-------------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
elseSystem.out.println("Insufficient funds");
}
//-------------------------------------------------// Adds deposit amount to balance.//------------------------------------------------- public void deposit(double amount){balance += amount;
} //-------------------------------------------------// Returns balance.//-------------------------------------------------
public double getBalance()
{
return balance;
} //-------------------------------------------------// Returns a string containing the name, account number, and balance.//-------------------------------------------------
public String toString()
{
return "Name:" + name +
" Account Number: " + acctNum +
" Balance: " + balance;
}
}
//************************************************************// TestAccount.java//// A simple driver to test the overloaded methods of// the Account class.//************************************************************ import java.util.Scanner; public class TestAccount
{
public static void main(String[] args)
{
String name;
double balance;
long acctNum;
Account acct; Scanner scan = new Scanner(System.in); System.out.println("Enter account holder's first name");
name = scan.next();
acct = new Account(name);
System.out.println("Account for " + name + ":");
System.out.println(acct); System.out.println(" Enter initial balance");
balance = scan.nextDouble();
acct = new Account(balance,name);
System.out.println("Account for " + name + ":");
System.out.println(acct);
System.out.println(" Enter account number");
acctNum = scan.nextLong();
acct = new Account(balance,name,acctNum);
System.out.println("Account for " + name + ":");
System.out.println(acct); System.out.print(" Depositing 100 into account, balance is now ");
acct.deposit(100);
System.out.println(acct.getBalance());
System.out.print(" Withdrawing $25, balance is now ");
acct.withdraw(25);
System.out.println(acct.getBalance());
System.out.print(" Withdrawing $25 with $2 fee, balance is now ");
acct.withdraw(25,2);
System.out.println(acct.getBalance()); System.out.println(" Bye!");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
