--in java don\'t copy-- Design a set of classes that, work together to simulate
ID: 3574712 • Letter: #
Question
--in java don't copy--
Design a set of classes that, work together to simulate a police officer issuing a parking ticket. You should design the following, classes:
1. the ParkedCar class: this class should simulate a parked car. The class's responsibility are as follows:
-To know the car's make, model, color, license number, and the number of minutes that the car has been parked
2. The ParkingMeter class: This class should a parking meter.. The class's only responsibility is as follows:
-To know the number of minutes of parking time that has been purchased
3. 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 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 office issuing the ticket
4. The PoliceOfficer class: This class should simulate a police officer inspecting parked cars. The class's responsibility 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 a car's time has expired
-To issue a parking ticket(generate a ParkingTicket object) if the car's time has expired
Also, Write a program that demonstrate these classes collaborate.
Explanation / Answer
public class ParkingMeter {
int parkingTimePurchased;
/**
* @param parkingTimePurchased
*/
ParkingMeter(int parkingTimePurchased) {
setParkingTimePurchased(parkingTimePurchased);
}
/**
* @return the parkingTimePurchased
*/
public int getParkingTimePurchased() {
return parkingTimePurchased;
}
/**
* @param parkingTimePurchased
* the parkingTimePurchased to set
*/
public void setParkingTimePurchased(int parkingTimePurchased) {
this.parkingTimePurchased = parkingTimePurchased;
}
}
public class ParkingTicket {
ParkedCar parkedCar;
ParkingMeter parkingMeter;
PoliceOfficer policeOfficer;
/**
* @param policeOfficer
* @param parkedCar
* @param parkingMeter
*/
ParkingTicket(PoliceOfficer policeOfficer, ParkedCar parkedCar,
ParkingMeter parkingMeter) {
this.policeOfficer = policeOfficer;
this.parkedCar = parkedCar;
this.parkingMeter = parkingMeter;
}
public String getColor() {
return parkedCar.getColor();
}
public Double getFine() {
int illegalMinutes = parkedCar.getMinutesParked()
- parkingMeter.getParkingTimePurchased();
double fine = 0;
if (illegalMinutes >= 60) {
fine += 25.00;
illegalMinutes -= 60;
}
while (illegalMinutes > 0) {
if (illegalMinutes >= 60) {
fine += 10.00;
illegalMinutes -= 60;
}
else {
fine += 10.00;
illegalMinutes -= illegalMinutes;
}
}
return fine;
}
public String getLicense() {
return parkedCar.getLicenseNumber();
}
public String getMake() {
return parkedCar.getMake();
}
public String getModel() {
return parkedCar.getModel();
}
public String getOfficerBadgeNumber() {
return policeOfficer.getBadgeNumber();
}
public String getOfficerName() {
return policeOfficer.getName();
}
}
public class PoliceOfficer {
private String name, badgeNumber;
/**
* @param name
* @param badgeNumber
*/
PoliceOfficer(String name, String badgeNumber) {
this.name = name;
this.badgeNumber = badgeNumber;
}
/**
* @param parkedCar
* @param parkingMeter
*/
public void examineParkedCar(ParkedCar parkedCar, ParkingMeter parkingMeter) {
if (parkingMeter.getParkingTimePurchased() < parkedCar
.getMinutesParked()) {
ParkingTicket parkingTicket = new ParkingTicket(this, parkedCar,
parkingMeter);
System.out.println(parkedCar.getModel()
+ " is illegally parked. Ticket issued.");
System.out.println(" Parking ticket:");
System.out.println("Issuing officer's name: "
+ parkingTicket.getOfficerName());
System.out.println("Issuing officer's badge number: "
+ parkingTicket.getOfficerBadgeNumber());
System.out.println("Fine: $" + parkingTicket.getFine());
}
else {
System.out.println(parkedCar.getModel() + " is legally parked.");
}
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the badgeNumber
*/
public String getBadgeNumber() {
return badgeNumber;
}
}
public class ParkingTicketSimulator {
/**
* @param arsg
*/
public static void main(String[] arsg) {
PoliceOfficer andy = new PoliceOfficer("Srinivas", "BD222");
ParkedCar accord = new ParkedCar("Maruthi", "Swift", "White",
"LICE332", 211);
ParkingMeter meter = new ParkingMeter(40);
andy.examineParkedCar(accord, meter);
}
}
OUTPUT:
Swift is illegally parked. Ticket issued.
Parking ticket:
Issuing officer's name: Srinivas
Issuing officer's badge number: BD222
Fine: $45.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.