Static Method and Bank Account Program
807598Nov 27 2005 — edited Nov 27 2005Hello, I am currently taking my first Java class, and am having a very hard time with my latest assignment.
The directions state: Add a static method Account Consolidate(Account acct1, Account acct2) to your 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.
Two important rules of consolidation:
1. Only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new account number.
2. Two accounts with the same number cannot be consolidated. Otherwise this would be an easy way to double your 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.
Then, write a test program that prompts for and reads in three names and creates and 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. Now print the accounts again, including the consolidated one if it was created.
---------------------So, I have been working on this for HOURS and really feel more confused now then ever. For the test file I only have a small bit done because I can't get the method Account Consolidate to work.
//************************************************** *****
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//************************************************** *****
public class Account
{
private double balance;
private String name;
private long acctNum;
private static int numAccounts;
private double newAccount;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
long acctNum = number;
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");
}
//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
//----------------------------------------------
// Returns the total number of accounts.
//----------------------------------------------
public static int getNumAccounts()
{
return numAccounts;
}
//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}
//----------------------------------------------
// Returns name on account.
//----------------------------------------------
public String getName()
{
return name;
}
//----------------------------------------------
// Returns number of account.
//----------------------------------------------
public long getAcctNum()
{
return acctNum;
}
//----------------------------------------------
// Close the current account.
//----------------------------------------------
public void close()
{
if (balance == 0)
numAccounts--;
System.out.println("CLOSED");
}
//----------------------------------------------
// Consolidates two accounts into one account.
//----------------------------------------------
public static AccountConsolidate(Account acct1, Account acct2)
{
newAccount = acct1+acct2;
close acct1;
close acct2;
return newAccount;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name:" + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
}
}
--------------------------Here is the test file-----------------------
//**********************************************
// TestAccounts2
// A simple program to test the methods of the
// Account class.
//**********************************************
import java.util.Scanner;
public class TestAccounts2
{
public static void main(String[] args)
{
Account acct1;
Account acct2;
Account acct3;
Scanner scan = new Scanner(System.in);
int num;
System.out.println("Enter the name for account 1: ");
int name1 = scan.nextInt();
System.out.println("Enter the name for account 2: ");
int name2 = scan.nextInt();
System.out.println("Enter the name for account 3: ");
int name3 = scan.nextInt();
for (int i=1; i>=num; i++)
acct1 = new Account(100, "name1", i);
System.out.println("\nCreated account " + name1);
System.out.println("Balance Available " + acct1.getBalance() + " account number: " + acct1.getAcctNum());
}
}
IF ANY ONE HAS ANY IDEAS OR CAN POINT ME IN THE RIGHT DIRECTION I WOULD REALLY APPRECIATE IT. THANKS!!