
/*
	Code that throws a checked exception must be called in a try-catch-finally block.
	Code that throws an unchecked exception does not.

	Classes that extend Exception are checked. 
	Classes that extend RuntimeException are unchecked and when thrown will 
		cause the program to terminate.  This should not happen.

	Use Exception for recoverable (user) errors
	Use RuntimeException for unrecoverable (programming) errors

	Common classes that extend Exception
		IOException
		URISyntaxExpression

	Common classes that extend RuntimeException: 
		IndexOutOfBoundsException,  
		IllegalArgumentException,
		NoSuchElementException,
		NullPointerException,
		NegativeArraySizeException

*/

import java.util.Scanner;

class Nov7 {

	private static User[] users = new User[3];

	public static void main(String[] args) {

		users[0] = new User("Joe");
		users[1] = new User("Kim");
		users[2] = new User("Lee");

		for(int i = 0; i < users.length; i++) {
			User user = getUser(i);
			System.out.println(user.toString());
		}

		Scanner kb = new Scanner(System.in);
		User user = null;
		
		do {
			System.out.println("> Please log in");

			String name = kb.next();

			try {
				user = getUser(name);
			}
			catch(UserNotFoundException e) {
				System.out.println("> Invalid username");
			}

		}while(user == null);

		System.out.println("> Hello " + user.username);
		System.out.println("> Would you like to play a game?");

	}

	static User getUser(int index) {
		if (index < 0 || index >= users.length) {
			throw new IndexOutOfBoundsException();
		}
	
		return users[index];
	}

	static User getUser(String username) throws UserNotFoundException {
		for(User user : users) {
			if (user.username.equals(username)) {
				return user;
			}
		}
		throw new UserNotFoundException();		
	}



} // End of class
