Advertisement
uriahheep

Untitled

Apr 9th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.59 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Main {
  4.  
  5.     public static void main ( String[] args ) {
  6.  
  7.         FlightDatabase flightDatabase = new FlightDatabase ();{
  8.         //    flightDatabase.checkIfFlightExists ("Wroclaw", "Berlin");
  9.  
  10.           //  flightDatabase.displayFlightsFromCity ( "Warsaw" );
  11.  
  12.         //    flightDatabase.displayFlightsToCity ( "Rotterdam" );
  13. //
  14.             ArrayList<String>citiesList = flightDatabase.getCities (); {
  15.                 System.out.println ( "\n" + citiesList );
  16.             }
  17. //            Flight cheapestFlight = flightDatabase.getCheapestFlight (); {
  18. //                System.out.println ( "The Cheapest Flight is:" + cheapestFlight.getInfo () );
  19. //            }
  20. //            Flight cheapestFlightFromCity = flightDatabase.getCheapestFlightFromSpecifiedCity ( "Wroclaw" ); {
  21. //                System.out.println ("The cheapest flight from specified city is: " + cheapestFlightFromCity.getInfo ());
  22. //            }
  23.             ArrayList<Flight> pathfinder = flightDatabase.getFlights ( "Wroclaw", "Vienna" );
  24.             System.out.println ("The flight you require looks like this: " + pathfinder );
  25.  
  26.  
  27.  
  28.         }
  29.     }
  30. }
  31. public class Flight{
  32.  
  33.     String departure;
  34.     String arrival;
  35.     double price;
  36.  
  37.     public Flight (String departure, String arrival, int price){
  38.         this.departure = departure;
  39.         this.arrival = arrival;
  40.         this.price = price;
  41.     }
  42.     public String getInfo(){
  43.  
  44.         return "\n\nFlight from: " + this.departure + "\n\nFlight to: " + this.arrival +
  45.                 "\n\nPrice of this flight is: " + this.price;
  46.     }
  47.  
  48. }
  49. import java.util.ArrayList;
  50.  
  51. public class FlightDatabase {
  52.     ArrayList<Flight> flights = new ArrayList<> ();
  53.  
  54.     public FlightDatabase ( ) {
  55.         this.flights.add ( new Flight ( "Wroclaw", "Krakow", 399 ) );
  56.         this.flights.add ( new Flight ( "Warsaw", "Poznan", 199 ) );
  57.         this.flights.add ( new Flight ( "Krakow", "Vienna", 699 ) );
  58.         this.flights.add ( new Flight ( "Wroclaw", "Rotterdam", 499 ) );
  59.         this.flights.add ( new Flight ( "Warsaw", "Moscow", 999 ) );
  60.         this.flights.add ( new Flight ( "Wroclaw", "Frankfurt", 699 ) );
  61.         this.flights.add ( new Flight ( "Warsaw", "London", 499 ) );
  62.         this.flights.add ( new Flight ( "Krakow", "Paris", 459 ) );
  63.     }
  64.  
  65.     public void checkIfFlightExists ( String departure, String arrival ) {
  66.         for (Flight check : flights) {
  67.             if (departure.equals ( check.departure ) && arrival.equals ( check.arrival )) {
  68.                 System.out.println ( "there is such flight" );
  69.                 return;
  70.             }
  71.         }
  72.         System.out.println ( "No such flight sorry" );
  73.     }
  74.  
  75.     public ArrayList<Flight> getFlightsFromCity ( String departure ) {
  76.         ArrayList<Flight> results = new ArrayList<> ();
  77.         for (Flight check : this.flights) {
  78.             if (departure.equals ( check.departure )) {
  79.                 results.add ( check );
  80.  
  81.             }
  82.         }
  83.         return results;
  84.     }
  85.  
  86.     public ArrayList<Flight> getFlightsToCity ( String arrival ) {
  87.         ArrayList<Flight> results = new ArrayList<> ();
  88.         for (Flight check : this.flights) {
  89.             if (arrival.equals ( check.arrival )) {
  90.                 results.add ( check );
  91.             }
  92.         }
  93.         return results;
  94.     }
  95.  
  96.     public void displayFlights ( ArrayList<Flight> results ) {
  97.         if (results.isEmpty ()) {
  98.             System.out.println ( "There is no such flight" );
  99.         }
  100.         for (Flight check : results) {
  101.             System.out.println ( check.getInfo () );
  102.         }
  103.     }
  104.  
  105.     public void displayFlightsFromCity ( String departure ) {
  106.         ArrayList<Flight> results = getFlightsFromCity ( departure );
  107.         displayFlights ( results );
  108.     }
  109.  
  110.     public void displayFlightsToCity ( String arrival ) {
  111.         ArrayList<Flight> results = getFlightsToCity ( arrival );
  112.         displayFlights ( results );
  113.     }
  114.  
  115.     public ArrayList<String> getCities ( ) {
  116.         ArrayList<String> cities = new ArrayList<> ();
  117.         for (Flight check : flights) {
  118.             if (!cities.contains ( check.arrival )) cities.add ( check.arrival );
  119.             if (!cities.contains ( check.departure )) cities.add ( check.departure );
  120.  
  121.         }
  122.         return cities;
  123.  
  124.     }
  125.  
  126.     public Flight getCheapestFlight ( ) {
  127.         Flight cheapestFlight = null;
  128.         for (Flight flight : this.flights) {
  129.             if (cheapestFlight == null || flight.price < cheapestFlight.price) {
  130.                 cheapestFlight = flight;
  131.             }
  132.         }
  133.         return cheapestFlight;
  134.     }
  135.     public Flight getCheapestFlightFromSpecifiedCity(String departure){
  136.         Flight cheapestFlight = null;
  137.         for (Flight flight: getFlightsFromCity ( departure )){
  138.             if (cheapestFlight == null || flight.price < cheapestFlight.price){
  139.                 cheapestFlight = flight;
  140.             }
  141.         }
  142.         return cheapestFlight;
  143.     }
  144.     public ArrayList<Flight>getFlights(String departure, String arrival){
  145.         ArrayList<Flight>departureCity = getFlightsFromCity ( departure );
  146.         ArrayList<Flight>arrivalCity = getFlightsToCity ( arrival );
  147.         ArrayList<Flight>results = new ArrayList<Flight> (  );
  148.         for (Flight first  : departureCity){
  149.             for (Flight second : arrivalCity){
  150.                 if (first.arrival.equals ( second.departure )){
  151.                     results.add (first);
  152.                     results.add (second);
  153.                 }
  154.             }
  155.         }
  156.         return results;
  157.     }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement