Java 停车模拟机如何创建和报告车票

Java 停车模拟机如何创建和报告车票,java,arrays,methods,associations,Java,Arrays,Methods,Associations,很抱歉把这些代码放在这里,但我被卡住了,已经好几天了。我在停车模拟器中创建for循环时遇到问题。我甚至不知道从哪里开始。我搜索过谷歌,但没有找到任何使用数组列表的停车模拟器。以下是要求: ParkingSimulator类:该类应模拟检查停车罚单。你应该在车里绕一圈,随机挑选一个警官,让那个警官检查是否有违规停车的情况。如果存在冲突,请将其添加到ParkingTicket阵列列表。检查完所有内容后,打印出票证摘要(使用ParkingTicket类中的toString() 我想除了ParkingT

很抱歉把这些代码放在这里,但我被卡住了,已经好几天了。我在停车模拟器中创建for循环时遇到问题。我甚至不知道从哪里开始。我搜索过谷歌,但没有找到任何使用数组列表的停车模拟器。以下是要求:

ParkingSimulator类:该类应模拟检查停车罚单。你应该在车里绕一圈,随机挑选一个警官,让那个警官检查是否有违规停车的情况。如果存在冲突,请将其添加到ParkingTicket阵列列表。检查完所有内容后,打印出票证摘要(使用ParkingTicket类中的toString()

我想除了ParkingTicket类之外,我所有的类都是正确的,我知道它在for循环中出错了,因为如果分钟数超过最大限制,我无法获取随机官员并创建和报告票据,并且它正在打印整个官员字符串,而它应该随机挑选一名官员分配给票据。它也没有打印出整个输出

我得到这个错误

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 badge报告:PO812

等等,

门票摘要: 许可证号码:234-567米处#:PM2:罚款:$25.00

许可证:W879HY4 at仪表#:PM4:罚款:25.00美元

许可证:BG65RF7计价器:PM5:罚款:$25.00

等等,

这应该是驻车模拟机的主要逻辑

    // 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)));

}
}
package停车场;
导入java.util.ArrayList;
导入java.util.Random;
公共级停车模拟器{
公共静态void main(字符串[]args){
ArrayList po=新的ArrayList();
ArrayList pm=新的ArrayList();
ArrayList pc=新的ArrayList();
ArrayList pt=新的ArrayList();
//创建随机数生成器,用于决定将分配给哪个官员
Random rand=新的Random();
装载数据(采购订单、pm、pc);
对于(inti=0;i0){
//创建票据
ParkingTicket票证=新ParkingTicket();
ticket.reportTicket();//报告ticket
//并将车票添加到停车票阵列列表
加件(票);
}     
否则{
System.out.println(“没有违反规则”);
}
//打印出报告这张支票的官员
System.out.println(po.toString());
//端环
}
System.out.println(“\n票证摘要:”);
//循环遍历票证数组列表并输出票证
对于(int i=0;i pm.getNumMinsMax())
//确定罚款数额。
minsOver=pc.getNumberOfMinutesParked()-pm.getNumMinsMax();

如果(minsOver当您创建新的ParkingTicket()时,您可以通过无参数构造函数来执行此操作。接下来,您将调用ParkingTicket.reportTicket()引用pc和pm的方法。此时pc和pm从未设置过-这会导致nullPointerException。看起来您的代码是通过具有参数的构造函数设置的

公共停车场(停车场pc、警察po、内务部){ minshover=mo

}


但是,即使在当前的状态下,它也无法工作,因为它没有设置pc或pm。

请缩小代码范围并发布重要内容。我主要关注停车模拟器中的for循环。我只是放置所有代码,因为我需要访问这些不同的类。此代码-int min=pm.get(I).getNumMinsMax()-pc.get(I).getNumberOfMinutesParked();如果汽车停放时间超过了仪表最大值,则会给出一个负数。例如60-80=-20是违规行为。然后检查(最小值>0)可能的重复我无法更改构造函数,因为这是umlOk中需要的,所以我修复了错误,我减去了错误,但现在仍然与票证报告混淆,不确定我应该在哪里设置pc和pmFix您的构造函数有参数并传递pc和创建新的ParkingTicket()对象时进行pm。
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;
}

}