File Account.java (see previous 4.1. exercise) contains a definition for a simpl
ID: 3797624 • Letter: F
Question
File Account.java (see previous 4.1. exercise) contains a definition for a simple bank account class with methods towithdraw, deposit, get the balance and account number, and return a String representation. Note that the constructor forthis class creates a random account number. Save this class to your directory and study it to see how it works. Then writethe following additional code:
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!");
}
}
1. Suppose the bank wants to keep track of how many accounts exist.a. Declare a private static integer variable numAccounts to hold this value. Like all instance and static variables, it will
be initialized (to 0, since it’s an int) automatically.b. Add code to the constructor to increment this variable every time an account is created.c. Add a static method getNumAccounts that returns the total number of accounts. Think about why this method should
be static - its information is not related to any particular account.
d. The provided File TestAccounts1.java contains a simple program that creates the specified number of bankaccounts then uses the getNumAccounts method to find how many accounts were created. Save it to your directory,then use it to test your modified Account class.
2. Add a method void close() to your Account class. This method should close the current account by appending“CLOSED” to the account name and setting the balance to 0. (The account number should remain unchanged.) Alsodecrement the total number of accounts.
3. Add a static method Account consolidate(Account acct1, Account acct2) to your Account class that creates a newaccount whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account shouldbe returned. Two important rules of consolidation:
• Only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a newaccount number.
• Two accounts with the same number cannot be consolidated. Otherwise this would be an easy way to double yourmoney!
Check these conditions before creating the new account. If either condition fails, do not create the new account or closethe old ones; print a useful message and return null.
4. Run the provided test program, namely TestAccounts2.java that prompts for and reads in three names and creates anaccount with an initial balance of $ 100 for each. Print the three accounts, then close the first account and try toconsolidate the second and third into a new account. Now print the accounts again, including the consolidated one if itwas created.
//************************************************************// TestAccounts1// A simple program to test the numAccts method of the// Account class.//************************************************************
import java.util.Scanner;
public class TestAccounts1{
public static void main(String[] args){
Account testAcct;
Scanner scan = new Scanner(System.in);
System.out.println("How many accounts would you like to create?");
int num = scan.nextInt();
for (int i=1; i<=num; i++)
{
testAcct = new Account(100, "Name" + i);
System.out.println(" Created account " + testAcct);
System.out.println("Now there are " + Account.numAccounts () +
" accounts");
}
}
}
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
import java.util.Random;
public class Account
{
private double balance;
private String name;
private long acctNum;
private static int numAccounts = 0;
//-------------------------------------------------//Constructor -- initializes balance, owner, and account number//-------------------------------------------------
public Account(String owner)
{
this(0, owner, new Random().nextLong());
}
//-------------------------------------------------//Constructor -- initializes balance, owner, and account number//-------------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = Math.abs(number);
numAccounts++;
}
public Account(double initBal, String owner)
{
this(initBal, owner, new Random().nextLong());
}
//-------------------------------------------------// 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;
else
System.out.println("Insufficient funds");
}
//-------------------------------------------------// Adds deposit amount to balance.//-------------------------------------------------
public void deposit(double amount){balance += amount;
}
//-------------------------------------------------// Returns balance.//-------------------------------------------------
public double getBalance()
{
return balance;
}
public static int getNumAccounts(){
return numAccounts;
}
public void close(){
name = name+"CLOSED";
balance = 0;
}
public Account consolidate(Account acct1, Account acct2){
if(acct1.name.equalsIgnoreCase(acct2.name)){
Account conAcct = new Account(acct1.name);
conAcct.deposit(acct1.balance + acct2.balance);
conAcct.acctNum = new Random().nextLong(); // new accoutn number
return conAcct;
}
else{
return null; // can not be consolidate
}
}
//-------------------------------------------------// Returns a string containing the name, account number, and balance.//-------------------------------------------------
public String toString()
{
return "Name:" + name +
" Account Number: " + acctNum +
" Balance: " + balance;
}
}
import java.util.Scanner;
public class TestAccounts2
{
public static void main(String[] args)
{
Account testAcct;
Scanner scan = new Scanner(System.in);
System.out.println("How many accounts would you like to create?");
int num = scan.nextInt();
for (int i=1; i<=num; i++)
{
testAcct = new Account(100, "Name" + i);
System.out.println(" Created account " + testAcct);
System.out.println("Now there are " + Account.getNumAccounts() +
" accounts");
}
}
}
/*
Sample run:
How many accounts would you like to create?
5
Created account Name:Name1
Account Number: 9062480550914002909
Balance: 100.0
Now there are 1 accounts
Created account Name:Name2
Account Number: 4229377706125191751
Balance: 100.0
Now there are 2 accounts
Created account Name:Name3
Account Number: 478366054882403945
Balance: 100.0
Now there are 3 accounts
Created account Name:Name4
Account Number: 6731845114956798394
Balance: 100.0
Now there are 4 accounts
Created account Name:Name5
Account Number: 7433847391558430069
Balance: 100.0
Now there are 5 accounts
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.