K
RecommendationsWe start with the recommendations since structuring your project and your code will help you facilitate work, neglect, this you will learn with the experience.To facilitate management I would recommend you to learn and use data structures. Among them are the Maps of the https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html which help associate keys with values; you can then associate for example, your car to a parking place. And as they explain in the link, a map cannot have duplicate keys, which is convenient in this case.An example of how a map would look is as follows:Parking spaceCar parked7Cart 25Carrio 38Carrio 7In this case the parking place would be the key and value the car parked.We see also that https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html has very useful methods for what you want to achieve: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html#putIfAbsent(K,V) : If the key does not exist, then place the key-value association https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html#remove(java.lang.Object) : Eliminates if the key-value ratio exists https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html#get(java.lang.Object) : Returns the value associated with the keyFor hours or dates you always use a type of Time or Date dataJava has class https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalTime.html which allows us to record an instant of the day (hour, minute, second, ms).There are also other classes like LocalDate and LocalDateTimewhich serve to record dates (year, month day), (year, month, day, time, minute, second, ms) respectively. And obviously they're better to manipulate time than the whole.With that we can create a stronger model than you need:Using a https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/HashMap.html for a class "parking"Class Carropublic class Car {
private final String license_plate;
private final LocalTime check_in;
public Car(String license_plate, LocalTime check_in) {
this.license_plate = license_plate;
this.check_in = check_in;
}
public String getLicense_plate() {
return license_plate;
}
public LocalTime getCheck_in() {
return check_in;
}
@Override
public String toString() {
return "license_plate: '" + license_plate + "'\t" + "check_in: " + check_in;
}
}
Class parkingpublic class ParkingLot {
private final HashMap<Integer, Car> cars = new HashMap<>();
/**
* If the place is available, the car successfully parks
* and registers data
* @param car car to park
* @param parking_place parking place
* @return the Car if successfully parked, null otherwise
*/
protected Car parkCar(Car car, Integer parking_place) {
return cars.putIfAbsent(parking_place, car);
}
/**
* Removes from the Car from the HashMap
* @param parking_place parking place to remove
*/
protected void clearParkingLot(Integer parking_place) {
cars.remove(parking_place);
}
/**
* @param parking_place parking place
* @return the car parked in the parking place specified
*/
protected Car getCarFromParkingLot(Integer parking_place) {
return cars.get(parking_place);
}
/**
* Retrieves the parking place from the Car with the SpecifiedId
* @param license_plate license plates
* @return null if the car doesn't exist, the parking place value otherwise
*/
protected Integer getParkingPlaceFromCarId(String license_plate) {
// for each entry compares if the license plate is equal
for (Map.Entry<Integer, Car> element : cars.entrySet()) {
if (element.getValue().getLicense_plate().equalsIgnoreCase(license_plate))
return element.getKey();
}
return null;
}
/**
* Retrieves the parking place from the Car with the SpecifiedId
* @param car Car to search
* @return null if the car doesn't exist, the parking place value otherwise
*/
protected Integer getParkingPlaceFromCar(Car car) {
return getParkingPlaceFromCarId(car.getLicense_plate());
}
}
Your problem.How can we get the car longer parked?We can do it in two ways:Long: For each value in the HashMap access the time you entered the parking lot and if it is the most antique, put that car as the minimum and at the end return the minimum.Automated: Implementing a Comparison to the Class Car (checks the https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html and find the minimum with the method https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collections.html#min(java.util.Collection) As we are interested in making it as simple as possible, then we can implement the interface https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Comparable.html (implements Comparable<Car>)public class Car implements Comparable<Car>{
private final String license_plate;
private final LocalTime check_in;
...
...
public int compareTo(@NotNull Car car) {
return check_in.compareTo(car.getCheck_in());
}
}
The method is reduced to something as simple as:public class ParkingLot {
private final HashMap<Integer, Car> cars = new HashMap<>();
...
...
/**
* Returns the car with longer time parked
* @return Car with the min check in time
*/
protected Car getCarWithLongerParkedTime() {
return Collections.min(cars.values());
}
}
Final codeimport org.jetbrains.annotations.NotNull;
import java.time.LocalTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class StackOverflow {
public static void main(String[] args) {
ParkingLot parkingLot = new ParkingLot();
parkingLot.parkCar(new Car("A3E567", LocalTime.of(14, 12)), 1);
parkingLot.parkCar(new Car("ER56TM", LocalTime.of(11,0, 33)), 2);
parkingLot.parkCar(new Car("J0FD7L", LocalTime.of(12, 15, 12)), 3);
System.out.println(parkingLot.getCarWithLongerParkedTime());
}
private static class ParkingLot {
private final HashMap<Integer, Car> cars = new HashMap<>();
/**
* If the place is available, the car successfully parks
* and registers data
* @param car car to park
* @param parking_place parking place
* @return the Car if successfully parked, null otherwise
*/
protected Car parkCar(Car car, Integer parking_place) {
return cars.putIfAbsent(parking_place, car);
}
/**
* Removes from the Car from the HashMap
* @param parking_place parking place to remove
*/
protected void clearParkingLot(Integer parking_place) {
cars.remove(parking_place);
}
/**
* @param parking_place parking place
* @return the car parked in the parking place specified
*/
protected Car getCarFromParkingLot(Integer parking_place) {
return cars.get(parking_place);
}
/**
* Retrieves the parking place from the Car with the SpecifiedId
* @param license_plate license plates
* @return null if the car doesn't exist, the parking place value otherwise
*/
protected Integer getParkingPlaceFromCarId(String license_plate) {
for (Map.Entry<Integer, Car> element : cars.entrySet()) {
if (element.getValue().getLicense_plate().equalsIgnoreCase(license_plate))
return element.getKey();
}
return null;
}
/**
* Retrieves the parking place from the Car with the SpecifiedId
* @param car Car to search
* @return null if the car doesn't exist, the parking place value otherwise
*/
protected Integer getParkingPlaceFromCar(Car car) {
return getParkingPlaceFromCarId(car.getLicense_plate());
}
/**
* Returns the car with longer time parked
* @return Car with the min check in time
*/
protected Car getCarWithLongerParkedTime() {
return Collections.min(cars.values());
}
}
private static class Car implements Comparable<Car>{
private final String license_plate;
private final LocalTime check_in;
public Car(String license_plate, LocalTime check_in) {
this.license_plate = license_plate;
this.check_in = check_in;
}
public String getLicense_plate() {
return license_plate;
}
public LocalTime getCheck_in() {
return check_in;
}
public int compareTo(@NotNull Car car) {
return check_in.compareTo(car.getCheck_in());
}
@Override
public String toString() {
return "license_plate: '" + license_plate + "'\t" + "check_in: " + check_in;
}
}
}
Input// LocalTime.of(hora, minuto, segundo, ms)
parkingLot.parkCar(new Car("A3E567", LocalTime.of(14, 12)), 1);
parkingLot.parkCar(new Car("ER56TM", LocalTime.of(11,0, 33)), 2);
parkingLot.parkCar(new Car("J0FD7L", LocalTime.of(12, 15, 12)), 3);
Output// El carro con mayor tiempo estacionado
license_plate: 'ER56TM' check_in: 11:00:33