Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Main {
- public static void main ( String[] args ) {
- FlightDatabase flightDatabase = new FlightDatabase ();{
- // flightDatabase.checkIfFlightExists ("Wroclaw", "Berlin");
- // flightDatabase.displayFlightsFromCity ( "Warsaw" );
- // flightDatabase.displayFlightsToCity ( "Rotterdam" );
- //
- ArrayList<String>citiesList = flightDatabase.getCities (); {
- System.out.println ( "\n" + citiesList );
- }
- // Flight cheapestFlight = flightDatabase.getCheapestFlight (); {
- // System.out.println ( "The Cheapest Flight is:" + cheapestFlight.getInfo () );
- // }
- // Flight cheapestFlightFromCity = flightDatabase.getCheapestFlightFromSpecifiedCity ( "Wroclaw" ); {
- // System.out.println ("The cheapest flight from specified city is: " + cheapestFlightFromCity.getInfo ());
- // }
- ArrayList<Flight> pathfinder = flightDatabase.getFlights ( "Wroclaw", "Vienna" );
- System.out.println ("The flight you require looks like this: " + pathfinder );
- }
- }
- }
- public class Flight{
- String departure;
- String arrival;
- double price;
- public Flight (String departure, String arrival, int price){
- this.departure = departure;
- this.arrival = arrival;
- this.price = price;
- }
- public String getInfo(){
- return "\n\nFlight from: " + this.departure + "\n\nFlight to: " + this.arrival +
- "\n\nPrice of this flight is: " + this.price;
- }
- }
- import java.util.ArrayList;
- public class FlightDatabase {
- ArrayList<Flight> flights = new ArrayList<> ();
- public FlightDatabase ( ) {
- this.flights.add ( new Flight ( "Wroclaw", "Krakow", 399 ) );
- this.flights.add ( new Flight ( "Warsaw", "Poznan", 199 ) );
- this.flights.add ( new Flight ( "Krakow", "Vienna", 699 ) );
- this.flights.add ( new Flight ( "Wroclaw", "Rotterdam", 499 ) );
- this.flights.add ( new Flight ( "Warsaw", "Moscow", 999 ) );
- this.flights.add ( new Flight ( "Wroclaw", "Frankfurt", 699 ) );
- this.flights.add ( new Flight ( "Warsaw", "London", 499 ) );
- this.flights.add ( new Flight ( "Krakow", "Paris", 459 ) );
- }
- public void checkIfFlightExists ( String departure, String arrival ) {
- for (Flight check : flights) {
- if (departure.equals ( check.departure ) && arrival.equals ( check.arrival )) {
- System.out.println ( "there is such flight" );
- return;
- }
- }
- System.out.println ( "No such flight sorry" );
- }
- public ArrayList<Flight> getFlightsFromCity ( String departure ) {
- ArrayList<Flight> results = new ArrayList<> ();
- for (Flight check : this.flights) {
- if (departure.equals ( check.departure )) {
- results.add ( check );
- }
- }
- return results;
- }
- public ArrayList<Flight> getFlightsToCity ( String arrival ) {
- ArrayList<Flight> results = new ArrayList<> ();
- for (Flight check : this.flights) {
- if (arrival.equals ( check.arrival )) {
- results.add ( check );
- }
- }
- return results;
- }
- public void displayFlights ( ArrayList<Flight> results ) {
- if (results.isEmpty ()) {
- System.out.println ( "There is no such flight" );
- }
- for (Flight check : results) {
- System.out.println ( check.getInfo () );
- }
- }
- public void displayFlightsFromCity ( String departure ) {
- ArrayList<Flight> results = getFlightsFromCity ( departure );
- displayFlights ( results );
- }
- public void displayFlightsToCity ( String arrival ) {
- ArrayList<Flight> results = getFlightsToCity ( arrival );
- displayFlights ( results );
- }
- public ArrayList<String> getCities ( ) {
- ArrayList<String> cities = new ArrayList<> ();
- for (Flight check : flights) {
- if (!cities.contains ( check.arrival )) cities.add ( check.arrival );
- if (!cities.contains ( check.departure )) cities.add ( check.departure );
- }
- return cities;
- }
- public Flight getCheapestFlight ( ) {
- Flight cheapestFlight = null;
- for (Flight flight : this.flights) {
- if (cheapestFlight == null || flight.price < cheapestFlight.price) {
- cheapestFlight = flight;
- }
- }
- return cheapestFlight;
- }
- public Flight getCheapestFlightFromSpecifiedCity(String departure){
- Flight cheapestFlight = null;
- for (Flight flight: getFlightsFromCity ( departure )){
- if (cheapestFlight == null || flight.price < cheapestFlight.price){
- cheapestFlight = flight;
- }
- }
- return cheapestFlight;
- }
- public ArrayList<Flight>getFlights(String departure, String arrival){
- ArrayList<Flight>departureCity = getFlightsFromCity ( departure );
- ArrayList<Flight>arrivalCity = getFlightsToCity ( arrival );
- ArrayList<Flight>results = new ArrayList<Flight> ( );
- for (Flight first : departureCity){
- for (Flight second : arrivalCity){
- if (first.arrival.equals ( second.departure )){
- results.add (first);
- results.add (second);
- }
- }
- }
- return results;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement