很抱歉把所有这些代码都放在这里但是我被困住了几天。我在停车模拟器中创建for循环时遇到问题。我甚至不知道从哪里开始。我搜索了谷歌,我还没有找到任何使用数组列表的停车模拟器。以下是要求:
ParkingSimulator类:此类应模拟检查停车票。你应该循环通过汽车,选择一名随机的官员,并让该官员检查是否有停车违规。如果存在违规,请将其添加到ParkingTicket ArrayList。检查完毕后,打印出故障单摘要(使用ParkingTicket类中的toString())。
我认为我的所有课程都是正确的,除了ParkingTicket课程,我知道这个课程在for循环中被搞砸了,因为如果分钟超过maxlimit并打印整个字符串,我无法获得随机官员并创建和报告票证当官员应该随机挑选一名官员分配给机票时。它也不是打印出整个输出。
我收到此错误
Exception in thread "main" java.lang.NullPointerException
at parking.ParkingTicket.reportTicket(ParkingTicket.java:19)
at parking.ParkingSimulator.main(ParkingSimulator.java:34)
输出应如下所示:
许可证#N34234的大众在PM1停放60分钟,最长时限为90分钟 - 没有违规。 乔官员报告:PO123
许可证#234-567的马自达在PM2停放70分钟,最长时限为60分钟 非法停车10分钟,罚款25.0。 军官Sam报告:PO812
等;
门票摘要: 执照:234-567米(#:PM2:罚款:25.00美元
)许可证:W879HY4 at meter#:PM4:罚款:$ 25.00
许可证:BG65RF7 at meter#:PM5:罚款:$ 25.00
等;
这应该是ParkingSimulator中主要的逻辑
// create four ArrayLists. One for each class
// create random number generator that will be used to decide which officer will be assigned
// load initial data by calling my method
// loop through the parked cars arraylist
// select a random officer
// output parked car information
// calculate the number of minutes of violation (the number of minutes for the car minus the maximum for the meter
// if number of minutes of violation is greater than zero
//create a ticket
//report ticket
// and add the ticket to parking ticket arraylist
//else
// output that there is no violation
// print out the officer that reported this check
// end loop
// output ticket summary
System.out.println("\nSummary of tickets:");
// loop through the ticket arraylist and output tickets
}
}
停车模拟器类
这是我在循环中遇到麻烦的地方。
package parking;
import java.util.ArrayList;
import java.util.Random;
public class ParkingSimulator {
public static void main(String[] args){
ArrayList<PoliceOfficer> po= new ArrayList<PoliceOfficer>();
ArrayList<ParkingMeter> pm= new ArrayList<ParkingMeter>();
ArrayList<ParkedCar> pc= new ArrayList<ParkedCar>();
ArrayList<ParkingTicket> pt= new ArrayList<ParkingTicket>();
// create random number generator that will be used to decide which officer will be assigned
Random rand = new Random();
loadData(po,pm,pc);
for(int i=0;i<pc.size();i++){
// select a random officer
int a=rand.nextInt(po.size());
// output parked car information
System.out.println(pc.get(i).toString());
// calculate the number of minutes of violation (the number of minutes for the car minus the maximum for the meter
int min=pm.get(i).getNumMinsMax()-pc.get(i).getNumberOfMinutesParked();
// if number of minutes of violation is greater than zero
if(min >0){
//create a ticket
ParkingTicket ticket = new ParkingTicket();
ticket.reportTicket();//report ticket
// and add the ticket to parking ticket arraylist
pt.add(ticket);
}
else{
System.out.println("There is no violation.");
}
// print out the officer that reported this check
System.out.println(po.toString());
// end loop
}
System.out.println("\nSummary of tickets:");
// loop through the ticket arraylist and output tickets
for (int i=0;i<pt.size();i++)
System.out.println(pt.get(i).toString());
}
public static void loadData(ArrayList<PoliceOfficer> po, ArrayList<ParkingMeter> pm, ArrayList<ParkedCar> pc) {
po.add(new PoliceOfficer("Joe", "PO123"));
po.add(new PoliceOfficer("Mike", "PO866"));
po.add(new PoliceOfficer("Suzie", "PO956"));
po.add(new PoliceOfficer("Sam", "PO812"));
pm.add(new ParkingMeter(90,"PM1"));
pm.add(new ParkingMeter(60,"PM2"));
pm.add(new ParkingMeter(30,"PM3"));
pm.add(new ParkingMeter(10,"PM4"));
pm.add(new ParkingMeter(90,"PM5"));
pm.add(new ParkingMeter(60,"PM6"));
pc.add(new ParkedCar("VW", "N34234",60,pm.get(0)));
pc.add(new ParkedCar("Mazda", "234-567",70,pm.get(1)));
pc.add(new ParkedCar("Subaru", "HelloKitty",45,pm.get(2)));
pc.add(new ParkedCar("Buick", "W879HY4",20,pm.get(3)));
pc.add(new ParkedCar("Miata", "BG65RF7",125,pm.get(4)));
pc.add(new ParkedCar("Corvette", "JI9987",10,pm.get(5)));
pc.add(new ParkedCar("Chevy", "12AS76",145,pm.get(1)));
pc.add(new ParkedCar("VW", "MK987",170,pm.get(2)));
pc.add(new ParkedCar("Dodge", "JR4534D",86,pm.get(3)));
pc.add(new ParkedCar("Ford", "C56FDS",60,pm.get(4)));
}
}
parkingticket class
package parking;
public class ParkingTicket {
private ParkedCar pc;
private PoliceOfficer po;
private ParkingMeter pm;
private int minsOver;
private double fine;
public void ParkingTicket(){
}
public void ParkingTicket(ParkedCar pc,PoliceOfficer po,int mo){
minsOver=mo;
}
public void reportTicket(){
if (pc.getNumberOfMinutesParked() > pm.getNumMinsMax())
// Determine the amount of the fine.
minsOver=pc.getNumberOfMinutesParked() - pm.getNumMinsMax();
if (minsOver <= 60)
fine = 25;
else
fine = 25 + (10 * (minsOver / 60));
if(minsOver <0){
System.out.println("No Violation");
}
}
public ParkedCar getPc() {
return pc;
}
public void setPc(ParkedCar pc) {
this.pc = pc;
}
public PoliceOfficer getPo() {
return po;
}
public void setPo(PoliceOfficer po) {
this.po = po;
}
public String toString(){
return " was illegally parked for "+minsOver+" for a fine of "+fine;
}
}
答案 0 :(得分:0)
当您创建新的ParkingTicket()时,您可以通过无参构造函数来完成。接下来,您调用ParkingTicket.reportTicket()方法,该方法引用pc和pm。那时pc和pm从未被设置 - 这会导致nullPointerException。看起来您的代码设置为通过具有参数的构造函数来设置它们。
public void ParkingTicket(ParkedCar pc,PoliceOfficer po,int mo){ minsOver = MO;
}
但即使这样也无法在当前状态下工作,因为它没有设置pc或pm。