Package crms.lib
Class CarDatabase
The
CarDatabase
class manages the collection of recorded Car
objects in the local database and performs various operations related to
them.
It implements the singleton pattern to ensure only one instance of the class exists.
It provides methods to add, remove, and retrieve Car
objects,
generate reports, and handle data persistence to a local file.
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Fetches the recordedCar
objects from the filecars.txt
.static CarDatabase
Returns the singleton instance ofCarDatabase
.getItemById
(String id) Retrieves aCar
object using the given ID.void
Saves the list ofCar
objects to the filecars.txt
.
-
Method Details
-
getInstance
Returns the singleton instance ofCarDatabase
. If the instance does not exist, it is created and the cars are fetched from disk.- Returns:
- the singleton instance of
CarDatabase
.
-
getItemById
Retrieves aCar
object using the given ID.- Specified by:
getItemById
in classDatabase<Car>
- Parameters:
id
- the ID of the car to be retrieved.- Returns:
- the
Car
object with the specified ID, ornull
if not found.
-
fetchFromDisk
public void fetchFromDisk()Fetches the recordedCar
objects from the filecars.txt
.- Specified by:
fetchFromDisk
in classDatabase<Car>
-
saveToDisk
public void saveToDisk()Saves the list ofCar
objects to the filecars.txt
.- Specified by:
saveToDisk
in classDatabase<Car>
-