package calc;
import java.lang.*;
import java.util.*;

public class CellId implements Comparable<CellId>{
	private char x;
	private int y;
	
	public CellId(char x, int y){
		//System.out.println(""+x+y);
		this.x=x;
		this.y=y;
	}
	
	public static CellId fromString(String s){
		//3ICE: s.substring(0, 1) would return string, not char.
		//substring() function with an argument of 1 to get the substring from position 1 (after the first character) to the end of the string (leaving the second argument out defaults to the full length of the string).
		try{
			//3ICE: System.out.println(s);
			if(s.length()<2)return null;//3ICE: Fixed.
			char c = s.charAt(0);
			int i = Integer.parseInt(s.substring(1));
			if(i>0 && c>='A' && c<='Z')return new CellId(s.charAt(0), i);
		}catch(Exception ex){
			return null;
		}
		return null;
	}
	
	@Override
	public boolean equals(Object that){
		if(that==null){return false;}//System.out.println("null");
		if(that.getClass()!=this.getClass()){return false;}//System.out.println("wrong type");
		//3ICE: vagy:
		if(that instanceof CellId){
			//System.out.println("right type "+(this.x==((CellId)that).x&&this.y==((CellId)that).y)+"  "+this.x+this.y+" "+((CellId)that).x+((CellId)that).y);
			return this.x==((CellId)that).x&&this.y==((CellId)that).y;
		}
		System.out.println("fall through");
		return false;
	}
	@Override
	public int hashCode(){
		return x*100+y;
	}
		public int compareTo(CellId that){
		if(this.equals(that))return 0;
		else if(this.x==that.x)return this.y<that.y?1:-1;
		return this.x<that.x?1:-1;
	}
}
