Account


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Account

  1. #1
    Join Date
    Nov 2006
    Posts
    5

    Account

    Opening and Closing Accounts

    File Account.java contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a String representation. Note that the constructor for this class creates a random account number. Save this class to your directory and study it to see how it works. Then write the following additional code:

    Suppose the bank wants to keep track of how many accounts exist.

    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.

    Add code to the constructor to increment this variable every time an account is created.

    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.

    File TestAccounts1.java contains a simple program that creates the specified number of bank accounts 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.


    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.) Also decrement the total number of accounts.

    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: 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 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.


    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. Now print the accounts again, including the consolidated one if it was created.

    Here is my code
    //*******************************************************
    // 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;
    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 balance.
    //----------------------------------------------
    public double getBalance()
    {
    return balance;
    }


    //----------------------------------------------
    //Returns number of accounts created
    //----------------------------------------------
    public static int getNumAccounts()
    {
    return numAccounts;
    }

    //----------------------------------------------
    // Returns name on the account
    //----------------------------------------------
    public String getName()
    {
    return name;
    }

    //----------------------------------------------
    // Returns account number
    //----------------------------------------------
    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 Account AccountConsolidate(Account acct1, Account acct2)
    {
    newAccount = acct1.getBalance() + acct2.getBalance();

    String name1 = acct1.getName();
    String name2 = acct2.getName();
    if(name1.equals(name2));
    newAccount = newAccount.getAcctNum();
    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;
    }
    }

    Now here are my errors when I compile it

    Account.java:95: non-static variable newAccount cannot be referenced from a static context
    newAccount = acct1.getBalance() + acct2.getBalance();
    ^
    Account.java:100: non-static variable newAccount cannot be referenced from a static context
    newAccount = newAccount.getAcctNum();
    ^
    Account.java:100: non-static variable newAccount cannot be referenced from a static context
    newAccount = newAccount.getAcctNum();
    ^
    Account.java:100: double cannot be dereferenced
    newAccount = newAccount.getAcctNum();
    ^
    Account.java:101: cannot resolve symbol
    symbol : class close
    location: class Account
    close acct2;
    ^
    Account.java:101: acct2 is already defined in AccountConsolidate(Account,Account)
    close acct2;
    ^
    Account.java:102: non-static variable newAccount cannot be referenced from a static context
    return newAccount;
    ^
    7 errors


    I also need help with my TestAccount too.

    //***********************************************************
    // TestAccounts1.java
    //
    // 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();

    or (int i=1; i<=num; i++)
    {

    testAcct = new Account(100, "Name", i);
    System.out.println("\nCreated account " + testAcct);
    System.out.println("Now there are " + Account.getNumAccounts() +
    " accounts");
    }
    }
    }

    Tmcclain
    View Public Profile
    Send a private message to Tmcclain
    Find More Posts by Tmcclain
    Add Tmcclain to Your Buddy List

    Yesterday, 08:55 AM

  2. #2
    Join Date
    Nov 2006
    Location
    NEW DELHI, INDIA
    Posts
    22

    suggestions

    Hi Tmcclain,

    Some of the errors in your code are due to the following code in your code::

    newAccount = acct1.getBalance() + acct2.getBalance();
    newAccount = newAccount.getAcctNum();
    close acct2;

    Note that whenever u r using a variable to store a reference to an object, u must declare the type of that variable also.
    define the close method in a way that it accepts the object as argument for which account is to be closed. it should be called like:
    close(object);
    I haven't analysed your code completely, just the "main".

    thanks,
    Last edited by abhijava; 11-17-2006 at 06:09 PM.
    Thanks,
    abhi

  3. #3
    Join Date
    Nov 2006
    Posts
    5
    I am a beginner at this so Can you tell what exactly I need to change in order to help me with my program. Thanks

  4. #4
    Join Date
    Nov 2006
    Location
    NEW DELHI, INDIA
    Posts
    22
    if u are a beginner then u should first grasp the basics from some good source. i mean books. which should be specially written for beginners.
    Thanks,
    abhi

Similar Threads

  1. Highschool Java Class Investigation
    By rsixtyone in forum Java
    Replies: 3
    Last Post: 10-12-2005, 06:02 AM
  2. DCOM, domains, and the guest account
    By Robert in forum Enterprise
    Replies: 0
    Last Post: 11-06-2002, 10:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center