Select Git revision
Journey.java

Quentin Leblanc authored
Journey.java 4.50 KiB
package ch.hepia.api.transport;
import org.json.JSONArray;
import org.json.JSONObject;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
public class Journey {
private String name;
private String category;
private int categoryCode;
private String number;
private String operator;
private String to;
private List<Stop> passList;
private int capacity1st;
private int capacity2nd;
/**
* Empty Journey
*/
private final static class EmptyJourney extends Journey{
/**
* Constructor of EmptyJourney
* No Information and no Stops in the Journey
* @throws ParseException
*/
private EmptyJourney() throws ParseException {
super("", "", 0, "", "", "",
new ArrayList<>(), 0, 0);
}
}
/**
* Constructor of the Journey Object
* @param name
* @param category what transport category the journey is
* @param categoryCode
* @param number
* @param operator which company operates the journey
* @param to destination of the journey
* @param passList Where the journey will pass through
* @param capacity1st number of seats inst class
* @param capacity2nd number of seats in nd class
*/
private Journey(String name, String category, int categoryCode, String number, String operator, String to, List<Stop> passList, int capacity1st, int capacity2nd){
this.name = name;
this.category = category;
this.categoryCode = categoryCode;
this.number = number;
this.operator = operator;
this.to = to;
this.passList = new ArrayList<>(passList);
this.capacity1st = capacity1st;
this.capacity2nd = capacity2nd;
}
/**
* Get the name of the journey
* @return name
*/
public String getName(){
return name;
}
/**
* Get the Category of the transport of the Journey
* @return category
*/
public String getCategory(){
return category;
}
/**
* Get the categoryCode affiliated with the Journey
* @return categoryCode
*/
public int getCategoryCode(){
return categoryCode;
}
/**
* Get where the Journey is headed to
* @return to
*/
public String getTo(){
return to;
}
/**
* Get all the Stops where the journey will pass through
* @return passList
*/
public List<Stop> getPassList(){
return new ArrayList<>(passList);
}
/**
* Builder of Journey object
*/
public final static class JourneyBuilder{
private JSONObject datas;
/**
* Constructor of the class JourneyBuilder
* Stock Datas from a JSON object given
* @param datas contains datas to construct a Journey object
*/
public JourneyBuilder(JSONObject datas){
this.datas = datas;
}
/**
* Build a new Journey object from the datas obtained
* @return a new Journey Object
* @throws ParseException
*/
public Journey build() throws ParseException {
if(datas.isEmpty()){
return new EmptyJourney();
}
String name = datas.isNull("name") ? "" : datas.getString("name");
String category = datas.isNull("category") ? "" : datas.getString("category");
int categoryCode = datas.isNull("categoryCode") ? 0 : datas.getInt("categoryCode");
String number = datas.isNull("number") ? "" : datas.getString("number");
String operator = datas.isNull("operator") ? "" : datas.getString("operator");
String to = datas.isNull("to") ? "" : datas.getString("to");
JSONArray passListJSON = datas.getJSONArray("passList");
List<Stop> passList = new ArrayList<>();
passListJSON.forEach( o -> {
JSONObject k = (JSONObject) o;
Stop.StopBuilder stop = new Stop.StopBuilder(k);
try {
passList.add(stop.build());
} catch (ParseException e) {
e.printStackTrace();
}
});
int capacity1st = datas.isNull("capacity1st") ? 0 : datas.getInt("capacity1st");
int capacity2nd = datas.isNull("capacity2nd") ? 0 : datas.getInt("capacity2nd");
return new Journey(name, category, categoryCode, number, operator, to, passList, capacity1st, capacity2nd);
}
}
}