Question

In: Computer Science

Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked...

Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked to calculate the sum of all fines in the policeOfficer class from the arraylist i created. I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way? if not please let me know. What I dont understand how to do is access the fineAmountInCAD from the arrayList to sum it all up? Do i use a for loop? if so how do i just get the fineAmount and have it calculate the sum of that specific variable?

Parking Ticket Class

public class ParkingTicket {
   private String officerName;
   private String officerBadgeNumber;
   private String ticketNumber;
   private String carLicensePlateNumber;
   private double fineAmountInCAD;
        public static int counter = 1000;
   public static final String TICKET_PREFIX = "VAN";
  
  
   public ParkingTicket(String name, String badgeNumber, String carLicense, double fine) {
       generatTicketNumber();
       setOfficerName(name);
       setOfficerBadgeNumber(badgeNumber);
       setCarLicensePlateNumber(carLicense);
       setFineAmountInCAD(fine);
      
   }
  
  
   private void generatTicketNumber() {
       ticketNumber = TICKET_PREFIX + ++counter;
      
   }

   public String getOfficerName() {
       return officerName;
   }
   public void setOfficerName(String name) {
       if(name != null && name.length() > 0) {
       this.officerName = name;
       } else if (name == null) {
           throw new IllegalArgumentException("officer name must not be null");
       } else {
           throw new IllegalArgumentException ("officer name must not be an empty String");
       }
   }


   public String getOfficerBadgeNumber() {
       return officerBadgeNumber;
   }


   public void setOfficerBadgeNumber(String badgeNumber) {
       if(badgeNumber != null && badgeNumber.length() > 0) {
           officerBadgeNumber = badgeNumber;
       } else if (badgeNumber == null) {
           throw new IllegalArgumentException("badge number must not be null");
       } else {
           throw new IllegalArgumentException ("badge number must not be empty String");
       }
   }


   public String getCarLicensePlateNumber() {
       return carLicensePlateNumber;
   }


   public void setCarLicensePlateNumber(String licensePNumber) {
       if(licensePNumber != null && licensePNumber.length() > 0) {
           carLicensePlateNumber = licensePNumber;
       } else if (licensePNumber == null) {
           throw new IllegalArgumentException("car license plate number must not be null");
       } else {
           throw new IllegalArgumentException ("car license plate number must not be empty String");
       }
   }


   public double getFineAmountInCAD() {
       return fineAmountInCAD;
   }


   public void setFineAmountInCAD(double fine) {
       if(fine > 0) {
           fineAmountInCAD = fine;
       }else if(fine == 0) {
           throw new IllegalArgumentException("fine amount must not be 0");
       }else {
           throw new IllegalArgumentException("fine amount must not be negative");
       }
   }
  

   public String getTicketNumber() {
       return ticketNumber;
   }
  
   public void displayDetails() {
       System.out.println("Ticket Number: "+ ticketNumber);
       System.out.println("Officer Name: "+ officerName);
       System.out.println("Officer Badge number: " + officerBadgeNumber);
       System.out.println("Car License Plate Number: "+ carLicensePlateNumber);
       System.out.println("Fine amount: "+ fineAmountInCAD);
   }
  
   public static void main(String[] args) {
       ParkingTicket ticket = new ParkingTicket("Bob Smith", "RCMP2251", "1A2B3C", 150.0);
       ticket.displayDetails();
   }
  
   public static void resetCounter() {
       counter = 1000;
  
   }
}

Police Officer Class

import java.util.ArrayList;


public class PoliceOfficer
{
    public static final int ONE_HOUR_FINE_AMOUNT = 20;
    public static final int MINUTES_IN_HOUR = 60;

    private String officerName;
    private String officerBadgeNumber;
    private ArrayList<ParkingTicket> ticketList;

    public PoliceOfficer(String name, String badgeNumber){
     setOfficerName(name);
     setOfficerBadgeNumber(badgeNumber);
     ticketList = new ArrayList<ParkingTicket>();
    }

    public ArrayList<ParkingTicket> getTicketList(){
        return ticketList;
    }

    public String getOfficerName(){
        return officerName;
    }

    public String getOfficerBadgeNumber(){
        return officerBadgeNumber;
    }

    public void setOfficerName(String officerName){
        if(officerName !=null && !officerName.isEmpty()){
            this.officerName = officerName;
        } else if(officerName == null){
            throw new IllegalArgumentException("officer name must not be null");
        } else if(officerName.isEmpty()) {
            throw new IllegalArgumentException("officer name must not be an empty String");
  
    }
    }

    public void setOfficerBadgeNumber(String officerBadgeNumber){
        if(officerBadgeNumber !=null && !officerBadgeNumber.isEmpty()){
            this.officerBadgeNumber = officerBadgeNumber;
        } else if(officerBadgeNumber == null) {
            throw new IllegalArgumentException("badge number must not be null");
        } else if(officerBadgeNumber.isEmpty()){
            throw new IllegalArgumentException("badge number must not be empty String");
     
     }
   }

   private boolean isParkingTimeExpired(ParkedCar car, ParkingMeter meter){
        if(car.getNumberOfMinutesParked() > meter.getNumberOfPurchasedMinutes())
        {
            return true;
        } else{
            return false;
        }   
     }      

      private double calculateFine(ParkedCar car, ParkingMeter meter){
     
    int totalFine = 0;
    int minutes = car.getNumberOfMinutesParked() - meter.getNumberOfPurchasedMinutes();
     if(isParkingTimeExpired(car,meter)){
      if(minutes % 60 == 0)
      totalFine = (minutes/60) * ONE_HOUR_FINE_AMOUNT;
       else
     totalFine = ((minutes/60) + 1) * ONE_HOUR_FINE_AMOUNT;
     }
     return totalFine;
    }
  

    public void issueParkingTicket(ParkedCar car,ParkingMeter meter){
      
        if(isParkingTimeExpired(car,meter) == true){
          ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
          ticket.displayDetails();
          ticketList.add(ticket);
    
    }

    }
  
    public double sumAllFines(ArrayList<ParkingTicket> tickets){
     
    }
  
}      

   public ParkingTicket issueParkingTicket(ParkedCar car,ParkingMeter meter){
      
        if(isParkingTimeExpired(car,meter) == true){
          ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
          ticket.displayDetails();
        
       
          return ticket;
            } else
             { return null;
    }         
   }

Solutions

Expert Solution

Question:  I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way?

Answer:

  • That's correct way of doing. I noticed that you have two methods with the same name "issueParkingTickets" with the same parameters. Hence, commented out one method.

Question: What I dont understand how to do is access the fineAmountInCAD from the arrayList to sum it all up? Do i use a for loop? if so how do i just get the fineAmount and have it calculate the sum of that specific variable?

Answer:

  • Yes, use for loop to iterate through all the tickets in the list and get the fine amount using getter method "getFineAmountInCAD()".
  • And you no need to pass the arraylist to the method sumAllFines as the tickets arraylist is already member of the class and can be accessed inside the function.

Since there were no information about classes ParkedCar and ParkingMeter and how to test the methods, I could not give you test driver class.

All other classes and methods remain same. Just modified sumAllFines method in PoliceOfficer.java File.

import java.util.ArrayList;

public class PoliceOfficer

{

    public static final int ONE_HOUR_FINE_AMOUNT = 20;

    public static final int MINUTES_IN_HOUR = 60;

    private String officerName;

    private String officerBadgeNumber;

    private ArrayList<ParkingTicket> ticketList;

    public PoliceOfficer(String name, String badgeNumber){

        setOfficerName(name);

        setOfficerBadgeNumber(badgeNumber);

        ticketList = new ArrayList<ParkingTicket>();

    }

    public ArrayList<ParkingTicket> getTicketList(){

        return ticketList;

    }

    public String getOfficerName(){

        return officerName;

    }

    public String getOfficerBadgeNumber(){

        return officerBadgeNumber;

    }

    public void setOfficerName(String officerName){

        if(officerName !=null && !officerName.isEmpty()){

            this.officerName = officerName;

        } else if(officerName == null){

            throw new IllegalArgumentException("officer name must not be null");

        } else if(officerName.isEmpty()) {

            throw new IllegalArgumentException("officer name must not be an empty String");

        }

    }

    public void setOfficerBadgeNumber(String officerBadgeNumber){

        if(officerBadgeNumber !=null && !officerBadgeNumber.isEmpty()){

            this.officerBadgeNumber = officerBadgeNumber;

        } else if(officerBadgeNumber == null) {

            throw new IllegalArgumentException("badge number must not be null");

        } else if(officerBadgeNumber.isEmpty()){

            throw new IllegalArgumentException("badge number must not be empty String");

        }

   }

   private boolean isParkingTimeExpired(ParkedCar car, ParkingMeter meter){

        if(car.getNumberOfMinutesParked() > meter.getNumberOfPurchasedMinutes())

        {

            return true;

        } else{

            return false;

        }  

     }     

    private double calculateFine(ParkedCar car, ParkingMeter meter){

        int totalFine = 0;

        int minutes = car.getNumberOfMinutesParked() - meter.getNumberOfPurchasedMinutes();

        if(isParkingTimeExpired(car,meter)){

            if(minutes % 60 == 0)

              totalFine = (minutes/60) * ONE_HOUR_FINE_AMOUNT;

            else

             totalFine = ((minutes/60) + 1) * ONE_HOUR_FINE_AMOUNT;

        }

        return totalFine;

    }

    public void issueParkingTicket(ParkedCar car, ParkingMeter meter){

        if(isParkingTimeExpired(car, meter) == true){

          ParkingTicket ticket = new ParkingTicket(officerName, officerBadgeNumber, car.getLicensePlateNumber(), calculateFine(car, meter));

          ticket.displayDetails();

          ticketList.add(ticket);

        }

    }

    

    /*public ParkingTicket issueParkingTicket(ParkedCar car, ParkingMeter meter){  

        if(isParkingTimeExpired(car,meter) == true){

          ParkingTicket ticket = new ParkingTicket(officerName, officerBadgeNumber, car.getLicensePlateNumber(),c alculateFine(car,meter));

          ticket.displayDetails();

          return ticket;

        } else {

            return null;

        }

    } */  

    

    public double sumAllFines(){

        double totalfine = 0;

        //Loop through all the tickets and add the fine amount to total

        for(ParkingTicket ticket : ticketList){

            totalfine += ticket.getFineAmountInCAD();

        }

        return totalfine;

    }

}

******************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Happy Studying!!!

******************************************************************************


Related Solutions

Hello, I am stuck on this problem....... A histogram of height measurements among middle students would...
Hello, I am stuck on this problem....... A histogram of height measurements among middle students would most likely be: This was a homework question I am confused because there is no data for how many kids or how tall they were. I am taking a guess thinking that the kids are would be normally distributed? Am I correct? Could you please solve? Thank you normally distributed orthogonal skewed left skewed right
I am stuck on this problem and I am not sure what the solution is. In...
I am stuck on this problem and I am not sure what the solution is. In C Write item.h and item.c. In item.h, typedef a struct (of type t_item) which contains the following information: t_item: char name[MAX_ITEM_NAME_STRING]; char description[MAX_ITEM_DESCRIPTION_STRING]; Make sure that MAX_ITEM_NAME_STRING and MAX_ITEM_DESCRIPTION_STRING are defined with suitable sizes in your item.h. Typical values are, 25 and 80, respectively. Add the following interface definition to item.h: int item_load_items(t_item items[], int max_items, char *filename); Returns the number of objects loaded...
I am stuck on this Java problem: Create an Animal class with: Attributes Age Rabies Vaccination...
I am stuck on this Java problem: Create an Animal class with: Attributes Age Rabies Vaccination Status Name Owner Name A constructor to set values Getters and Setters for all private attributes A toString method that gives the data of all attributes Create a Dog Class Attributes Distemper Vaccination Status A constructor to set values in sub class A constructor to set values in sub and super class Getters and Setters for private attributes A toString method that overrides the...
Parking Ticket simulator For this assignment you will design a set of classes that work together...
Parking Ticket simulator For this assignment you will design a set of classes that work together to simulate a police officer issuing a parking ticket. You should design the following classes: • The ParkedCar Class: This class should simulate a parked car. The class’s responsibili-ties are as follows: – To know the car’s make, model, color, license number, and the number of minutes that the car has been parked. • The ParkingMeter Class: This class should simulate a parking meter....
parking Ticket simulator For this assignment you will design a set of classes that work together...
parking Ticket simulator For this assignment you will design a set of classes that work together to simulate a police officer issuing a parking ticket. You should design the following classes: • The ParkedCar Class: This class should simulate a parked car. The class’s responsibili-ties are as follows: – To know the car’s make, model, color, license number, and the number of minutes that the car has been parked. • The ParkingMeter Class: This class should simulate a parking meter....
Hi! I am in an intro level Finance course and I am stuck on this problem....
Hi! I am in an intro level Finance course and I am stuck on this problem. Any help would be greatly appreciated. I am deciding on opening a restaurant. I was able to scrape together some capital from friends and family, but I must pay them back in 4 years at 12% per annum. I figure that it will cost me $165,000 to start up with rent, deposits, equipment, salaries, chicken, basil, rice, etc. for the first year, but I...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
Hello! I am stuck on the following question: Show that every simple subgroup of S_4 is...
Hello! I am stuck on the following question: Show that every simple subgroup of S_4 is abelian.
Hello this is for C++ language. I am currently stuck on creating my api for Day...
Hello this is for C++ language. I am currently stuck on creating my api for Day Trading Stocks. as follows I need an api for *//Function Signature * * parameter: * * Return Value: ** *// Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument. Your function recommends the maximum profit an investor can make by placing AT MOST one buy and one sell order in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT