package data.trainnetwork;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;

import org.joda.time.LocalTime;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import util.JSONTools;

import data.MissingParameterException;

public class Route extends LinkedList<SectionInfo> {
	
	private String id;
	private int maxPassengers = -1;
	
	public Route(String id) {
		this.id = id;
	}
	
	/**
	 * Set the maximum number of passengers a train can take on this route 
	 * @param max The maximum number of passengers
	 */
	public void setMaxPassengers(int max) {
		maxPassengers = max;
	}
	
	/**
	 * Get the maximum number of passengers the train can take on this route
	 * @return The maximum number of passengers
	 */
	public int getMaxPassengers() {
		return maxPassengers;
	}
	
	/**
	 * Create a {@link Route} from a {@link JSONObject}
	 * @param object The {@link JSONObject} containing the route information 
	 * @return The created {@link Route}
	 * @throws MissingParameterException Thrown in case a required parameter is not defined
	 */
	public static Route getRouteFromJSON(JSONObject object) throws MissingParameterException {
		String routeID = (String) JSONTools.getParameter(object, "routeID");
		int maxPassengers = (int)(long)(Long) JSONTools.getParameter(object, "maxPassengers");
		
		Route route = new Route(routeID);
		route.setMaxPassengers(maxPassengers);
		
		JSONArray rawSections = (JSONArray) JSONTools.getParameter(object, "route");
		for(Object entry : rawSections) {
			JSONObject rawSection = (JSONObject) entry;
			
			Section section = Section.getSectionFromJSON(rawSection, routeID);
			section.setMaxPassengers(maxPassengers);
			
			route.add(
				new SectionInfo(
					section,
					(String) JSONTools.getParameter(rawSection, "start"), 
					(String) JSONTools.getParameter(rawSection, "end")));
		}

		return route;
	}
	
	public static void main(String[] args) {
		JSONParser parser = new JSONParser();
		try {
			Object obj = parser.parse(new FileReader("/Users/balazspete/Desktop/test.json"));
			getRouteFromJSON((JSONObject) obj);
		} catch (MissingParameterException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

@@ -135,78 +135,20 @@
 til.
-HashMap;%0Aimport java.util.LinkedList;%0A%0Aimport org.joda.time.LocalTime;
+LinkedList;%0A
 %0Aimp
@@ -1995,578 +1995,6 @@
 %0A%09%7D%0A
-%09%0A%09public static void main(String%5B%5D args) %7B%0A%09%09JSONParser parser = new JSONParser();%0A%09%09try %7B%0A%09%09%09Object obj = parser.parse(new FileReader(%22/Users/balazspete/Desktop/test.json%22));%0A%09%09%09getRouteFromJSON((JSONObject) obj);%0A%09%09%7D catch (MissingParameterException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (FileNotFoundException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (ParseException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

Remove test method