Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------
- import java.util.Scanner;
- public class UserInterface {
- private Airport airport;
- private Scanner reader;
- public UserInterface(Scanner reader, Airport airport) {
- this.reader = reader;
- this.airport = airport;
- }
- public void start() {
- System.out.println("Airport panel\n"
- + "--------------------\n"
- + "\n");
- String input = "";
- while (!input.equals("x")) {
- printAirportMenu();
- input = getInput();
- handleInput1(input);
- }
- System.out.println("Flight service\n"
- + "------------\n"
- + "\n");
- input = "";
- while (!input.equals("x")) {
- printFlightMenu();
- input = getInput();
- handleInput2(input);
- }
- }
- private String stringFix(String str) {
- return str.trim().toLowerCase();
- }
- private void handleInput1(String input) {
- if (input.equals("1")) {
- addPlane();
- } else if (input.equals("2")) {
- addFlight();
- }
- }
- private void addPlane() {
- System.out.print("Give plane ID: ");
- String id = stringFix(reader.nextLine());
- System.out.print("Give plane capacity: ");
- int capacity = Integer.parseInt(stringFix(reader.nextLine()));
- this.airport.addPlane(id, capacity);
- }
- private void addFlight() {
- System.out.print("Give plane ID: ");
- String id = stringFix(reader.nextLine());
- Airplane plane = this.airport.findPlane(id);
- System.out.print("Give departure airport code: ");
- String depart = stringFix(reader.nextLine());
- System.out.print("Give destination airport code: ");
- String destination = stringFix(reader.nextLine());
- this.airport.addFlight(plane, depart, destination);
- }
- private String getInput() {
- System.out.print("> ");
- String input = stringFix(reader.nextLine());
- return input;
- }
- private void printAirportMenu() {
- System.out.println("Choose operation:\n"
- + "[1] Add airplane\n"
- + "[2] Add flight\n"
- + "[x] Exit");
- }
- private void printFlightMenu() {
- System.out.println("Choose operation:\n"
- + "[1] Print planes\n"
- + "[2] Print flights\n"
- + "[3] Print plane info\n"
- + "[x] Quit");
- }
- private void handleInput2(String input) {
- if (input.equals("1")) {
- printPlanes();
- } else if (input.equals("2")) {
- printFlight();
- } else if (input.equals("3")) {
- printPlaneInfo();
- }
- }
- private void printPlanes() {
- airport.printPlanes();
- }
- private void printFlight() {
- airport.printFlights();
- }
- private void printPlaneInfo() {
- System.out.println("Give plane ID: ");
- String input = stringFix(reader.nextLine());
- System.out.println(airport.planeInfo(input));
- }
- }
- ---------------------------------------------------------------
- import java.util.ArrayList;
- public class Airport {
- /*
- could use hashmaps instead of ArrayList to avoid scrolling through each
- then the plane id can just point to the plane
- */
- private ArrayList<Airplane> planes;
- private ArrayList<Flight> flights;
- public Airport() {
- this.planes = new ArrayList<Airplane>();
- this.flights = new ArrayList<Flight>();
- }
- public void addPlane(String id, int capacity) {
- this.planes.add(new Airplane(id, capacity));
- }
- public void addFlight(Airplane plane, String depart, String destination) {
- this.flights.add(new Flight(plane, depart, destination));
- }
- public Airplane findPlane(String id) {
- Airplane plane = null;
- for (Airplane airplane : planes) {
- if (airplane.getID().equals(id)) {
- plane = airplane;
- }
- }
- return plane;
- }
- public void printPlanes() {
- for (Airplane plane : planes) {
- System.out.println(plane);
- }
- }
- public Airplane planeInfo(String id) {
- for (Airplane plane : planes) {
- if (plane.getID().equals(id)) {
- return plane;
- }
- }
- return null;
- }
- public void printFlights() {
- for (Flight flight : flights) {
- System.out.println(flight);
- }
- }
- }
- ---------------------------------------------------------------
- public class Airplane {
- private String id;
- private int capacity;
- public Airplane(String id, int capacity) {
- this.id = id;
- this.capacity = capacity;
- }
- public String getID() {
- return this.id;
- }
- public String toString() {
- return id.toUpperCase() + " (" + capacity + " ppl)";
- }
- }
- ---------------------------------------------------------------
- public class Flight {
- private Airplane plane;
- private String departure;
- private String destination;
- public Flight(Airplane plane, String departure, String destination){
- this.plane = plane;
- this.departure = departure;
- this.destination = destination;
- }
- public String toString() {
- return plane + " (" + departure.toUpperCase() + "-" + destination.toUpperCase() + ")";
- }
- }
- ---------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment