I have to add to each plane file a specific passanger as they are added to the plane
Ex: plane1 = p1 = new Passanger("Victor", "Mihai", 28);
So inside file Beoing.txt I will have only Passanger{name='Victor', surname='Mihai', age=28}
But instead I have all of them in each file and that is not what I want to have.
The following code I made is adding all the passangers , but I cannot find the way to add only the one who is added to the plane.
My Passenger class:
public class Passanger {
private String name;
private String surname;
private int age;
public Passanger(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Passanger{" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
", age=" + age +
'}';
}
}
My Plane class:
import java.util.ArrayList;
import java.util.List;
public class Plane {
private String name;
private String color;
private int spaces;
List<Passanger> passangers = new ArrayList<>();
public Plane(String name, String color, int spaces) {
this.name = name;
this.color = color;
this.spaces = spaces;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSpaces() {
return spaces;
}
public void setSpaces(int spaces) {
this.spaces = spaces;
}
@Override
public String toString() {
return "Plane{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", spaces=" + spaces +
'}';
}
public void addPassanger(Passanger newPassanger) {
passangers.add(newPassanger);
}
}
My main:
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main2 {
public static void main(String[] args){
Passanger p1 = new Passanger("Victor", "Mihai", 28);
Passanger p2 = new Passanger("Alex", "Olan", 25);
Passanger p3 = new Passanger("Vlad", "Pioter", 23);
List<Passanger> passengerList = new ArrayList<>(Arrays.asList(p1, p2, p3));
Plane plane1 = new Plane("Beoing", "red", 200);
plane1.addPassanger(p1);
Plane plane2 = new Plane("Mig", "gray", 2);
plane2.addPassanger(p2);
Plane plane3 = new Plane("jet", "blue", 10);
plane3.addPassanger(p3);
List<Plane> planeList = new ArrayList<>(Arrays.asList(plane1, plane2, plane3));
writePassengersOnPlaneFile(passengerList, planeList);
public static void writePassengersOnPlaneFile(List<Passanger> passengerList, List<Plane> planeList) {
for (int i = 0; i < passengerList.size(); i++) {
for (int j = 0; j < planeList.size(); j++) {
try {
***//This write all the passangers to each file***
FileWriter fileWriter = new FileWriter(planeList.get(j)
.getName() + ".txt", true);
fileWriter.write(passengerList.get(i)***//here I must take passanger per
//plane added but I do not get it how,(any hint please?)***
.toString() + "\n");
fileWriter.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
}
}
Can someone please give me a hint ?
Thanks
>Solution :
You should iterate through the planes first, then make a getter for the passenger list inside the plane and iterate through those.
public class Plane {
// ...
public List<Passanger> getPassengers() {
return this.passangers;
}
}
You also need to make sure you create one file writer per plane:
public static void writePassengersOnPlaneFile(List<Plane> planeList) {
for (int j = 0; j < planeList.size(); j++) {
Plane plane = planeList.get(j);
try {
FileWriter fileWriter = new FileWriter(plane.getName() + ".txt", true);
for (int i = 0; i < plane.getPassengers().size(); i++) {
Passanger passenger = plane.getPassengers().get(i);
fileWriter.write(passenger.toString() + "\n");
}
fileWriter.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}