Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Flight Cities problem

uriahheep Apr 6th, 2020 (edited) 197 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2.  
  3. public class Main {
  4. /* So the thing is I'm supposed to create a new method for
  5. FlightDatabase class which will return (not print) the list
  6. of all cities included in FlightDatabase constructor, without
  7. duplicates. I got a hint to use reversed boolean (!=), and
  8. Arraylist.contains (object o) method.
  9. On the bottom of this paste is what I came up with, doesn't work,
  10. IntelliJ's saying that this method's return is never used.*/
  11.  
  12.     public static void main ( String[] args ) {
  13.  
  14.         FlightDatabase flightDatabase = new FlightDatabase ();{
  15. //            flightDatabase.checkIfFlightExists ("Wroclaw", "Berlin");
  16. //            flightDatabase.displayFlightsFromCity ( "Warsaw" );
  17. //            flightDatabase.displayFlightsToCity ( "Rotterdam" );
  18.               flightDatabase.getCities ();
  19.  
  20.  
  21.         }
  22.     }
  23. }
  24. public class Flight{
  25.  
  26.     String departure;
  27.     String arrival;
  28.  
  29.     public Flight (String departure, String arrival){
  30.         this.departure = departure;
  31.         this.arrival = arrival;
  32.     }
  33.     public String getInfo(){
  34.  
  35.         return "\n\nFlight from: " + this.departure + "\n\nFlight to: " + this.arrival;
  36.     }
  37.  
  38. }
  39. import java.util.ArrayList;
  40.  
  41. public class FlightDatabase {
  42.     ArrayList<Flight> flights = new ArrayList<Flight> ();
  43.  
  44.     public FlightDatabase ( ) {
  45.         this.flights.add ( new Flight ( "Wroclaw", "Berlin" ) );
  46.         this.flights.add ( new Flight ( "Warsaw", "Poznan" ) );
  47.         this.flights.add ( new Flight ( "Krakow", "Vienna" ) );
  48.         this.flights.add ( new Flight ( "Wroclaw", "Rotterdam" ) );
  49.         this.flights.add ( new Flight ( "Warsaw", "Moscow" ) );
  50.         this.flights.add ( new Flight ( "Wroclaw", "Frankfurt" ) );
  51.         this.flights.add ( new Flight ( "Warsaw", "London" ) );
  52.         this.flights.add ( new Flight ( "Krakow", "Paris" ) );
  53.     }
  54.  
  55.     public void checkIfFlightExists ( String departure, String arrival ) {
  56.         for (int i = 0; i < flights.size (); i++) {
  57.             Flight check = flights.get ( i );
  58.             if (departure.equals ( check.departure ) && arrival.equals ( check.arrival )) {
  59.                 System.out.println ( "there is such flight" );
  60.                 return;
  61.             }
  62.         }
  63.         System.out.println ( "No such flight sorry" );
  64.     }
  65.  
  66.     public ArrayList<Flight> getFlightsFromCity ( String departure ) {
  67.         ArrayList<Flight> results = new ArrayList<Flight> ();
  68.         for (int i = 0; i < this.flights.size (); i++) {
  69.             Flight check = this.flights.get ( i );
  70.             if (departure.equals ( check.departure )) {
  71.                 results.add ( check );
  72.  
  73.             }
  74.         }
  75.         return results;
  76.     }
  77.  
  78.     public ArrayList<Flight> getFlightsToCity ( String arrival ) {
  79.         ArrayList<Flight> results = new ArrayList<Flight> ();
  80.         for (int i = 0; i < this.flights.size (); i++) {
  81.             Flight check = this.flights.get ( i );
  82.             if (arrival.equals ( check.arrival )) {
  83.                 results.add ( check );
  84.             }
  85.         }
  86.         return results;
  87.     }
  88.  
  89.     public void displayFlights ( ArrayList<Flight> results ) {
  90.         if (results.isEmpty ()) {
  91.             System.out.println ( "There is no such flight" );
  92.         }
  93.         for (int i = 0; i < results.size (); i++) {
  94.             Flight check = results.get ( i );
  95.             System.out.println ( check.getInfo () );
  96.         }
  97.     }
  98.  
  99.     public void displayFlightsFromCity ( String departure ) {
  100.         ArrayList<Flight> results = getFlightsFromCity ( departure );
  101.         displayFlights ( results );
  102.     }
  103.  
  104.     public void displayFlightsToCity ( String arrival ) {
  105.         ArrayList<Flight> results = getFlightsToCity ( arrival );
  106.         displayFlights ( results );
  107.     }
  108.  
  109.     public ArrayList<String> getCities ( ) {
  110.         ArrayList<String> cities = new ArrayList<String> ();
  111.         for (Flight check : flights) {
  112.             if (!flights.contains ( check.arrival )) cities.add ( check.arrival );
  113.             if (!flights.contains ( check.departure )) cities.add ( check.departure );
  114.  
  115.         }
  116.         return (cities);
  117.  
  118.     }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top