简体   繁体   中英

java incompatible types class data types

Hi this is my first post on here and I have hit a stump. I'm suppose to make a class based off a pre made data type class called Account. My main issues lie within public int findAccountByAcctNumber(int acctNumber) and public Account removeAccount(int index). The difficulty is how to create these methods with the different data types?

import java.util.*;

public class Bank
{

    private ArrayList<Account> Accounts;
    private int currentSize;
    private String bankName;


    public Bank(String name)
    {
        bankName = name;
        Accounts = new ArrayList<Account>(0);

    }


    public void addAccount(Account acct){
        Accounts.add(acct);

    }
    public int findAccountByAcctNumber(int acctNumber){
        int tempIndex = -1;
        for(int i = 0; i < Accounts.size(); i++){
            if(Accounts.get(i) == acctNumber){
                tempIndex = i;
            }
        }
        return tempIndex;

    }
    public Account removeAccount(int index){

        Accounts.remove(index);
        Account 
        return index;

    }
    public String toString(){
        String output = "";

        output += bankName + "/n";
        for(int i = 0; i < arrlist.size(); i++){
            output += Accounts.get(i);
        }
        return output;


    }


}

You haven't shown us the Account class, but I'm guessing it has a field accountNumber .

You need to compare the input account number to the field, not the Account object itself:

  public int findAccountByAcctNumber(int acctNumber){
        int tempIndex = -1;
        for(int i = 0; i < Accounts.size(); i++){
            //NOT  if(Accounts.get(i) == acctNumber){ ->
            if(Accounts.get(i).getAccountNumber() == acctNumber){
                tempIndex = i;
            }
        }
        return tempIndex;

    }

remove is pretty simple (ArrayList has already implemented this function):

public Account removeAccount(int index){
    return Accounts.remove(index);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM