Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS PROBLEM I BOOS FROM BOOK STARTING OUT WITH C++ FROM CONTROL STRUCTURES TROU

ID: 3819014 • Letter: T

Question

THIS PROBLEM I BOOS FROM BOOK STARTING OUT WITH C++ FROM CONTROL STRUCTURES TROUGHT OBJECTS

TONY GADDIS PAGE 889 PROBLEM 14

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. The classes you should design are: The ParkedCar Class: This class should simulate a parked car. The class's responsibilities are 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. The class's only responsibility is To know the number of minutes of parking time that has been purchased The Parking Ticket Class: This class should simulate a parking ticket. The class's responsibilities are: To report the make, model, Color, and license number of the illegally parked car To report the amount of the fine, which is $25 for the first hour or part of an hour that the car is egally parked, plus $10 for every additional hour or part of an hour that the car is egally parked To report the name and badge number of the police officer issuing the ticket The Policeofficer class: This class should simulate a police officer inspecting parked cars The class's responsibilities are: To know the police officer's name and badge number To examine a ParkedCar object and a ParkingMeter object, and determine whether the cars time has expired To issue a parking ticket (generate a ParkingTicket object) if the cars time has expired Write a program that demonstrates how these classes Collaborate.

Explanation / Answer

/**Test java class ParkingCarDemo that tests the classes
* Policeoffice, parkingticket,parkingmeter and parkedcar*/
//ParkingCarDemo.java
public class ParkingCarDemo
{
   public static void main(String[] args) {

       //Create an instance of ParkedCar class with parking minutes 40
       ParkedCar car = new ParkedCar("Johnson",
               "2012", "White",
               "ABCXYZ", 40);
      
       //create an instance of ParkingMeter with purchase minutes 50
       ParkingMeter meter = new ParkingMeter(50);
      
       //Create an intance of PoliceOfficer
       PoliceOfficer officerMiller = new PoliceOfficer
               ("Mathew", 070);
      
       //parking office search for car and its meter to raise a ticket
       ParkingTicket ticket = officerMiller.search(car, meter);

       System.out.println("For car , "+car.toString());
       //checking if ticket is not null
       if (ticket != null)
           System.out.println(ticket.toString());
       else
           System.out.println("No ticket issued!");
      
      
       // A second car checked to see if it passes or not, it's over
       ParkedCar car2 = new ParkedCar("Market", "1999",
               "yellow", "xyzABC", 120);
       System.out.println("For car , "+car2.toString());
       ParkingMeter meter2 = new ParkingMeter(70);
       ParkingTicket ticket2 = officerMiller.search(car2, meter2);
       //checking if ticket is not null
       if (ticket2 != null)
           System.out.println(ticket2.toString());
       else
           System.out.println("No ticket issued!");
      
   }
}
---------------------------------------------------------------------------------
//PoliceOfficer.java
public class PoliceOfficer {
   private String name;
   private int badge;
   private double ticket;

   public PoliceOfficer()
   {
       name="Not set";
       badge=0;
       ticket=0;
      
   }

   public PoliceOfficer(String poName, int poBadge) {

       name = poName;
       badge = poBadge;
   }

   public String getName() {
       return name;
   }

   public int getBadge() {
       return badge;
   }
   public String toString() {

       String string = "Officer : " + name
               + " Badge: " + badge;
              
       return string;

   }
   public ParkingTicket search(ParkedCar car, ParkingMeter meter) {

       PoliceOfficer po = new PoliceOfficer(this.name,this.badge);
       int time = ParkedCar.getMinutes() - ParkingMeter.getMinPurchased();

       if (ParkedCar.getMinutes() > ParkingMeter.getMinPurchased()) {
           if (time <= 60) {
               setTicket(25);

           } else {
               setTicket(25 + (10 * (time / 60)));
           }
       }
       //No ticket for time less than 0
       if( time <=0)
           return null;
       return new ParkingTicket(car, po, getTicket(), time);
   }

   public double getTicket() {
       return ticket;
   }

   public void setTicket(double ticket) {
       this.ticket = ticket;
   }
}
---------------------------------------------------------------------------------

//ParkingTicket.java
public class ParkingTicket
{
   private ParkedCar vehicle;
   private PoliceOfficer copster;
   private double fine;
   private int minutes;
   private final double firstFine = 25;
   private final double moreFine = 10;

   public ParkingTicket(ParkedCar car,
           PoliceOfficer cop,
           double guyFine, int mins) {

       vehicle = car;
       copster = cop;
       fine = guyFine;
       minutes = mins;
   }

   //Returns fine
   public void getTotalFine()
   {
       int time = ParkedCar.getMinutes() - ParkingMeter.getMinPurchased();

       if (time <= 60) {
           fine = firstFine;
       } else {
           fine = firstFine + moreFine * (time / 60);
       }
   }

   public double getFirstFine() {
       return firstFine;
   }

   public double getMoreFine() {
       return moreFine;
   }

   public ParkedCar getVehicle() {
       return vehicle;
   }

   public PoliceOfficer getCopster() {
       return copster;
   }

   public int getMinutes() {
       return minutes;
   }

   public double getFine() {
       return fine;
   }

   public String toString() {

       String string = "Fine : " + this.fine
               + " Minutes: " + minutes
               + " " + vehicle.toString()
               + " " + this.getCopster().toString();

       return string;

   }
}
---------------------------------------------------------------------------------
//The ParkingMeter Class
public class ParkingMeter {

   private static int minPurchased;

   public ParkingMeter() {
       minPurchased=0;
   }

   public ParkingMeter(int carMinPurchased) {

       minPurchased = carMinPurchased;
   }

   public static int getMinPurchased() {
       return minPurchased;
   }

   public String toString() {
       String string = "Minutes Purchased: " + minPurchased;
       return string;
   }
}
---------------------------------------------------------------------------------

//ParkedCar.java
public class ParkedCar {
   private String make;
   private String model;
   private String color;
   private String license;
   private static int minutes;

   public ParkedCar()
   {
   }

   public ParkedCar(String carMake, String carModel,
           String carColor, String carLicense,
           int carMinutes)
   {
       make = carMake;
       model = carModel;
       color = carColor;
       license = carLicense;
       minutes = carMinutes;
   }

   public String getMake() {
       return make;
   }

   public String getModel() {
       return model;
   }

   public String getColor() {
       return color;
   }

   public String getLicense() {
       return license;
   }

   public static int getMinutes() {
       return minutes;
   }

   public String toString() {

       return String.format("Make:%s,Model:%s,Color: %s,License Plate: %s, Minutes %d",
               make,model,color,license,minutes);
   }
}
---------------------------------------------------------------------------------
Sample Output:
For car , Make:Johnson,Model:2012,Color: White,License Plate: ABCXYZ, Minutes 40
No ticket issued!
For car , Make:Market,Model:1999,Color: yellow,License Plate: xyzABC, Minutes 120
Fine : 25.0
Minutes: 50
Make:Market,Model:1999,Color: yellow,License Plate: xyzABC, Minutes 120
Officer : Mathew
Badge: 56