In: Computer Science
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 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 ParkingTicket object)
if the car's time has
expired
Write a program that demonstrates how these classes collaborate.
Make the aforementioned classes nested classes within the class
containing your main method; only this outermost class can be
declared public.
Sample Run
java ParkingTicketSimulator
===·Parking·Ticket·Simulator·===↵
↵
---------↵
Car·Data↵
---------↵
↵
·Enter·car·make:Bugatti↵
·Enter·car·model:Veyron↵
·Enter·car·color:Black↵
·Enter·car·license·number:ESC-1532↵
·Enter·minutes·car·has·been·parked:450↵
·↵
----------↵
Meter·Data↵
----------↵
↵ Enter·minutes·purchased·by·driver:60↵
·↵
-------↵
PO·Data↵
-------↵
↵
Enter·police·officer's·name:Robert·Smith↵
·Enter·police·officer's·badge·number:230475↵
·↵ ---------------------↵
Parking·Ticket·Issued↵
---------------------↵
↵
|·Parking·ticket·#:·.·.·.↵
|·Fined·amount:·$75.00↵
|·Car·issued·to:·Black·Bugatti·Veyron,·license·#:·ESC-1532↵
|·Issued·by·officer:·Robert·Smith,·badge·#:·230475↵
[Note the fine output in the question doesn't match with the question as if we divide 450 by 60 we will get 7.5 hrs so for the first-hour car will be charged $25 and the rest 6.5 hrs car will be charged $70 as part of the hour is also considered so the all total fine will be $95]
if you have any question regarding the program feel free to comment
Program
package parkingTicketSimulation;
import java.util.Scanner;
public class ParkingTicketSimulation {
//nested classes
static class ParkedCar {
private String make, model, color,
license;
private int noOfMin;
public ParkedCar(String make,
String model, String color, String license, int noOfMin) {
this.make =
make;
this.model =
model;
this.color =
color;
this.license =
license;
this.noOfMin =
noOfMin;
}
public int getNoOfMin() {
return
noOfMin;
}
@Override
public String toString() {
return color+"
"+make+" "+model+", License #: "+license;
}
}
static class ParkingMeter {
private int purchasedParkTime;
public ParkingMeter(int
purchasedParkTime) {
this.purchasedParkTime = purchasedParkTime;
}
public int
getPurchasedParkTime() {
return
purchasedParkTime;
}
}
static class PoliceOfficer {
private String name;
private int badgeNo;
public PoliceOfficer(String name,
int badgeNo) {
this.name =
name;
this.badgeNo =
badgeNo;
}
public void examin(ParkedCar car,
ParkingMeter meter) {
//when car min
exceeds its purachse min officer will issue ticket
if(car.getNoOfMin() > meter.getPurchasedParkTime()) {
System.out.println("Parking ticket
issued");
ParkingTicket.report(car, meter, this);
}
else {
System.out.println("No parking ticket issued for
");
System.out.println(car);
}
}
@Override
public String toString() {
return name+",
badge#: "+badgeNo;
}
}
static class ParkingTicket {
private static int
ticketNo=0;
public static void report(ParkedCar
car, ParkingMeter meter, PoliceOfficer officer) {
ticketNo++;//will increment with every issue
int fine =
0;
int noOfHours =
car.getNoOfMin() / meter.getPurchasedParkTime();
int
noOfRemainHours = car.getNoOfMin() %
meter.getPurchasedParkTime();
//part of first
hour fine
if(noOfRemainHours != 0 && noOfHours<1) {
fine+=25;
}
else {
if(noOfHours>=1) {//first hour fine plus
additional hour
fine+=25 +
((noOfHours-1)*10);
}
if(noOfRemainHours != 0)//part of additional
hour
fine+=10;
}
System.out.println("Parking ticket#: "+ticketNo);
System.out.println("Fined amount: $"+fine);
System.out.println("Car issued to: "+car);
System.out.println("Issued by officer: "+officer);
}
}
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
String make, model, color, license,
name;
int noOfMin, purchasedParkTime,
badgeNo;
System.out.println("Car
Data");
System.out.print("Enter car make:
"); make = sc.nextLine();
System.out.print("Enter car model:
"); model = sc.nextLine();
System.out.print("Enter car color:
"); color = sc.nextLine();
System.out.print("Enter car license
number: "); license = sc.nextLine();
System.out.print("Enter minutes car
has been parked: "); noOfMin = sc.nextInt();
sc.nextLine();
System.out.println("\nMeter
Data");
System.out.print("Enter minutes
purchased by driver: ");purchasedParkTime= sc.nextInt();
sc.nextLine();
System.out.println("\nPO
Data");
System.out.print("Enter police
officer's name: "); name = sc.nextLine();
System.out.print("Enter police
officer's badge number: "); badgeNo = sc.nextInt();
sc.nextLine();
System.out.println();
ParkingTicketSimulation.ParkedCar
car = new ParkedCar(make, model, color, license, noOfMin);
ParkingTicketSimulation.ParkingMeter meter = new
ParkingMeter(purchasedParkTime);
ParkingTicketSimulation.PoliceOfficer officer = new
PoliceOfficer(name, badgeNo);
officer.examin(car, meter);
sc.close();
}
}
Output