我正在创建机场注册模型。该程序可以通过终端(cmd / IDE)进行交互,并且可以向ArrayList添加不同的对象类型。我想列出所有创建的对象及其相应的字段。
我创建了一种通过终端列出所有生成的机场的方法,但是对于航班,乘客,机组人员,我不知道该怎么做。感谢您的帮助!学习成为一名初级Java开发人员。谢谢。
private static List<AirField> airfields = new ArrayList<>();
static int startingIndexToOne = 1;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //writing in/reading from the console
boolean looperMain = true;
while (looperMain) {
System.out.println("Create a new...: a - AIRFIELD, f - FLIGHT, p - PASSENGER, c - CREW MEMBER");
System.out.println("Other input commands: exit - EXIT PROGRAM");
System.out.println();
System.out.print("Enter: ");
String line = reader.readLine();
switch (line) {
case "exit":
looperMain = false;
break;
case "a":
AirField airfield = new AirField();
System.out.println("An AIRFIELD has been instantiated!");
System.out.print("Assign a NAME to the AIRFIELD: ");
airfield.setName(reader.readLine());
airfields.add(airfield);
System.out.println("The " + (airfields.indexOf(airfield) + startingIndexToOne) + ".AIRFIELD's name has been set as [" + airfield.getName() + "].");
System.out.println();
getListAirFields();
airfield.getListFlights();
break;
case "f":
Flight flight = new Flight();
System.out.println("The FLIGHT has been instantiated.");
System.out.println("Assign a CODE to the FLIGHT: ");
flight.setCode(reader.readLine());
System.out.println("The FLIGHT's code has been set as [" + flight.getCode() + "].");
System.out.println();
break;
case "p":
Passenger passenger = new Passenger();
System.out.println("A passenger has been instantiated.");
System.out.print("Assign a NAME to the PASSENGER: ");
passenger.setName(reader.readLine());
System.out.print("\nThe PASSENGER's name has been set as [" + passenger.getName() + "].");
System.out.print("\nAssign " + passenger.getName() + "'s checked bags value(in Kg): ");
int checkedBagsInKg = reader.read();
passenger.setCheckedBagsInKg(checkedBagsInKg);
System.out.println(passenger.getName() + "'s checked bags has been set as [" + passenger.getCheckedBagsInKg() + " kg].");
break;
case "c":
Crew crewMember = new Crew();
System.out.println("A CREW MEMBER has been instantiated.");
System.out.print("Assign the NAME of the CREW MEMBER: ");
crewMember.setName(reader.readLine());
System.out.print("The CREW MEMBER's name has been set as [" + crewMember.getName() + "].");
System.out.print("Assign " + crewMember.getName() + "'s JOB type: ");
crewMember.setCrewJob(reader.readLine());
System.out.println(crewMember.getName() + "'s JOB type has been set as [" + crewMember.getCrewJob() + "]");
break;
default:
System.out.println("Error: Incorrect input.");
break;
}
}
}
private static void getListAirFields () { //reading airfield one by one from the ArrayList<AirField> airfields.
System.out.println("Created airfields: ");
System.out.println();
for (AirField airfield : airfields) {
System.out.println("#" + (airfields.indexOf(airfield) + startingIndexToOne) + " airfield: " + "Name - " + airfield.getName());
}
}
}
公共类AirField实现了Iterable {
public static List<Flight> flights = new ArrayList<>();
String name;
public List<Flight> getFlights() {
return flights;
}
public void setFlights(List<Flight> flights) {
this.flights = flights;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Iterator<Flight> iterator() {
return flights.iterator();
}
public void getListFlights() {
System.out.println("Created flights: ");
for (Flight flight : flights) {
System.out.println(flight.getCode());
}
}
公共类Flight实现了Iterable {
private String code;
private static List<Person> people = new ArrayList<>();
public List<Person> getPeople() {
return people;
}
public void setPeople(List<Person> people) {
this.people = people;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public Iterator<Person> iterator() {
return people.iterator();
}
}