My task was to create a list of flights using ArrayList. I've created a Main class, a Flight class and FlightDatabase class,and in FlightDatabase I've added a few new Flights with departure and array Strings in ().
I was supposed to create a method in this class which checks if a searched flight exists. The whole goal was to practise the usage of .equals method with Strings. The problem is, I don't know how to encode this all together, my idea is to create a method which takes the String variables from Flight class constructor and compare them with the Strings added in ArrayList of Flights in FlightDatabase class. No idea how to achieve that.
Here's what I did already. Doesn't work the way i wanted, i even know why, but i have no idea how to make it work.
import java.util.ArrayList;
public class FlightDatabase {
ArrayList<Flight> flights = new ArrayList<Flight> ();
public FlightDatabase ( ) {
this.flights.add ( new Flight ( "Wroclaw", "Berlin" ) );
this.flights.add ( new Flight ( "Warsaw", "Poznan" ) );
this.flights.add ( new Flight ( "Krakow", "Vienna" ) );
this.flights.add ( new Flight ( "Wroclaw", "Rotterdam" ) );
this.flights.add ( new Flight ( "Warsaw", "Moscow" ) );
this.flights.add ( new Flight ( "Wroclaw", "Frankfurt" ) );
this.flights.add ( new Flight ( "Warsaw", "London" ) );
this.flights.add ( new Flight ( "Krakow", "Paris" ) );
}
public void checkIfFlightExists (String departure, String arrival ) {
for(int i = 0; i < flights.size (); i++) {
Flight check = flights.get ( i );
if (departure.equals ( check ) && arrival.equals ( check )){
System.out.println ("there is such flight");
}else {
System.out.println ("No such flight sorry");
}
}
}
}