Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.09 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Airplane {
  4.     private String id;
  5.     private int capacity;
  6.     private ArrayList<String> flights;
  7.  
  8.     public Airplane(String id, int capacity) {
  9.         this.capacity = capacity;
  10.         this.id = id;
  11.         this.flights = new ArrayList<String>();
  12.     }
  13.  
  14.     public void addFlight(String flight) {
  15.         this.flights.add(flight);
  16.     }
  17.  
  18.     public void printFlights() {
  19.         for (String flight : this.flights) {
  20.             System.out.println(this.toString() + " (" + flight + ")");
  21.         }
  22.     }
  23.  
  24.     public String getId() {
  25.         return this.id;
  26.     }
  27.  
  28.     public String toString() {
  29.         return this.id + " (" + this.capacity + " ppl)";
  30.     }
  31. }
  32.  
  33. import java.util.ArrayList;
  34.  
  35. public class Airport {
  36.     private ArrayList<Airplane> airplanes;
  37.  
  38.     public Airport() {
  39.         this.airplanes = new ArrayList<Airplane>();
  40.     }
  41.  
  42.     public void addAirplane(Airplane airplane) {
  43.         airplanes.add(airplane);
  44.     }
  45.  
  46.     public Airplane getAirplane(String id) {
  47.         Airplane airplaneWithID = null;
  48.         for (Airplane airplane : this.airplanes) {
  49.             if (airplane.getId().equals(id)) {
  50.                 airplaneWithID = airplane;
  51.                 break;
  52.             }
  53.         }
  54.         return airplaneWithID;
  55.     }
  56.  
  57.     public void printAirplanes() {
  58.         for (Airplane airplane : this.airplanes) {
  59.             System.out.println(airplane);
  60.         }
  61.     }
  62.  
  63.     public void printAllFlights() {
  64.         for (Airplane airplane : this.airplanes) {
  65.             airplane.printFlights();
  66.         }
  67.     }
  68. }
  69.  
  70. import java.util.Scanner;
  71.  
  72. public class TestUserInterface {
  73.     private Scanner scanner;
  74.     private Airport airport;
  75.  
  76.     public TestUserInterface(Scanner scanner, Airport airport) {
  77.         this.airport = airport;
  78.         this.scanner = scanner;
  79.     }
  80.  
  81.     public void start() {
  82.         System.out.println("Airport panel\n" +
  83.                 "--------------------\n" +
  84.                 "\n");
  85.         String command = "";
  86.  
  87.         while (!command.equals("x")) {
  88.             System.out.println("Choose operation:\n" +
  89.                     "[1] Add airplane\n" +
  90.                     "[2] Add flight\n" +
  91.                     "[x] Exit");
  92.             command = this.scanner.nextLine();
  93.  
  94.             commandHandling(command);
  95.             if (command.equals("x")) {
  96.                 break;
  97.             }
  98.         }
  99.     }
  100.  
  101.     public void commandHandling(String command) {
  102.         if (command.equals("1")) {
  103.             System.out.print("Give plane ID: ");
  104.             String id = this.scanner.nextLine();
  105.             System.out.print("Give plane capacity: ");
  106.             int capacity = Integer.parseInt(this.scanner.nextLine());
  107.  
  108.             Airplane airplane = new Airplane(id, capacity);
  109.             this.airport.addAirplane(airplane);
  110.  
  111.         } else if (command.equals("2")) {
  112.             System.out.println("Give plane ID: ");
  113.             String id = this.scanner.nextLine();
  114.  
  115.             if (!this.airport.getAirplane(id).equals(null)) {
  116.                 System.out.print("Give departure airport code: ");
  117.                 String departure = this.scanner.nextLine();
  118.                 System.out.print("Give destination airport code: ");
  119.                 String destination = this.scanner.nextLine();
  120.  
  121.                 this.airport.getAirplane(id).addFlight(departure + "-" + destination);
  122.             } else {
  123.                 System.out.println("There is no Airplane with this ID");
  124.  
  125.             }
  126.         } else if (command.equals("x")) {
  127.             System.out.println("Flight service\n" +
  128.                     "------------");
  129.             String printingCommand = "";
  130.  
  131.             while (!printingCommand.equals("x")) {
  132.                 System.out.println("Choose operation:\n" +
  133.                         "[1] Print planes\n" +
  134.                         "[2] Print flights\n" +
  135.                         "[3] Print plane info\n" +
  136.                         "[x] Quit");
  137.                 printingCommand = scanner.nextLine();
  138.  
  139.                 if (printingCommand.equals("1")) {
  140.                     this.airport.printAirplanes();
  141.                 } else if (printingCommand.equals("2")) {
  142.                     this.airport.printAllFlights();
  143.                 } else if (printingCommand.equals("3")) {
  144.                     System.out.println("Give plane ID: ");
  145.                     String id = this.scanner.nextLine();
  146.                     System.out.println(this.airport.getAirplane(id));
  147.                 } else if (printingCommand.equals("x")) {
  148.                     break;
  149.                 } else {
  150.                     System.out.println("There is no command like this!");
  151.                 }
  152.             }
  153.         }
  154.     }
  155. }
  156.  
  157. import java.util.Scanner;
  158.  
  159. public class Main {
  160.     public static void main(String[] args) {
  161.         // Write your main program here. Implementing your own classes will be useful.
  162.         Scanner scanner = new Scanner(System.in);
  163.         Airport airport = new Airport();
  164.  
  165.         TestUserInterface ui = new TestUserInterface(scanner, airport);
  166.         ui.start();
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement