We want to realize a program to be used in a car rental office. For each car, the following information is stored: plate number (a string), category (a character), name of person who rented the car (a string), and number of days for which the car is rented (a non-negative integer). If the number of days is 0, this means that the car is not rented.
Part 1.
Write a class Car
that implements the following methods:
Car(String pn, char c)
: costructor that, given plate
number and category, constructs a car that is initially not rented;
String getPlate()
: that returns the plate number of the
car;
char getCategory()
: that returns the category of the car;
int getDays()
: that returns the number of days for which
the car is rented;
String getPerson()
: that returns the name of the person
who rented the car, if the car is rented, and null
otherwise;
void rentCar(int d, String n)
: that rents the car to person
with name n
for d
days, if it is not already
rented, and does nothing otherwise;
void giveBackCar()
: that sets the status of the car to
being not rented (this means also that there is no person who rents the
car);
String toString()
: that returns a string with all the
information about the car.
Use the example program
TestCar.Java
to test the class you have developed.
Part 2.
Write a class UseCar
, client of Car
, that contains
the following public static method:
static String longestRental(Car c1, Car c2)
: that, given two
cars c1
and c2
, returns the name of the person
who rented for the longest period the car among c1
and
c2
. If both c1
and c2
are rented
for the same number of days, the method should return the name of the
person who rented c1
. If both c1
and
c2
are not rented, the method should return
null
.
Use the example program
TestUseCar.java
to test the method you have developed.
Solutions:
Car.java
,
UseCar.java