OOP null value for date or a random date in super constructor

113 Views Asked by At

I'm working with a class in Java that implements the functionality of a ticket:

public class Ticket {
    private LocalDateTime dateAndTime;
    private String departureCity;
    private String arrivalCity;

    public Ticket(LocalDateTime dateAndTime, String departureCity, String arrivalCity) {
        assert (isDateAndTimeCorrect(dateAndTime));
        assert (isRouteCorrect(departureCity, arrivalCity));
        /** ... **/
    }

    public double getPrice() { /** ... **/ }

    @Override
    public String toString() { /** ... **/ }

    public String getDepartureCity() { /** ... **/ }

    public String getArrivalCity() { /** ... **/ }

    public LocalDateTime getDateAndTime() { /** ... **/ }

    public boolean isDateAndTimeCorrect(LocalDateTime dateAndTime) {
        return dateAndTime != null && dateAndTime.isAfter(LocalDateTime.now());
    }

    public boolean isRouteCorrect(String departureCity, String arrivalCity) {
        return departureCity != null && arrivalCity != null && !departureCity.isEmpty() && !arrivalCity.isEmpty()
                && !departureCity.equals(arrivalCity);
    }
}

As you can see, this is intended to represent a normal Ticket. But, now I want to add to the system a more specific type of ticket called OpenTicket, which allows an undetermined date and time for the ticket without modifying the original Ticket class. I can’t modify the Ticket class because I don’t own it.

According to the principles of Object Oriented Programming, which is the best alternative to proceed? Calling the Ticket constructor from a child class with a future date than .now()? Or calling super(...) with null value in the date attribute and somehow manage to avoid the assertion?

0

There are 0 best solutions below