Create a Parking Ticket Simulator For this assignment, design a set of classes t
ID: 3681496 • Letter: C
Question
Create a Parking Ticket Simulator
For this assignment, 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 responsibilities 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. The class's only responsibility is as follows:
- To know the number of minutes of parking time that has been purchased.
- The ParkingTicket Class: This class should simulate a parking ticket. The class's responsibilities are as follows:
- 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 illegally parked, plus $10 for every additional hour or part of an hour that the car is illegally 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 as follows:
- To know the police officer's name and badge number
- To examine a ParkedCar object and a ParkingMeter object, and determine whether the car's time has expired
- To issue a parking ticket (generate a Parking Ticket object) if the car's time has expired
Write a program that demonstrates how these classes collaborate.
Explanation / Answer
Test.java
public class Test {
public static void main(String[] args) {
/* Different Parked Time Test Cases */
ParkedCar parkedcar = new ParkedCar("Telsa", "S", "Silver", "PA0108",
90);
/* Different Purchased Parking Time Test Cases */
ParkingMeter parkingmeter = new ParkingMeter(0);
PoliceOfficer policeofficer = new PoliceOfficer("John Edwin",
2009010498, parkedcar, parkingmeter);
policeofficer.examine();
/* Check If There IS A Fine, If Yes Print It out */
if (policeofficer.fine != 0) {
ParkingTicket parkingticket = new ParkingTicket(policeofficer.fine,
parkedcar, parkingmeter, policeofficer);
System.out.println(parkingticket.toString());
System.out.println(parkedcar.toString());
System.out.println(parkingticket.toString2());
}
}
}
ParkedCar.java
public class ParkedCar {
private String make;
private String model;
private String color;
private String license;
public int minutes = 0;
/* Initialize the Info of the Car */
public ParkedCar(String make, String model, String color, String license,
int minutes) {
this.make = make;
this.model = model;
this.color = color;
this.license = license;
this.minutes = minutes;
}
public ParkedCar(ParkedCar parkedcar2) {
make = parkedcar2.make;
model = parkedcar2.model;
color = parkedcar2.color;
license = parkedcar2.license;
minutes = parkedcar2.minutes;
}
public String toString() {
String str = "Parked Car's Make: " + make + " Parked Car's Model: "
+ model + " Parked Car's Color: " + color
+ " Parked Car's License number: " + license
+ " Minutes Parked: " + minutes;
return str;
}
}
ParkingMeter.java
public class ParkingMeter {
public int minutesPurchased = 0;
/* Initialize the Minutes Purchased */
public ParkingMeter(int minutesPurchased) {
this.minutesPurchased = minutesPurchased;
}
public ParkingMeter(ParkingMeter parkingmeter2) {
minutesPurchased = parkingmeter2.minutesPurchased;
}
}
ParkingTicket.java
public class ParkingTicket {
private PoliceOfficer policeofficer;
private ParkedCar parkedcar;
private ParkingMeter parkingmeter;
private double fine;
/* Initialize the Parameters in the Ticket */
public ParkingTicket(double fine, ParkedCar parkedcar,
ParkingMeter parkingmeter, PoliceOfficer policeofficer) {
this.parkedcar = new ParkedCar(parkedcar);
this.parkingmeter = new ParkingMeter(parkingmeter);
this.policeofficer = new PoliceOfficer(policeofficer);
this.fine = fine;
}
/* Content of the Ticket */
public String toString() {
String str = "PARKING VIOLATION " + "Illegally Parked Car Info: ";
return str;
}
public String toString2() {
String str = "Minutes Purchased: " + parkingmeter.minutesPurchased
+ " Amount of the fine: " + fine + " Police officer: "
+ policeofficer.name + " Badge Number: "
+ policeofficer.badgeNumber;
return str;
}
}
PoliceOfficer.java
public class PoliceOfficer {
public String name;
public int badgeNumber;
public double fine = 0.00;
private ParkedCar parkedcar;
private ParkingMeter parkingmeter;
private ParkingTicket parkingticket;
/* Initialize the Info of Police Officer */
public PoliceOfficer(String name, int badgeNumber, ParkedCar parkedcar,
ParkingMeter parkingmeter) {
this.name = name;
this.badgeNumber = badgeNumber;
this.parkedcar = new ParkedCar(parkedcar);
this.parkingmeter = new ParkingMeter(parkingmeter);
}
public PoliceOfficer(PoliceOfficer policeofficer2) {
name = policeofficer2.name;
badgeNumber = policeofficer2.badgeNumber;
}
public void examine() {
if ((parkedcar.minutes - parkingmeter.minutesPurchased) > 0) {
if ((parkedcar.minutes - parkingmeter.minutesPurchased) < 60) {
fine = 25.00;
} else {
fine = 25.00 + 10 * (int) ((parkedcar.minutes - parkingmeter.minutesPurchased) / 60);
}
}
}
}
sample output
PARKING VIOLATION
Illegally Parked Car Info:
Parked Car's Make: Telsa
Parked Car's Model: S
Parked Car's Color: Silver
Parked Car's License number: PA0108
Minutes Parked: 90
Minutes Purchased: 0
Amount of the fine: 35.0
Police officer: John Edwin
Badge Number: 2009010498