无法从用户输入打印ArrayList中的元素

时间:2015-01-30 00:13:22

标签: java arraylist multiple-constructors

我无法弄清楚为什么我的程序没有打印用户输入(平面ID,容量)。我编辑了我的代码,所以当用户选择打印平面打印平面信息时,它工作正常。但是我的原始问题与副本不同。我正在尝试返回planeID,容量和目的地,离开。我已经有一个toString方法,我不想用另一个方法覆盖。

基本上,该计划的目标是添加飞机,飞机目的地/起飞和飞机容量。然后,一旦添加它们,用户就可以选择他们想要打印的选项。 printAllPlanes打印所有planeID和capacity,printAllFlights打印planeID,capacity和destintation / departure,printPlanesInfo打印planeId / capacity。

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        FlightServices fs = new FlightServices();

        UserInput ui = new UserInput(reader, fs);
        ui.start();

        System.out.println("Flight Service");
        System.out.println("----------");

        ui.printing();

    }

}
public class Airport {
    private String planeId;
    private int capacity;
    private String dest;
    private String dep;

    public Airport(String planeId, int capacity) {
        this.planeId = planeId;
        this.capacity = capacity;
    }

    public Airport(String planeId, String dep, String dest) {
        this.dest = dest;
        this.dep = dep;

    }

    public String getPlaneId() {
        return this.planeId;
    }

    public void setPlaneId(String planeId) {
        this.planeId = planeId;
    }

    public int getCapacity() {
        return this.capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public String getDestination() {
        return this.dest;
    }

    public void setDestination(String dest) {
        this.dest = dest;
    }

    public String getDeparture() {
        return this.dep;
    }

    public void setDeparture(String dep) {
        this.dep = dep;
    }

    public String firstString() {
        return planeId + " (" + capacity + ")";
    }

    public String secString() {
        return planeId + " (" + capacity + ")" + "(" + dest + "-" + dep;
    }
}
import java.util.ArrayList;

public class FlightServices {
    private ArrayList<Airport> airport;

    public FlightServices() {
        airport = new ArrayList<Airport>();

    }

    public void add(String planeId, int capacity) {
        airport.add(new Airport(planeId, capacity));
    }

    public void addFlight(String planeId, String dest, String dep) {
        airport.add(new Airport(planeId, dest, dep));
    }

    public void printAllPlanes() {
        for (Airport all : airport) {
            System.out.println(all);
        }
    }

    public void printAllFlights() {
        for (Airport all : airport) {
            System.out.println(all);
        }
    }

    public void printPlanesInfo(String planeId) {
        for (Airport info : airport) {
            if (planeId.equals(info.getPlaneId())) {
                info.toString();
            }
        }
    }

}
import java.util.Scanner;

public class UserInput {
    private Scanner reader;
    private FlightServices fs;

    public UserInput(Scanner reader, FlightServices fs) {
        this.reader = reader;
        this.fs = fs;
    }

    public void start() {

        while (true) {
            System.out.println("Choose operation: ");
            System.out.println("[1] Add airplane");
            System.out.println("[2] Add flight");
            System.out.println("[3] Exit");

            int input = Integer.parseInt(reader.nextLine());
            if (input == 3) {
                break;
            } else if (input == 1) {
                this.addPlane();
            } else if (input == 2) {
                this.addFlight();
            }
        }

    }

    public void addPlane() {
        System.out.println("Give plane ID: ");
        String id = reader.nextLine();
        System.out.println("Give plane capacity: ");
        int capacity = Integer.parseInt(reader.nextLine());

        this.fs.add(id, capacity);
    }

    public void addFlight() {
        System.out.println("Give plane ID: ");
        String id = reader.nextLine();
        System.out.println("Give departure airport code: ");
        String dep = reader.nextLine();
        System.out.println("Give destination airport code: ");
        String des = reader.nextLine();

        this.fs.addFlight(id, dep, des);
    }

    public void printing() {
        while (true) {
            System.out.println("Choose operation: ");
            System.out.println("[1] Print planes");
            System.out.println("[2] Print flights");
            System.out.println("[3] Print plane info");
            System.out.println("[4] Quit");

            int command = Integer.parseInt(reader.nextLine());
            if (command == 4) {
                break;
            } else if (command == 1) {
                this.fs.printAllPlanes();
            } else if (command == 2) {
                this.fs.printAllFlights();
            } else if (command == 3) {
                this.addPlaneInfo();
            }

        }

    }

    public void addPlaneInfo() {
        System.out.println("Give plane ID: ");
        String id = reader.nextLine();

        this.fs.printPlanesInfo(id);

    }

}

0 个答案:

没有答案