Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Main {
- /* So the thing is I'm supposed to create a new method for
- FlightDatabase class which will return (not print) the list
- of all cities included in FlightDatabase constructor, without
- duplicates. I got a hint to use reversed boolean (!=), and
- Arraylist.contains (object o) method.
- On the bottom of this paste is what I came up with, doesn't work,
- IntelliJ's saying that this method's return is never used.*/
- public static void main ( String[] args ) {
- FlightDatabase flightDatabase = new FlightDatabase ();{
- // flightDatabase.checkIfFlightExists ("Wroclaw", "Berlin");
- // flightDatabase.displayFlightsFromCity ( "Warsaw" );
- // flightDatabase.displayFlightsToCity ( "Rotterdam" );
- flightDatabase.getCities ();
- }
- }
- }
- public class Flight{
- String departure;
- String arrival;
- public Flight (String departure, String arrival){
- this.departure = departure;
- this.arrival = arrival;
- }
- public String getInfo(){
- return "\n\nFlight from: " + this.departure + "\n\nFlight to: " + this.arrival;
- }
- }
- 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.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<Flight> ();
- for (int i = 0; i < this.flights.size (); i++) {
- Flight check = this.flights.get ( i );
- if (departure.equals ( check.departure )) {
- results.add ( check );
- }
- }
- return results;
- }
- public ArrayList<Flight> getFlightsToCity ( String arrival ) {
- ArrayList<Flight> results = new ArrayList<Flight> ();
- for (int i = 0; i < this.flights.size (); i++) {
- Flight check = this.flights.get ( i );
- 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 (int i = 0; i < results.size (); i++) {
- Flight check = results.get ( i );
- 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<String> ();
- for (Flight check : flights) {
- if (!flights.contains ( check.arrival )) cities.add ( check.arrival );
- if (!flights.contains ( check.departure )) cities.add ( check.departure );
- }
- return (cities);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement