Sorry to put all this code out here but I am stuck and have been for days. I am having trouble creating the for loop in the Parking Simulator. I dont even know where to start. I have searched google and I havent found any Parking simulators that used array lists. Here are the requirements:
The ParkingSimulator Class: This class should simulate checking the parking tickets. You should loop through the cars, choose a random officer, and have that officer check to see if there is a parking violation. If there is a violation, add it to the ParkingTicket ArrayList. After all have been checked, print out a summary of the tickets (using the toString() from the ParkingTicket class).
I think I have all the classes right except for the ParkingTicket Class which I know is screwed up in the for loop because I cannot get the random officer and create and report ticket if mins were over maxlimit and it is printing the whole string of officers when it should be picking one officer at random to assign to the ticket. Also it is not printing out the whole output.
I get this error
Exception in thread "main" java.lang.NullPointerException
at parking.ParkingTicket.reportTicket(ParkingTicket.java:19)
at parking.ParkingSimulator.main(ParkingSimulator.java:34)
output should look like:
VW with license # N34234 parked for 60 minutes at PM1 and a maximum time limit of 90 minutes - no violation. Reported by officer Joe badge: PO123
Mazda with license # 234-567 parked for 70 minutes at PM2 and a maximum time limit of 60 minutes was parked illegally for 10 minutes for a fine of 25.0. Reported by officer Sam badge: PO812
etc;
Summary of tickets: License:234-567 at meter #:PM2: Fine: $25.00
License:W879HY4 at meter #:PM4: Fine: $25.00
License:BG65RF7 at meter #:PM5: Fine: $25.00
etc;
This should be the logic of main in 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
}
}
parking simulator class
Here is where I am having trouble in the loop.
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;
}
}