Я нахожусь в процессе создания модели регистрации в аэропорту. Программа является интерактивной через терминал (cmd / IDE) и может добавлять различные типы объектов в ArrayLists. Я хотел бы перечислить все созданные объекты с соответствующими полями.
Я создал метод для перечисления всех сгенерированных аэродромов через терминал, но для рейсов, пассажиров, экипажей я не знаю, как это сделать. Спасибо за помощь! Учиться, чтобы стать младшим разработчиком 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();
}
}