駅の距離を計算する


public final class Demo {
	
	private static int[] miles = new int[5];
	private static String[] stations = {"S1","S2","S3","S4","S5","S6"};
	
	public static void main(String[] args)
	{
		Station s1 = new Station("S6", "S5", 5);
		Station s2 = new Station("S1", "S2", 2);
		Station s3 = new Station("S4", "S3", 3);
		Station s4 = new Station("S2", "S3", 6);
		Station s5 = new Station("S4", "S5", 10);
		Station[] s = {s1,s2,s3,s4,s5};
		getMiles(s);
	}

	public static void getMiles(Station[] s)
	{
	    for(int i=0; i<s.length; i++)
	    {
	    	int p = Integer.valueOf(s[i].getPreStation().substring(1, s[i].getPreStation().length())).intValue();
	    	int a = Integer.valueOf(s[i].getAftStation().substring(1, s[i].getAftStation().length())).intValue();
	    	System.out.println(p+" "+a);
	    	if(p>a)
	    	{
	    		for(int j=0; j<stations.length; j++)
		    	{
	    			if(s[i].getAftStation().equals(stations[j]))
	    			{
	    				miles[a-1] = s[i].getDistance();
	    				System.out.println("p>a:"+(a-1));
	    				break;
	    			}
		    	}
	    	}
	    	else
	    	{
	    		for(int j=0; j<stations.length; j++)
		    	{
	    			if(s[i].getPreStation().equals(stations[j]))
	    			{
	    				miles[p-1] = s[i].getDistance();
	    				System.out.println("a>p:"+(p-1));
	    				break;
	    			}
		    	}
	    	}
	    }
	    for(int w=0; w<miles.length; w++)
    	{
    		System.out.println(miles[w]);
    	}
	}
}


 
public class Station {

	private String preStation;
	private String aftStation;
	private int distance;

	public Station(String preStation, String aftStation, int distance) {
		this.preStation = preStation;
		this.aftStation = aftStation;
		this.distance = distance;
	}

	public String getPreStation() {
		return preStation;
	}

	public void setPreStation(String preStation) {
		this.preStation = preStation;
	}

	public String getAftStation() {
		return aftStation;
	}

	public void setAftStation(String aftStation) {
		this.aftStation = aftStation;
	}

	public int getDistance() {
		return distance;
	}

	public void setDistance(int distance) {
		this.distance = distance;
	}

}