In: Computer Science
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 classes' responsibilities are as the following:
-to know the make, the model, 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 simulated a parking ticket. The class's responsibilities are as follows:
-to report the make, model, and the license number of the illegally parked car
-to report the amount of the fine, which is $175 for the first hour and the part of an hour that the car illegally parked, plus $25 for every additional hour or part of the hour.
-to report the name and the badge number of the police officer issuing the ticket.
The policeOfficer Class: the class should simulate the officer inspecting parked cars. the class's responsibilities as follow:
-to know the police officers' name and badge number
-to examine a parkedCar object and 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 java program that demonstrate how these classes collaborate.
class ParkedCar
{
public String make;
public String model;
public String license_number;
public int num_of_minutes_parking;
ParkedCar(String make, String model, String license_number, int time){
this.make = make;
this.model = model;
this.license_number = license_number;
this.num_of_minutes_parking = time;
}
public void setTime(int time){
this.num_of_minutes_parking = time;
}
}
class ParkingMeter
{
public int parking_time_bought;
ParkingMeter(int time){
this.parking_time_bought = time;
}
}
class ParkingTicket
{
public ParkedCar car_instance;
public ParkingMeter ticket_instance;
public String police_name;
public String badge_number;
ParkingTicket(ParkedCar car_instance, ParkingMeter ticket_instance, String police_name, String badge_number)
{
this.car_instance = car_instance;
this.ticket_instance = ticket_instance;
this.police_name = police_name;
this.badge_number = badge_number;
}
public void reportFine()
{
float fine = 0;
float extra_time = car_instance.num_of_minutes_parking - ticket_instance.parking_time_bought;
//System.out.println(extra_time);
if(extra_time <= 60) fine = 175*((float)extra_time/60);
else fine = 175 + 25*(float)(extra_time - 60)/60;
System.out.println("Fine: Rs. "+( (float)Math.round(fine*100) / 100));
}
public void reportCar()
{
System.out.println("Car Details\nMake: "+car_instance.make+"\nModel: "+car_instance.model+"\nLicense Number: "+car_instance.license_number);
}
public void reportPoliceOfficer()
{
System.out.println("Police Officer Details\nName: "+police_name+"\nBadge Number: "+badge_number);
}
}
class PoliceOfficer
{
public String name;
public String badge_number;
private ParkingTicket newTicket;
PoliceOfficer(String name, String badge_number)
{
this.name = name;
this.badge_number = badge_number;
}
public boolean examine(ParkedCar car_instance, ParkingMeter ticket_instance)
{
if(ticket_instance.parking_time_bought < car_instance.num_of_minutes_parking) return true;
return false;
}
public void issueTicket(ParkedCar car_instance, ParkingMeter ticket_instance)
{
this.newTicket = new ParkingTicket(car_instance, ticket_instance, this.name, this.badge_number);
System.out.println("Parking Ticket Issued: ");
this.newTicket.reportFine();
this.newTicket.reportCar();
this.newTicket.reportPoliceOfficer();
}
}
public class SampleTest
{
public static void main(String [] args)
{
ParkedCar car_instance = new ParkedCar("Tesla", "Model S", "17BAN0135", 75);
ParkingMeter ticket_instance = new ParkingMeter(50);
PoliceOfficer po = new PoliceOfficer("Sgt. Adams K", "S14");
if(po.examine(car_instance, ticket_instance)) po.issueTicket(car_instance, ticket_instance);
}
}