I would like to create a "Planet" object from a .csv file using a PlanetBuilder and stream. I've only gotten so far that I can read the csv file and for each line, create a Planet object, but unfortunately i do not know how to assign the constructor values to the object, so it basically just creates an empty planet object for each line. Could someone please tell me how to also assign the value for each lines?
After creating the objects I will add them to List planets
This is the method which creates a new planet object.
public List<Planet> csvDataToPlanets(String filePath) {
List<Planet> planets = new ArrayList<>();
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
List<List<String>> places = lines.skip(1).map(line -> Arrays.asList(line.split(","))).collect(Collectors.toList());
places.forEach(place -> new PlanetBuilder().createPlanet());
} catch (IOException e) {
e.printStackTrace();
}
return planets;
}
This is the CSV file.
planet_name,inhabitants,has_stargate,has_dhd,teams_visited
Abydos,0,True,False,[sg-1]
Adara II,117778380,True,True,[sg-1]
Adora,0,False,False,[sg-1]
Alaris,13663,True,True,[sg-1]
Alebran's planet,186402846,False,False,[sg-1; sg-6]
Alpha Site,460849025,True,True,[sg-1; sg-4]
Altair,8087,False,False,[sg-1; sg-2]
Amon Shek,0,True,True,[sg-1; sg-9]
Amra,467925616,True,False,[sg-1]
Apophis's training camp,10188,True,True,[sg-1]
Ardena,13693,True,True,[sg-1; sg-5]
Argos,0,True,True,[sg-1]
Arkad's planet,257318689,True,False,[sg-1; sg-14]
Arkhan's planet,464136466,False,False,[sg-1]
As well as my Planet class and Planetbuilder class
public class Planet {
private String name;
private long inhabitants;
private boolean stargateAvailable;
private boolean dhdAvailable;
private List<String> teamsVisited;
public Planet(String name, long inhabitants, boolean stargateAvailable, boolean dhdAvailable, List<String> teamsVisited) {
}
public String getName() {
return name;
}
public long getInhabitants() {
return inhabitants;
}
public boolean isStargateAvailable() {
return stargateAvailable;
}
public boolean isDhdAvailable() {
return dhdAvailable;
}
public List<String> getTeamsVisited() {
return teamsVisited;
}
@Override
public String toString() {
return name;
}
public class PlanetBuilder {
private String name;
private long inhabitants;
private boolean stargateAvailable;
private boolean dhdAvailable;
private List<String> teamsVisited;
public PlanetBuilder Name(String name) {
this.name = name;
return this;
}
public PlanetBuilder Inhabitants(long inhabitants) {
this.inhabitants = inhabitants;
return this;
}
public PlanetBuilder StargateAvailable(boolean stargateAvailable) {
this.stargateAvailable = stargateAvailable;
return this;
}
public PlanetBuilder DhdAvailable(boolean dhdAvailable) {
this.dhdAvailable = dhdAvailable;
return this;
}
public PlanetBuilder TeamsVisited(List<String> teamsVisited) {
this.teamsVisited = teamsVisited;
return this;
}
public Planet createPlanet() {
return new Planet(name, inhabitants, stargateAvailable, dhdAvailable, teamsVisited);
}