import java.io.*; 
class ArraysTutorial {
	public static void main(String args[]) throws Exception{
		// get set to do some input 
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 

		// instantiates the array itself. No Residents exist yet!
		Resident[] friend = new Resident[10];
	
		// so far we have no elements in our array 
		int numFriends = 0; 
			// the number of friends represented in Resident[]
			// Resident[0]...Resident[numFriends-1] are all friends
			// represented in Resident
	
		// add an element directly to show how 
		// it was done for earlier steps 
		// 
		// Notice how numFriends is incremented AFTER it is 
		// used so it acts as the index for the array AND as 
		// the total elements added so far 

		friend[numFriends++] = new Resident("Jean","Phase V", 252);
	
		System.out.println("Please enter a name"); String name = in.readLine();
	
		while ( ! name.equals("quit")) {
			// first get the information from the user 
			System.out.println("Please enter a residence building"); 
			String building = in.readLine(); 
			System.out.println("Please enter a room number"); 
			int room = Integer.parseInt(in.readLine());
			
			// next instantiate the element 
			Resident toInsert = new Resident(name,building,room);
			
			// if toInsert's room is already occupied, change the occupant to toInsert
			// else if toInsert is already in friend, don't add them
			// else toInsert is not mentioned in friend, so add it at the end

			boolean isFound=false; // false means not mentioned, 
				// true means either found or room occupied means room occupied
				// isFound==true is dealt with inside the for loop.
			
			for(int i=0;i<numFriends;i++){
				if(toInsert.equals(friend[i])){
					System.out.println("Already in list");
					isFound=true;
					break;
				} else if(toInsert.roomEquals(friend[i])){
					System.out.println("Room already occupied - changing name to "+toInsert.getName());
					friend[i].setName(toInsert.getName());
					isFound=true;
					break;
				}
			}
			if(!isFound)friend[numFriends++] = toInsert;
			
			System.out.println("Please enter a name - quit to stop"); 
			name = in.readLine(); 
		}
		
		// If you actually run this code. It is nice to see 
		// the printout of the entire friend list 
		for (int i=0; i<numFriends; i++)System.out.println(friend[i]);
	}
}
		

