import objectdraw.*;

import java.io.*;

public class StupidNameWithBadName extends WindowController {
	
	public void begin(){
		String name = "";
		
		// as long the name isn't "exit" keep getting a new name
		while(!name.equals("exit")){
			System.out.print("Enter your name: ");
			
			try{
				name = readName();
				System.out.println(name + ", that's a stupid name");
				System.out.println();
			}catch(IOException e){
				// print out the error and try again
				System.out.println(e.getMessage());
			}catch(BadNameException e){
				System.out.println(e.getMessage());
				System.out.println("Try again...");
			}
		}		
	}
	
	// reads the next line from the command line and returns a name
	// if the name is not a proper name, throws BadNameException
	public String readName() throws IOException, BadNameException{
		StringBuffer name = new StringBuffer();
		
		char next = (char)System.in.read();

		// as long as the user hasn't hit return, keep reading characters.
		while( next != '\n'){
			name.append(next);
			next = (char)System.in.read();
		}
		
		// check if the name input is a valid name, for us, that's just
		// that it doesn't contains any numbers
		for( char c: name.toString().toCharArray() ){
			if( Character.isDigit(c) ){
				throw new BadNameException(name.toString());
			}
		}
		
		return name.toString();
	}
}