Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Airplane {
- private String id;
- private int capacity;
- private ArrayList<String> flights;
- public Airplane(String id, int capacity) {
- this.capacity = capacity;
- this.id = id;
- this.flights = new ArrayList<String>();
- }
- public void addFlight(String flight) {
- this.flights.add(flight);
- }
- public void printFlights() {
- for (String flight : this.flights) {
- System.out.println(this.toString() + " (" + flight + ")");
- }
- }
- public String getId() {
- return this.id;
- }
- public String toString() {
- return this.id + " (" + this.capacity + " ppl)";
- }
- }
- import java.util.ArrayList;
- public class Airport {
- private ArrayList<Airplane> airplanes;
- public Airport() {
- this.airplanes = new ArrayList<Airplane>();
- }
- public void addAirplane(Airplane airplane) {
- airplanes.add(airplane);
- }
- public Airplane getAirplane(String id) {
- Airplane airplaneWithID = null;
- for (Airplane airplane : this.airplanes) {
- if (airplane.getId().equals(id)) {
- airplaneWithID = airplane;
- break;
- }
- }
- return airplaneWithID;
- }
- public void printAirplanes() {
- for (Airplane airplane : this.airplanes) {
- System.out.println(airplane);
- }
- }
- public void printAllFlights() {
- for (Airplane airplane : this.airplanes) {
- airplane.printFlights();
- }
- }
- }
- import java.util.Scanner;
- public class TestUserInterface {
- private Scanner scanner;
- private Airport airport;
- public TestUserInterface(Scanner scanner, Airport airport) {
- this.airport = airport;
- this.scanner = scanner;
- }
- public void start() {
- System.out.println("Airport panel\n" +
- "--------------------\n" +
- "\n");
- String command = "";
- while (!command.equals("x")) {
- System.out.println("Choose operation:\n" +
- "[1] Add airplane\n" +
- "[2] Add flight\n" +
- "[x] Exit");
- command = this.scanner.nextLine();
- commandHandling(command);
- if (command.equals("x")) {
- break;
- }
- }
- }
- public void commandHandling(String command) {
- if (command.equals("1")) {
- System.out.print("Give plane ID: ");
- String id = this.scanner.nextLine();
- System.out.print("Give plane capacity: ");
- int capacity = Integer.parseInt(this.scanner.nextLine());
- Airplane airplane = new Airplane(id, capacity);
- this.airport.addAirplane(airplane);
- } else if (command.equals("2")) {
- System.out.println("Give plane ID: ");
- String id = this.scanner.nextLine();
- if (!this.airport.getAirplane(id).equals(null)) {
- System.out.print("Give departure airport code: ");
- String departure = this.scanner.nextLine();
- System.out.print("Give destination airport code: ");
- String destination = this.scanner.nextLine();
- this.airport.getAirplane(id).addFlight(departure + "-" + destination);
- } else {
- System.out.println("There is no Airplane with this ID");
- }
- } else if (command.equals("x")) {
- System.out.println("Flight service\n" +
- "------------");
- String printingCommand = "";
- while (!printingCommand.equals("x")) {
- System.out.println("Choose operation:\n" +
- "[1] Print planes\n" +
- "[2] Print flights\n" +
- "[3] Print plane info\n" +
- "[x] Quit");
- printingCommand = scanner.nextLine();
- if (printingCommand.equals("1")) {
- this.airport.printAirplanes();
- } else if (printingCommand.equals("2")) {
- this.airport.printAllFlights();
- } else if (printingCommand.equals("3")) {
- System.out.println("Give plane ID: ");
- String id = this.scanner.nextLine();
- System.out.println(this.airport.getAirplane(id));
- } else if (printingCommand.equals("x")) {
- break;
- } else {
- System.out.println("There is no command like this!");
- }
- }
- }
- }
- }
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- // Write your main program here. Implementing your own classes will be useful.
- Scanner scanner = new Scanner(System.in);
- Airport airport = new Airport();
- TestUserInterface ui = new TestUserInterface(scanner, airport);
- ui.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement