The problem
For this assignment you will 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 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 ParkingTicket object) if the car's time has expired
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 hour that the car is illegally parked, plus $l0 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
Write a program that demonstrates how these classes collaborate.
Breaking it down
Parked car
/**
* 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;
//omitted getter/setters
}
ParkingMeter
/**
* This class should simulate a parking ticket, see exercise for description
* of responsibilities
*
*/
class ParkingMeter {
private int minutesPurchased;
//omitted getter/setters
}
PoliceOfficer
/**
* This class should simulate a police officer inspecting parked cars.
*
*/
class PoliceOfficer {
private String name;
private String 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;
}
//omitted getter/setters
}
ParkingTicket
/**
* 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);
}
//omitted getter/setters
}
Main Method
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!");
}
}
Output
ParkingTicket [car=com.levelup.java.exercises.beginner.ParkingTicketSimulator$ParkedCar@7f31245a, officer=com.levelup.java.exercises.beginner.ParkingTicketSimulator$PoliceOfficer@6d6f6e28, fine=45.0, minutes=65, BASE_FINE=25.0, HOURLY_FINE=10.0]
Level Up
- Write a java unit test for each set of logic in PoliceOfficer and Parking ticket class
- Fix program output so it isn't calling the the ParkingTicket toString class
- What are two ways to handle the
if (ticket != null)
, hint check out guava optional and java 8 optional