Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Java Objects (http://www.go4expert.com/forums/java-objects-t14157/)

cyrow 23Sep2008 21:46

Java Objects

import java.text.DecimalFormat;

public class Account{
        //class Variables
        private static int NumAccounts = 0;
        private static final double Interest = 0.05;
        //Instant Variables
        private  long accNum;
        private  double balance;
        DecimalFormat x = new DecimalFormat("$00,000.00");//Format Balance
        public Account(long accN, double bal){//Constructor
                accNum  = accN;
                balance = bal;
        public int GetNumAccounts(){
                return NumAccounts;
        public double getBalance(){
                return balance;
        public long getAccountNum(){
                return accNum;
        public void debit(double amount){
                if(balance >= amount){
                  balance -= amount;       
        public void credit(double amount){
                balance += amount;
        public void setNumAccounts(int numAccounts){//deincrement accounts
        public String toString(){
                String Str;
                Str ="Account Number  :" + accNum + "\n"+
                        "Account Balance  :" + x.format(balance) + "\n";
                return Str;
}//End of class Account
import java.io.*;
import java.util.*;

public class TestAccount{
private static  final int Max = 1000;
 static Account[]accList= new Account[Max];

public static Account getAccount(Account z){//using binary search to find and return account if exist
int lo = 0;
int hi = accList.length - 1;

while(lo <= hi){
        int mid = (lo + hi)/2;
        int q =        accList[mid].compareTo(z);
                return accList[mid];
        if(q < 0){
                mid = hi - 1;
                mid = lo + 1;
}//end while

return null;
}//end getaccoutn;       

public static void insertIntoArray(Account p)
  {//Insert Objects into account
                  int numAccounts = Account.GetNumAccounts();
        accList[numAccounts - 1] = p;
      int lo = 0;
      int hi = numAccounts -1;
                  for(int j = lo + 1; j <= hi; j++){
                          Account hold = accList[j];
                          int k = j - 1;
                          while (k >= 0 && hold.getAccountNum()< accList[k].getAccountNum()){
                                  accList[k + 1] = accList[k];
                          }//End While
                          accList[k + 1] = hold;
                  }//End For
  }        //End Insertion Sort       


public static void transaction(int accNum, double amount,int transType)
              if (transType == 1){
      if (transType == 2){
  }//end transaction
  public static void deleteAccount(long accNum){
          int j;
          for(int i = 0; j < Account.GetNumAccounts(); i++){
                          if(i == Account.getNumAccounts()){
                                  System.out.println("Print error message");
                                  for(j = i; j < Account.getAccountNum(); j++){
                                          accList[j] = accList[j + 1];
                  }//end if
          }//end for
  }//end function


        public static void main(String[] args){
        Account        a = new Account(123456,5255.17);
        Account        b = new Account(188468,19218.20);
        Account        c = new Account(217794,1002.62);
        Account        d = new Account(998283,20203.75);

 //Create Transactions
                Account q = getAccount(a);
                        System.out.println("Account does not exist");
        for(int i = 0; i < 4; i++){
        }//end main
}//end class TestAccount

If you compile my code you will see the errors. in the GetAccount()method, i am using the binarySort() to return an Account,but it is not working. Please help me.
First, I am to create an Account class (works ok) . Next, I am to test the Account class by creating a TestAccount class with the following methods:
getAccount(Account) //returns an Account from the array accList if the account exist(Using the binarySearch)
insertIntoArray(Account)//insert an object into accList using and Insertion sort to maintain order.
deleteAccount(accNum)return type void. Delete and account from the array if exist.
transaction(accNum, amount, transType) return type void. Receives an account from the array and perform debit(1) or credit(2).
printAccounts()// prints the accounts (works ok)

All times are GMT +5.5. The time now is 07:17.