Reputation: 168
I'm doing Car Rental app in Java.
Car
with Strings RegNo
, producer
, model
and boolean isCarRented
.List of cars I'm keeping in:
Collection<Car> carList = new HashSet<Car>();
Everything works fine.
Now what I need to do is history/statistic module for whole rental company:
history of all car rentals
rental history for each car separately
My idea is to:
Create class CarHistory
with:
private static List<String> rentalDates = new ArrayList<String>();
Keeping there dates which I'm gonna add every time the car is rented.
Create data structure to remember each car rental history like this:
static Map<Car, CarHistory> rentalList = new HashMap<Car, CarHistory>();
Is it a good way? I do have troubles with constructor for single CarHistory
in this solution. Not really sure what it should return. Should I create it after first rental? And should create empty List
rentalDates
for each car to create HashMap
?
Upvotes: 0
Views: 241
Reputation: 2733
What you are trying to do is to implement a one-to-one relationship, because a Car
has only one CarHistory
and one CarHistory
concerns only one Car
. That is why, the correct way of doing it would be to add field CarHistory carHistory
to the class of Car
.
In the beginning, the list of CarHistory
would be empty. With each reservation, you would simply add one record to the list. The car history would be easily accessible, and the model would match the reality in the most accurate way.
Upvotes: 3