英文:
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;
}
我不确定如何将所有的警官与该车牌进行比较,以获取发给特定车辆的罚单数量?非常感谢您的任何帮助!
英文:
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 !
答案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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论