Java访问一个ArrayList以将其与一个String参数进行比较?

huangapple go评论86阅读模式
英文:

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

问题

我正在开发一个停车罚单模拟器,尝试编写一个方法,该方法以汽车车牌作为参数,搜索"officerslist"集合,并返回特定车辆被开出的罚单数量。我有以下这些类:

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;
}

我的方法目前如下所示:

public int totalParkingTicketCountOfACar(String licensePlateNumber) {
    int totalCount = 0;
    ArrayList<PoliceOfficer> carTickets = new ArrayList<PoliceOfficer>();

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

    while (it.hasNext()) {
        PoliceOfficer carticket = it.next();
        
        if (carticket.getTicketList().contains(licensePlateNumber)) {
            totalCount++;
        }
    }
    
    return totalCount;
}

我不确定如何将所有的警官与该车牌进行比较,以获取发给特定车辆的罚单数量?非常感谢您的任何帮助! Java访问一个ArrayList以将其与一个String参数进行比较?

英文:

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&lt;ParkingTicket&gt; ticketList;

import java.util.ArrayList;
import java.util.Iterator;
public class PoliceDepartment
{ 
    
    private String address;
    private ArrayList&lt;PoliceOfficer&gt; officerList;

my method looks like this at the moment

public int totalParkingTicketCountOfACar

int totalCount = 0;
    int totalCount = 0;
        ArrayList&lt;PoliceOfficer&gt; carTickets = new ArrayList&lt;PoliceOfficer&gt;();

    Iterator&lt;PoliceOfficer&gt; 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 ! Java访问一个ArrayList以将其与一个String参数进行比较?

答案1

得分: 2

每个PoliceDepartment都有一个PoliceOfficer列表。
每个PoliceOfficer都有一个ParkingTicket列表。
每张ParkingTicket都是针对一辆ParkedCar

您想要遍历所有的PoliceOfficer。对于每个PoliceOfficer,您想要遍历他的ParkingTicket。对于每张ParkingTicket,如果其carLicensePlateNumber与您的参数匹配,您想要增加一个累加总数。

public int getTotalCarTickets(String license) {
    int total = 0;
    PoliceDepartment pd = // 无论您如何获取它。
    for (PoliceOfficer po : pd.getOfficerList()) {
        for (ParkingTicket ticket : po.getTicketList()) {
            if (ticket.getCarLicensePlateNumber().equals(license)) {
                total++;
            }
        }
    }
    return total;
}
英文:

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;
}

huangapple
  • 本文由 发表于 2020年4月4日 05:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61020979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定