Discussion of Activity 5.4 - Providing the code for the class UserComputers

Here is our sample solution for the class UserComputers.

import java.util.HashMap;
 
class UserComputers
{
   private HashMap<String, String> userTable;

   public UserComputers(int capacity)
   {
      userTable = new HashMap<String, String>(capacity);
   }

   public void addLink(String userName, String computerName)
   {
      userTable.put(userName, computerName);
   }

   public String getComputer(String userName)
   {
      return userTable.get(userName);
   }
 
   public boolean isThereAUser(String userName)
   {
      return userTable.containsKey(userName);
   }

   public boolean isThereAComputer(String computerName)
   {
      return userTable.containsValue(computerName);
   }
}