Guest User

Untitled

a guest
Jan 9th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.75 KB | None | 0 0
  1. ---------------------------------------------------------------
  2. import java.util.Scanner;
  3.  
  4. public class UserInterface {
  5.  
  6.     private Airport airport;
  7.     private Scanner reader;
  8.  
  9.     public UserInterface(Scanner reader, Airport airport) {
  10.         this.reader = reader;
  11.         this.airport = airport;
  12.     }
  13.  
  14.     public void start() {
  15.        
  16.         System.out.println("Airport panel\n"
  17.                 + "--------------------\n"
  18.                 + "\n");
  19.         String input = "";
  20.         while (!input.equals("x")) {
  21.             printAirportMenu();
  22.             input = getInput();
  23.             handleInput1(input);
  24.         }
  25.        
  26.        
  27.         System.out.println("Flight service\n"
  28.                 + "------------\n"
  29.                 + "\n");
  30.         input = "";
  31.         while (!input.equals("x")) {
  32.             printFlightMenu();
  33.             input = getInput();
  34.             handleInput2(input);
  35.         }
  36.     }
  37.  
  38.     private String stringFix(String str) {
  39.         return str.trim().toLowerCase();
  40.     }
  41.  
  42.     private void handleInput1(String input) {
  43.         if (input.equals("1")) {
  44.             addPlane();
  45.         } else if (input.equals("2")) {
  46.             addFlight();
  47.         }
  48.  
  49.     }
  50.  
  51.     private void addPlane() {
  52.         System.out.print("Give plane ID: ");
  53.         String id = stringFix(reader.nextLine());
  54.         System.out.print("Give plane capacity: ");
  55.         int capacity = Integer.parseInt(stringFix(reader.nextLine()));
  56.         this.airport.addPlane(id, capacity);
  57.  
  58.     }
  59.  
  60.     private void addFlight() {
  61.         System.out.print("Give plane ID: ");
  62.         String id = stringFix(reader.nextLine());
  63.         Airplane plane = this.airport.findPlane(id);
  64.         System.out.print("Give departure airport code: ");
  65.         String depart = stringFix(reader.nextLine());
  66.         System.out.print("Give destination airport code: ");
  67.         String destination = stringFix(reader.nextLine());
  68.         this.airport.addFlight(plane, depart, destination);
  69.     }
  70.  
  71.     private String getInput() {
  72.         System.out.print("> ");
  73.         String input = stringFix(reader.nextLine());
  74.         return input;
  75.     }
  76.    
  77.     private void printAirportMenu() {
  78.         System.out.println("Choose operation:\n"
  79.                 + "[1] Add airplane\n"
  80.                 + "[2] Add flight\n"
  81.                 + "[x] Exit");
  82.     }
  83.    
  84.     private void printFlightMenu() {
  85.         System.out.println("Choose operation:\n"
  86.                 + "[1] Print planes\n"
  87.                 + "[2] Print flights\n"
  88.                 + "[3] Print plane info\n"
  89.                 + "[x] Quit");
  90.     }
  91.  
  92.     private void handleInput2(String input) {
  93.         if (input.equals("1")) {
  94.             printPlanes();
  95.         } else if (input.equals("2")) {
  96.             printFlight();
  97.         } else if (input.equals("3")) {
  98.             printPlaneInfo();
  99.         }
  100.     }
  101.  
  102.     private void printPlanes() {
  103.         airport.printPlanes();
  104.     }
  105.    
  106.  
  107.     private void printFlight() {
  108.         airport.printFlights();
  109.     }
  110.  
  111.     private void printPlaneInfo() {
  112.         System.out.println("Give plane ID: ");
  113.         String input = stringFix(reader.nextLine());
  114.         System.out.println(airport.planeInfo(input));
  115.     }
  116. }
  117. ---------------------------------------------------------------
  118.  
  119. import java.util.ArrayList;
  120.  
  121.  
  122. public class Airport {
  123.  
  124.     /*
  125.     could use hashmaps instead of ArrayList to avoid scrolling through each
  126.     then the plane id can just point to the plane
  127.     */
  128.     private ArrayList<Airplane> planes;
  129.     private ArrayList<Flight> flights;
  130.    
  131.     public Airport() {
  132.         this.planes = new ArrayList<Airplane>();
  133.         this.flights = new ArrayList<Flight>();
  134.                
  135.     }
  136.  
  137.     public void addPlane(String id, int capacity) {
  138.         this.planes.add(new Airplane(id, capacity));
  139.     }
  140.  
  141.     public void addFlight(Airplane plane, String depart, String destination) {
  142.         this.flights.add(new Flight(plane, depart, destination));
  143.     }
  144.  
  145.     public Airplane findPlane(String id) {
  146.         Airplane plane = null;
  147.         for (Airplane airplane : planes) {
  148.             if (airplane.getID().equals(id)) {
  149.                 plane = airplane;
  150.             }
  151.         }
  152.         return plane;
  153.     }
  154.    
  155.     public void printPlanes() {
  156.         for (Airplane plane : planes) {
  157.             System.out.println(plane);
  158.         }
  159.     }
  160.  
  161.     public Airplane planeInfo(String id) {
  162.         for (Airplane plane : planes) {
  163.             if (plane.getID().equals(id)) {
  164.                 return plane;
  165.             }
  166.         }
  167.         return null;
  168.     }
  169.  
  170.     public void printFlights() {
  171.         for (Flight flight : flights) {
  172.             System.out.println(flight);
  173.         }
  174.     }
  175. }
  176.  
  177. ---------------------------------------------------------------
  178.  
  179. public class Airplane {
  180.    
  181.     private String id;
  182.     private int capacity;
  183.    
  184.     public Airplane(String id, int capacity) {
  185.         this.id = id;
  186.         this.capacity = capacity;
  187.        
  188.     }
  189.    
  190.     public String getID() {
  191.         return this.id;
  192.     }
  193.    
  194.     public String toString() {
  195.         return id.toUpperCase() + " (" + capacity + " ppl)";
  196.     }
  197.    
  198. }
  199. ---------------------------------------------------------------
  200.  
  201. public class Flight {
  202.     private Airplane plane;
  203.     private String departure;
  204.     private String destination;
  205.    
  206.     public Flight(Airplane plane, String departure, String destination){
  207.         this.plane = plane;
  208.         this.departure = departure;
  209.         this.destination = destination;
  210.     }
  211.    
  212.     public String toString() {
  213.         return plane + " (" + departure.toUpperCase() + "-" + destination.toUpperCase() + ")";
  214.     }
  215. }
  216.  
  217. ---------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment