Java accessing an Arraylist to compare it to a String parameter?

onesnapthanos :

I am working on a parking ticket simulator and I am trying to write a method that uses the car license plate as a parameter which searches the collection of officerslist and returns the number of tickets issued to a specific car. I have these classes

public class ParkedCar {


private String ownerName;
private String carMake;
private String licensePlateNumber;
private int modelYear;
private int numberOfMinutesParked;

public class ParkingTicket {
    private String officerName;
    private String officerBadgeNumber;
    private String ticketNumber;
    private String carLicensePlateNumber;
    private double fineAmountInCAD;

public class PoliceOfficer
{ 

    private String officerName;
    private String officerBadgeNumber;
    private ArrayList<ParkingTicket> ticketList;

import java.util.ArrayList;
import java.util.Iterator;
public class PoliceDepartment
{ 

    private String address;
    private ArrayList<PoliceOfficer> officerList;

my method looks like this at the moment

public int totalParkingTicketCountOfACar

int totalCount = 0;
    int totalCount = 0;
        ArrayList<PoliceOfficer> carTickets = new ArrayList<PoliceOfficer>();

    Iterator<PoliceOfficer> it = carTickets.iterator();

     while(it.hasNext()){

            PoliceOfficer carticket = it.next();


         if(officerList.equalsIgnoreCase(licensePlateNumber)){
             totalCount++;
            }
        }

          return totalCount;

I am not sure how to go about having the all the officers in the list and compare it to that license plate to get the number of tickets issued to a specific car? any help is much appreciated ! :)

Abra :

Each PoliceDepartment has a list of PoliceOfficer.
Each PoliceOfficer has a list of ParkingTicket.
Each ParkingTicket is for one ParkedCar.

You want to iterate through all the PoliceOfficer's. For each one you want to iterate through his ParkingTicket's. For each ParkingTicket whose carLicensePlateNumber matches your parameter, you want to increment a running total.

public int getTotalCarTickets(String license) {
    int total = 0;
    PoliceDepartment pd = // However you obtain it.
    for (PoliceOfficer po : pd.getOfficerList()) {
        for (ParkingTicket ticket : po.getTicketList()) {
            if (ticket.getCarLicensePlateNumber().equals(license)) {
                total++;
            }
        }
    }
    return total;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403817&siteId=1