Search code examples
javalocaldate

Why the compare between two LocalDate variables doesn't work?


I've created an ArrayList of type Flight in Main class.Also Flight is a class that contains LocalDate variable , getters and setters. In main I created a boolean flag theDay and setted to true. I want this flag to change in false when the For loop find a LocalDate day value as the same before. For example:

1st Try: I putted the value 3 into scanner.nextInt(); It printed 2019-5-3 and "Didn't found a same date."

2nd Try: I putted the value 6 into scanner.nextInt(); It printed 2019-5-6 and "Didn't found a same date."

3rd Try: I putted the value 3 again into scanner.nextInt(); It printed 2019-5-3 and "Didn't found a same date." I was expected to get a message of "A same date found".

public static void main(String[] args) {
    ArrayList<Flight> flightList = new ArrayList<Flight>();
    Scanner scanner = new Scanner(System.in);

    int counter=1;
    while (counter <= 3) {
        Flight flight = new Flight();

        System.out.println("Give the day of the departure.");

        LocalDate day = LocalDate.of(2019, Month.MAY, scanner.nextInt());
        flight.setDateOfDeparture(day);

        System.out.println(flight.getDateOfDeparture());

        boolean theDay = true; //Flag (reversed way in order to achieve the TRUE logic value).
        for (Flight flight1 : flightList) {
            System.out.println(flight1.getDateOfDeparture());

            if (flight1.getDateOfDeparture().compareTo(flight.getDateOfDeparture()) == 0) {
                theDay = false;
            }
        }

        counter++;

        if (theDay){
            System.out.println("Didn't found a same day.");
        }else
            System.out.println("A same date found");
    }
}

Solution

  • You never add any instance in your list. Based on your expectations :

    • 1st Try: I putted the value 3 into scanner.nextInt(); It printed 2019-5-3 and "Didn't found a same date."
    • 2nd Try: I putted the value 6 into scanner.nextInt(); It printed 2019-5-6 and "Didn't found a same date."
    • 3rd Try: I putted the value 3 again into scanner.nextInt(); It printed 2019-5-3 and "Didn't found a same date." I was expected to get a message of "A same date found".

    What you need is to insert a flight when the theDay is true, when there is no departure date already matching.

    if (theDay){
        flightList.add(flight);
        System.out.println("Didn't found a same day.");
    }else{
        System.out.println("A same date found");
    }
    

    When you are ready to move forward, you could use a Set<Flight> with a correct equivalence implementation

    You will never need to check yourself if there is already an "equivalent" instance, the Set will do it for you.

    All you need is to implement equals and hashCode correctly and your code will simply look like :

    Set<Flight> flights = new HashSet<>();
    
    Scanner sc = new Scanner(System.in);
    for(int i = 0; i < 5; ++i){
        Flight f = new Flight();
        f.setDeparture(LocalDate.of(2019, Month.MAY, sc.nextInt()));
    
        if(flights.add(f)){
            System.out.println("New flight added");
        } else {
            System.out.println("Flight already booked");
        }
    }
    
    sc.close();
    

    And to give you an idea, here is the generated methods by eclipse for a simple class

    class Flight {
        LocalDate departure;
    
        public void setDeparture(LocalDate departure) {
            this.departure = departure;
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result
                    + ((departure == null) ? 0 : departure.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Flight other = (Flight) obj;
            if (departure == null) {
                if (other.departure != null)
                    return false;
            } else if (!departure.equals(other.departure))
                return false;
            return true;
        }
    }