停车模拟器如何创建和报告车票
对不起,把所有这些代码放在这里,但我被卡住了,已经有好几天了。我在停车模拟器中创建for循环时遇到问题。我甚至不知道从哪里开始。我搜索谷歌,我还没有找到任何使用数组列表的停车模拟器。这里是要求:停车模拟器如何创建和报告车票
ParkingSimulator类:这个类应该模拟检查停车票。你应该循环车辆,选择一名随机的官员,并让该官员检查是否存在违规停车。如果存在违规,请将其添加到ParkingTicket ArrayList中。全部检查完毕后,打印票据摘要(使用ParkingTicket类中的toString())。
我认为除了ParkingTicket Class之外,我知道所有类都是正确的,因为如果分钟超过maxlimit,并且它正在打印整个文档,我无法获得随机人员并创建并报告票证他们应该随机挑选一名军官分配给机票。而且它不打印出整个输出。
我得到这个错误
Exception in thread "main" java.lang.NullPointerException
at parking.ParkingTicket.reportTicket(ParkingTicket.java:19)
at parking.ParkingSimulator.main(ParkingSimulator.java:34)
输出应该是这样的:
大众汽车停放在PM1 60分钟,90分钟的最长时限许可证编号N34234 - 没有违规。 通过官乔徽章报道:PO123
马自达与牌照#234-567停放在PM270分钟和60分钟 最大时间限制被违停10分钟,25.0以下的罚款。 报警官Sam徽章:PO812
etc;
门票摘要: 许可证:234-567在米#:PM2:精细:$ 25.00
许可:W879HY4在米#:PM4:精细:$ 25.00
许可:BG65RF7在米#: PM5:Fine:$ 25.00
etc;
这应该是主要的逻辑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类
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;
}
}
当您创建新ParkingTicket()你通过无参数的构造做。接下来调用引用pc和pm的ParkingTicket.reportTicket()方法。那时pc和pm从来没有被设置 - 这会导致nullPointerException。看起来你的代码是通过构造函数来设置的,它的确有参数。
public void ParkingTicket(ParkedCar pc,PoliceOfficer po,int mo)minsOver = mo;
}
但即使这样,也不会在工作状态下它是目前因为它不设置PC或下午。
好吧,所以我固定了分钟扣除,但我仍然在报告票证时感到困惑。我在哪里设置下午和电脑
请缩小您的代码并发布重要的内容。 – 2014-09-13 06:00:03
我主要关注的停车模拟器for循环我只是把所有的代码,因为我需要访问这些不同的类 – SikRik 2014-09-13 06:04:30
此代码 - int min = pm.get(i).getNumMinsMax() - pc.get(i ).getNumberOfMinutesParked();如果汽车停放时间超过仪表最大值,会给您一个负数。像60-80 = -20是违规行为。然后检查是否(min> 0)违反了不一致的规则。 – jch 2014-09-13 07:29:49