Well i'm having trouble with this code with the following lines:
return new AccountID(this);
return new Customer(this);
^These two from the clone method.
Customer myCustomer = new Customer(newBank.Find(myAccountID));
^Not sure what i'm doing wrong here, it is on 4 different methods of deposit, withdrawal etc.

Not only that, but my return function for Balance & CustomerName seems off. As well as the report method. Everything else is fine though. Here is the whole code from scratch. Any help is appreciated.

public class Bank
{


   private static class AccountID implements Comparable<AccountID>, HasClone<AccountID>
   {
   public AccountID(int iID)
   {
    ID = iID;
   } 
   public int compareTo(AccountID N)
   {
   return ID-N.ID;
 } 
   public AccountID Clone()
   {
   return new AccountID(this);
   };
public int ID;
};

  private static class Customer implements HasVisit, HasClone<Customer>
  {
   public Customer(String customerName, double iBalance)
   { 
   CustomerName = customerName;
   Balance = iBalance;
    };
   public void Visit() {
   System.out.println("Customer Name : "+CustomerName+" Balance : "+Balance);
   };
   public Customer Clone()
   {
    return new Customer(this);
   };



   public String CustomerName;
   public double Balance;

  };

   public void AddAccount(int accountID, String customerName) throws Exception
   {
   AccountID myAccountID = new AccountID(accountID);
   Customer myCustomer = new Customer(customerName,0);
   newBank.Insert(myAccountID, myCustomer); 




   };


   public void RemoveAccount(int accountID) throws Exception
   {
   AccountID myAccountID = new AccountID(accountID);
   newBank.Delete(myAccountID);
   };


   public void Deposit(int accountID, double amount) throws Exception
   {
   AccountID myAccountID = new AccountID(accountID);
   Customer myCustomer = new Customer(newBank.Find(myAccountID));
   myCustomer.Balance = myCustomer.Balance + amount;
   newBank.Update(myAccountID, myCustomer);
   };


   public void Withdraw(int accountID, double amount) throws Exception
   {
   AccountID myAccountID = new AccountID(accountID);
   Customer myCustomer = new Customer(newBank.Find(myAccountID));
   myCustomer.Balance = myCustomer.Balance - amount;
   newBank.Update(myAccountID, myCustomer);
   };


   public double Balance(int accountID) throws Exception
   {
   AccountID myAccountID = new AccountID(accountID);
   Customer myCustomer = new Customer(newBank.Find(myAccountID));


     return myCustomer.Balance;
   };

   public String CustomerName(int accountID) throws Exception
   {

   AccountID myAccountID = new AccountID(accountID);
   Customer myCustomer = new Customer(newBank.Find(myAccountID));
     return mycustomer; 
   };


   public void Report()
   {
   Report(AccountID, Customer);
   };

 private SearchTree<AccountID, Customer> newBank = new SearchTree<AccountID, Customer>(); 

};

Recommended Answers

All 3 Replies

private static class AccountID implements Comparable<AccountID>, HasClone<AccountID> {
    public AccountID(int iID) {
        ID = iID;
    }

    public int compareTo(AccountID N) {
        return ID-N.ID;
    }

    public AccountID Clone() {
        return new AccountID(this);
    }

    public int ID;
};

That's your code, cleaned up a bit for readability.
Your "return new AccountID(this)" will fail, as you no doubt experienced.
The reason is that you did not supply a copy constructor "public AccountID(AccountID a)".
You also have a rather weird construct here with that "HasClone<>" interface. Why not use the perfectly good "Cloneable" interface from the standard library instead?

Your code would then change to something like

private static class AccountID implements Comparable<AccountID>, Cloneable {
    public int ID;

    public AccountID(int iID) {
        ID = iID;
    }


    public int compareTo(AccountID N) {
        return ID-N.ID;
    }

    public clone(Object o) {
        if (!(o instanceof AccountID) {
        }
        return new AccountID(((AccountID)o).ID);        
    }
};

Similarly, your Customer class has no constructor taking an argument of type Customer, hence that too fails. Either make such a constructor, or change the calling code to use the existing constructor instead.

Your method to retrieve the customer name tries to return the Customer, not his name.

And the balance method will return the balance just fine, but you're running into the inherent problem with floating point numbers on computers, which is rounding errors.
These are the reason that no real program will use floating point numbers like this. Instead you'd either use integers for dollars and cents (so your customer class (not debating the design decision, that field belongs on the account class...) would have not a balance field but a dollars field and a cents field (though probabably better named to be independent of any specific currency).
Or better yet, make use of the BigDecimal class to define the amount instead, which handles all that stuff internally.

Last tip: use proper CamelCase. No capital letters for method and member/variable names, helps reduce confusion.

Thanks jwenting. After reworking, my errors went from 13 to 2 errors. I fixed the problems you suggested. I believe the only issue is just my Report method, which only requires one line of coding. My report needs to have a tree report. I believe I need to have the visit function for the customer account.

Still reworking the single code but now I believe I just have to print the information with all customer name's and i'm finished.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.