Design the following set of classes so that they work together to simulate a pol
ID: 3770392 • Letter: D
Question
Design the following set of classes so that they work together to simulate a police officer issuing a parking ticket.
A- The ParkedCar Class: This class should simulate a parked car. The class’s responsibilities are:
1- To know the car’s make, model, color, license number, and the number of minutes that the car has been parked.
B- The ParkingMeter Class: This class should simulate a parking meter. The class’s only responsibility is:2- To know the number of minutes of parking time that has been purchased.
C- The ParkingTicket Class: This class should simulate a parking ticket. The class’s responsibilities are:3- To report the make, model, color, and license number of the illegally parked car
4-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
5- To report the name and badge number of the police officer issuing the ticket
D- The PoliceOfficer Class: This class should simulate a police officer inspecting parked cars. The class’s responsibilities are:
6-To know the police officer’s name and badge number
7- To examine a ParkedCar object and a ParkingMeter object, and determine whether the car’s time has expired
8- To issue a parking ticket (generate a ParkingTicket object) if the car’s time has expired Write a program that demonstrates how these classes collaborate.
Explanation / Answer
public class ParkingTicketSimulator {
/**
* This class should simulate a parked car, see exercise for description of
* responsibilities
*
*/
class ParkedCar {
private String make;
private String model;
private String color;
private String licenseNumber;
private int minutesParked;
public ParkedCar(String make, String model, String color,
String licenseNumber, int minutesParked) {
super();
this.make = make;
this.model = model;
this.color = color;
this.licenseNumber = licenseNumber;
this.minutesParked = minutesParked;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getLicenseNumber() {
return licenseNumber;
}
public void setLicenseNumber(String licenseNumber) {
this.licenseNumber = licenseNumber;
}
public int getMinutesParked() {
return minutesParked;
}
public void setMinutesParked(int minutesParked) {
this.minutesParked = minutesParked;
}
}
/**
* This class should simulate a parking ticket, see exercise for description
* of responsibilities
*
*/
class ParkingMeter {
private int minutesPurchased;
public ParkingMeter(int minutesPurchased) {
super();
this.minutesPurchased = minutesPurchased;
}
public int getMinutesPurchased() {
return minutesPurchased;
}
public void setMinutesPurchased(int minutesPurchased) {
this.minutesPurchased = minutesPurchased;
}
}
/**
* This class should simulate a police officer inspecting parked cars.
*
*/
class PoliceOfficer {
private String name;
private String badgeNumber;
public PoliceOfficer(String name, String badgeNumber) {
super();
this.name = name;
this.badgeNumber = badgeNumber;
}
public ParkingTicket patrol(ParkedCar car, ParkingMeter meter) {
ParkingTicket ticket = null;
// Calculate the total number of minutes parked over minutes
// purchased
int illegalMinutes = car.getMinutesParked()
- meter.getMinutesPurchased();
// if illegalMinutes, give ticket
if (illegalMinutes > 0) {
// Yes, it is illegally parked.
ticket = new ParkingTicket(car, this, illegalMinutes);
}
return ticket;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBadgeNumber() {
return badgeNumber;
}
public void setBadgeNumber(String badgeNumber) {
this.badgeNumber = badgeNumber;
}
}
/**
* This class should simulate a parking ticket.
*
*/
class ParkingTicket {
private ParkedCar car;
private PoliceOfficer officer;
private double fine;
private int minutes;
public final double BASE_FINE = 25.0;
public final double HOURLY_FINE = 10.0;
public ParkingTicket(ParkedCar car, PoliceOfficer officer, int minutes) {
super();
this.car = car;
this.officer = officer;
this.minutes = minutes;
calculateFine();
}
private void calculateFine() {
double hours = minutes / 60.0;
int hoursAsInt = (int) hours;
if ((hours - hoursAsInt) > 0) {
hoursAsInt++;
}
// Assign the base fine.
fine = BASE_FINE;
// Add the additional hourly fines.
fine += (hoursAsInt * HOURLY_FINE);
}
public ParkedCar getCar() {
return car;
}
public void setCar(ParkedCar car) {
this.car = car;
}
public PoliceOfficer getOfficer() {
return officer;
}
public void setOfficer(PoliceOfficer officer) {
this.officer = officer;
}
public double getFine() {
return fine;
}
public void setFine(double fine) {
this.fine = fine;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
@Override
public String toString() {
return "ParkingTicket [car=" + car + ", officer=" + officer
+ ", fine=" + fine + ", minutes=" + minutes
+ ", BASE_FINE=" + BASE_FINE + ", HOURLY_FINE="
+ HOURLY_FINE + "]";
}
}
public static void main(String[] args) {
// A green car was parked for 125 minutes
ParkingTicketSimulator parkingTicketSimulator = new ParkingTicketSimulator();
ParkedCar car = parkingTicketSimulator.new ParkedCar("Toyota", "2005",
"Green", "ABC123", 125);
// 60 minutes of time was purchased
ParkingMeter meter = parkingTicketSimulator.new ParkingMeter(60);
// Officer Jack was on duty
PoliceOfficer officer = parkingTicketSimulator.new PoliceOfficer(
"Sargent Jack Johnson", "8909");
ParkingTicket ticket = officer.patrol(car, meter);
// Did the officer issue a ticket?
if (ticket != null) {
System.out.println(ticket);
} else {
System.out.println("No crimes committed!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.