ParkingPlace
and ParkingLot
We want to realize a program for the administration of a parking lot formed by a collection of parking places. For each parking place the following information has to be stored:
Part 1
Write a class ParkingPlace
, for handling a parking place, that
implements the following methods:
ParkingPlace()
: constructor without parameters that
constructs a parking place that is initially free;
String toString()
: that returns "-------"
if the parking place is free, and the licence plate of the car, if the
parking place is occupied;
void carArrives(String plate, int hour, int minutes)
:
modifies the state of the parking place by setting it to occupied, sets
the plate of the car that occupies the place to plate
, and
sets the time since when it is occupied to hour
and
minutes
; if the parking place is already occupied, the
method does nothing;
void carLeaves()
: modifies the state of the parking place
by setting it to free;
boolean free()
: returns true
if the parking
place is free, false
otherwise;
String getCar()
: returns the plate of the car that occupies
the parking place, if the place is occupied, null
otherwise;
int getHour()
: returns the hour since when the parking
place is occupied, if the place is occupied, -1
otherwise;
int getMinutes()
: returns the minutes since when the
parking place is occupied, if the place is occupied,
-1
otherwise;
Use the example program
TestParkingPlace.Java
to test the class you have developed.
Part 2
Write a class UseParkingPlace
that contains various public static
methods that are clients of ParkingPlace
. In the description of
all methods below, we assume that a parking lot is always represented as an
array of parking places. The class should contain the following methods:
static int firstFreePlace(ParkingPlace[] p)
: that, given a
parking lot p
, returns the index of the first free parking
place in p
;
static int countFreePlaces(ParkingPlace[] p)
: that, given a
parking lot p
, returns the number of free parking places in
p
;
static int[] freePlaces(ParkingPlace[] p)
: that, given
a parking lot p
, returns an array of integers containing the
indices of all free parking places in p
;
static void carEnters(ParkingPlace[] p, String a, int hour, int
minutes)
: that, given a parking lot p
, modifies the
array p
by inserting the car a
(represented by
its plate) in the first free parking place available in p
,
assigning hour
and minutes
as arrival time; if
there is no free parking place in p
, the method does
nothing;
static void carExits(ParkingPlace[] p, String a)
: that,
given a parking lot p
, modifies the array p
by
freeing the parking place where the car a
is parked; if the
car a
is not present in the parking lot, the method does
nothing;
static int longestParkedCar(ParkingPlace[] p)
: that, given
a parking lot p
, returns the index of the parking place in
which the longest parked car is present; if there is more than one such
car, the method should return one of the indexes (chosen arbitrarily); if
there is no car parked in the parking lot, the method should return
-1
;
static String[] allParkedCars(ParkingPlace[] p)
: that,
given a parking lot p
, returns an array of strings that
represent the plates of all cars present in p
;
static ParkingPlace[] readParkingLotFromFile(String
filename)
: that, given the name of a file containing
parkingPlace records, constructs an array of
ParkingPlace
that represents the parking lot having exactly
the parking places stored on the file. Each parkingPlace record
has the following form:
free license-plate hour minuteswhere
free
is "true"
or
"false"
,
license-plate
is the license plate of the car
if the parking place is occupied, the empty string otherwise,
hour
and minutes
are
two integers that represent the arrival time of the car, if the
parking place is occupied, the empty string otherwise;
static void writeParkingLotToFile(ParkingPlace[] p, String
filename)
: that, given a parking lot p
and the name
of a file, writes on the file the parkingPlace records (see above)
corresponding to the entire parking lot.
Part 3
Realize a class ParkingLot
to represent parking lots, and whose
objects support the same functionalities as those implemented in
UseParkingPlace
through static methods.
The class should export also a constructor that takes as parameter a positive
integer n
and constructs a parking lot with n
parking
places that initially are all empty.
Notice that the method readParkingLotFromFile
, that takes as
parameter a string representing a filename containing parkingPlace
records (see above), and constructs a parking lot having exactly the parking
places stored on the file, should be implemented as a static method that
creates and returns an object of the class ParkingLot
.