Hi Everyone, I have to add a static method Account consolidate(Account acct1, Account acct2) to an existing Account class that creates a new account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned. (only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new account number. Two accounts with the same number cannot be consolidated, otherwise could double money)
Check these conditions before creating the new account. If either condition fails, do not create the new account or close the old ones; print a useful message and return null.
Account class
import java.util.Random;
public class Account
{
private double balance;
private String name;
private long acctNum;
private static int numAccounts; //initialized to 0 automatically
private static double consolidatedBalance;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
numAccounts++;
}
/* overload1 */
public Account(double initBal, String owner)
{
balance = initBal;
name = owner;
Random generator = new Random();
long rndNumber = generator.nextLong();
long rndNumberPos = Math.abs(rndNumber);
acctNum = rndNumberPos;
numAccounts++;
}
/* overload2 */
public Account(String owner)
{
balance = 0;
name = owner;
Random generator = new Random();
long rndNumber = generator.nextLong();
long rndNumberPos = Math.abs(rndNumber);
acctNum = rndNumberPos;
numAccounts++;
}
//----------------------------------------------
// 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");
}
public void withdraw(double amount, double fee)
{
if (balance >= amount + fee)
balance -= amount + fee;
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;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name: " + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
}
/* Returns total number of accounts */
public static int getNumAccounts()
{
return numAccounts;
}
/* closes the current account */
void close()
{
name = name + " CLOSED";
balance = 0;
numAccounts--;
System.out.println(name);
}
/* */
public static Account consolidate(Account acct1, Account acct2)
{
if (acct1.name == acct2.name && acct1.acctNum != acct2.acctNum)
{
Account consolidatedAccount = new Account(0, acct1.name);
double a1Bal = acct1.getBalance();
double a2Bal = acct2.getBalance();
consolidatedBalance = a1Bal + a2Bal;
consolidatedAccount.balance = consolidatedBalance;
acct1.close();
acct2.close();
return consolidatedAccount;
}
else
{
System.out.println("Cannot consolidate, either names do not match"
+ " or account numbers are the same");
return null;
}
}
}
Write a test program that prompts for and reads in three names and creates an account with an initial balance of $100 for each. Print the three accounts, then close the first account and try to consolidate the second and third into a new account. No print the accounts again, including the consolidated one if it was created.
TestAccounts2 class
import java.util.Scanner;
public class TestAccounts2
{
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
final int INITIALBALANCE = 100;
String name1, name2, name3;
Account account1, account2, account3, account4;
System.out.println("Enter name one: ");
name1 = scan.nextLine();
System.out.println("Enter name two: ");
name2 = scan.nextLine();
System.out.println("Enter name three: ");
name3 = scan.nextLine();
account1 = new Account(100, name1);
account2 = new Account(100, name2);
account3 = new Account(100, name3);
System.out.println(account1 +"\n"+ account2 +"\n"+ account3);
account1.close();
account4 = new Account(name2);
account4.consolidate(account2, account3);
System.out.println("\n" + account4);
}
}
Output
Enter name one:
albert
Enter name two:
brenda
Enter name three:
charles
Name albert
Account number: 23987429749
Balance: 100.0
Name brenda
Account number: 23947293874
Balance: 100.0
Name charles
Account number: 23724385748
Blance: 100.0
albert CLOSED
Cannot consolidat, either names do not match or account are the same.
Name: brenda
Account Number: 34574593479
Balance: 0.0
As you can see my output is not whats needed. Any suggestions on how to get the consolidateAccounts method in Account class to work properly as well as how to test it properly in TestAccounts2. Thanks for any help.