简体   繁体   中英

Adding attributes in 2 arraylists

I have 2 arraylist which the records are stored as objects.
First arraylist object has attributes such as patient id , totaltestcost, totaltreatmentcost. Second arraylist object has attributes such as patient id, totalservicecharges.

Now my problem is when i try to add up the total, meaning totaltestcost + totaltreatmentcost + totalservicecharges, for a given patient id, i cant add up the total and display it.

I found out that that since it is in 2 arrays i need to search the arrays by the id and get the costs.

This is the way how i search a patient record public patient_class searchrecord(ArrayList patients, String search) {

    for(patient_class patient: patients) //used  enhanced for loop


        if(patient.getid().equals(search))
        {

            return patient;
        }
    return null;

}

Is there a possibility which i can modify this code to get the costs stored in 2 arrays ?

for example like this :-

public int searchrecord(ArrayList patients, String search) {

    for(patient_class patient: patients) //used  enhanced for loop


        if(patient.getid().equals(search))
        {

           int total= patient.gettotaltestcost + patient.gettotaltreatmentcost
        }
    return null;

} But if i use the above method ill only be able to get the total of totaltestcost+totaltreatmentcost , i need to obtain the servicechargecost as well but its in another array.

So how can i do this ?

Thank you for your time.

Edit :-

Finally i managed to do it guys and here is how i did it :-

public String addtotal(ArrayList patients, String search,ArrayListpatient2,String search2) { int total;

    for(patient_class patient: patients) //used  enhanced for loop

    {
        if(patient.getid().equals(search))
        {



        for(patient_class patient3: patient2)
        {
            if(patient3.getid().equals(search2))
            {
                total=patient.gettreatmentcost()+patient.gettestcost()+patient3.getservicecharge();

                return Double.toString(total);
            }
        }
        }
    }

    return null;

}

i passed in both arrays at once.

Thanks everyone for sharing your answers , next time ill definitely use treemaps

I think you forget about else keyword. All your two method's will be return only null

You need to write somethink like this:

public patient_class searchrecord(ArrayList patients, String search) {

for(patient_class patient: patients) //used  enhanced for loop


    if(patient.getid().equals(search))
    {
        return patient;
    }
    else 
        return null;
}

and this

public int searchrecord(ArrayList patients, String search) {

for(patient_class patient: patients) //used  enhanced for loop


    if(patient.getid().equals(search))
    {
       int total= patient.gettotaltestcost + patient.gettotaltreatmentcost;
       return total;
    }
    else
       return null;
}

I would recommend to change the data structure for a map. If you have to use two arrayLists you can write a method that would return you the desired result as an integer. It is inefficient tho.

public static int total(List<Patient> firstList, List<Patient> secondList, int search)
{
    int total = 0;

    for(Patient p:firstList)
    {
        if(p.getId() == search)
        {
            total = p.getServiceCost();
            for(Patient p2 : secondList)
            {
                if(p2.getId()== search)
                {
                    total += p2.gettotaltestcost() + p2.gettotaltreatmentcost();
                    break;
                }
            }
        }
    }
    return total;
}

That's from top of my head without testing, so you there might be errors. I assumed that id is an integer, you can change it to .equals if it's a String. I hope it helps. If your type is patient_class then of course change all Patient to patient_class. In your main you can do something like

List<patient_class> list1 = new ArrayList<patient_class>();
List<patient_class> list2 = new ArrayList<patient_class>();
// populate those lists 
int search = 12;    
int result = total(list1, list2, int search);

Btw you can pass any number of lists into a method as arguments as long as you specify thei

public int(ArrayList a, ArrayList b)

wouldn't compile.

There are a whole lot of things wrong with your code, but coding to your functionality and disregarding the proper java coding conventions (which you clearly ignore for some reason and which I'm not going to fix). I came up with this. 1 method to find your patient in your list, and 1 method to calculate the patient total from the 2 lists.

public patient_class findPatient(List<patient_class> patients, String search) {
    for(patient_class patient: patients) //used  enhanced for loop
        if(patient.getid().equals(search))
        {
           return patient;
        }
    }
    return null; // no patient found matching the search id
 }

 public int calculatePatientTotal(List<patient_class> patientsList1, List<patient_class> patientsList2, String search){
    patient_class patientInList1 = findPatient(patientsList1,search);// assuming unique id
    patient_class patientInList2 = findPatient(patientsList2,search);// assuming unique id

    int total=0;
    // calc general total
    if(patientInList1!=null{
        // I'm assuming you put these getters in methods instead of public properties!
        // hence the brackets at the end
        total= patientInList1.gettotaltestcost() + patientInList1.gettotaltreatmentcost();
    } 

    // add any extra charges
    if(patientInList2!=null ){
        // I'm assuming this is the method that calculates the other charges in that patient object.
        total+= patientInList2.getOtherCharges();
    }

    return total;
 }

Next time I would recommend you think about the proper structures to use and mainly to not scatter same info over different items.

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