package example;
public class URLParse {
	private String url;                                                    
    
	URLParse (String url)
	{                                            
	   this.url=url;                                                    
	}                                                                           
	//should be synchronized                                       
	public void parse(String key)                                            
	{                                                                           
    	String val = getVal(key);
    	if(val.equals("Alice"))
    		replaceVal(key,"A");
    	if(val.equals("Bob"))
    		replaceVal(key,"B");
	}
	private void replaceVal(String key, String newVal)
	{
        int keyPos=url.indexOf(key);                             
        int valPos=url.indexOf("=", keyPos)+1;               
        int ampPos=url.indexOf("&", keyPos);                
        if(ampPos<0) ampPos = url.length(); 	               
        url=url.substring(0, valPos) 	                               
                +newVal+url.substring(ampPos); 	       
    }                                                                          
    private String getVal(String key)
    { 	                       
        int keyPos=url.indexOf(key); 	                       
        int valPos=url.indexOf("=", keyPos)+1; 	       
        int ampPos=url.indexOf("&", keyPos); 	               
        if(ampPos<0) ampPos=url.length();
		
        return url.substring(valPos,ampPos);                  
	}
    public String getURL()
    {
    	return url;
    }
}