简体   繁体   中英

how do I print the list for this

System.out.println("Enter the Account no :");//in my main class
int accNo=sc.nextInt();
switch(n)
{
    case 1 ->{
            
                    
        Account ac=nbank.searchAccount(accNo);//account is my other class
        if(ac!=null)
        {
                    
        System.out.println("Enter the Amount to Deposite :");
        double amt=sc.nextDouble();
                        
        List<Trans> tr=nbank.deposite(accNo,amt);//Trans is also class and I created the list for this class and Trans contains the following below methods
                                            
System.out.println("AccNo :"+tr.getAccNo()+"  TransID :"+tr.getTransId()+"  type: "+tr.getType()+" Amount :"+tr.getWithAmt()+"  Status :"+tr.getStatus()+" Date :"+tr.getDate());

                    

From the withdraw method, I'm getting a list, but somehow I can't print the list contents using LinkedList. For every method it shows method not found. Class I made are as Trans(getter and setter), bank as logic.

How do I print the list for the above programs using LinkedList?

List<Trans> withdraw(int accNo,double amt)//in my bank class where i'm implementing the logic and trying to store to other classes. 

Here my intention is to view the withdraw the transaction done by the customer in my main():

{
    List<Trans> tr=new LinkedList<Trans>();
    Account ac=searchAccount(accNo);
    if(ac instanceof FDAccount)
        tr=((FDAccount)ac).withdraw(amt);
    else if(ac instanceof Account)
        tr=ac.withdraw(amt);
    return tr;
}

This is my withdraw method that is called by List<Trans> tr=nbank.withdraw(accNo,amt);

UserUI.java:189: error: cannot find symbol
                                                        System.out.println("AccNo :"+tr.getAccNo()+"  TransID :"+tr.getTransId()+"  type: "+tr.getType()+" Amount :"+tr.getDepAmt()+"  Status :"+tr.getStatus()+" Date :"+tr.getDate());
                                                                                       ^
  symbol:   method getAccNo()
  location: variable tr of type List<Trans>
UserUI.java:189: error: cannot find symbol`

Error like this etc.

tr is a List as returned by withdraw method. As such it does not have method getAccNo .

You probably mean reference to an instance of Trans in the list eg for the first Trans in the List :

tr.get(0).getAccNo()

Or for every Trans :

for (Trans tran : tr) {
  System.out.println("AccNo :"+tran.getAccNo()+"  TransID :"+tran.getTransId()+"  type: "+tran.getType()+" Amount :"+tran.getWithAmt()+"  Status :"+tran.getStatus()+" Date :"+tran.getDate());
}

Hard to be more specific or sure without additional clarifications/code in your question.

As @xlm said, tr is a List, you only can access getter by index. You can use for loop or foreach , you can't use tr.getAcNo() .

This is a list, in java you must loop or iterate through the list, for an example:

//indexed loop
for (int i = 0; i < tr.size(); i++) {
    System.out.println("AccNo :"+tr.get(i).getAccNo()+"  TransID :"+tr.get(i).getTransId()+"  type: "+tr.get(i).getType()+" Amount :"+tr.get(i).getWithAmt()+"  Status :"+tr.get(i).getStatus()+" Date :"+tr.get(i).getDate());
}

or

//foreach loop
for (Trans trans: tr) {
    System.out.println("AccNo :"+trans.getAccNo()+"  TransID :"+trans.getTransId()+"  type: "+trans.getType()+" Amount :"+trans.getWithAmt()+"  Status :"+trans.getStatus()+" Date :"+trans.getDate());
}

I think you want to search through the list and print a specific element, to do this, you need account number that want to search, here is some code for you:

for (Trans trans: tr) {
    if (accNo.equals(trans.getAccNo())) {
            System.out.println("AccNo :"+trans.getAccNo()+"  TransID :"+trans.getTransId()+"  type: "+trans.getType()+" Amount :"+trans.getWithAmt()+"  Status :"+trans.getStatus()+" Date :"+trans.getDate());
    }
}

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