You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

71 lines
1.1 KiB

  1. /*
  2. * Written by : Bin Hong Lee
  3. * Last edited : 6/7/2016
  4. */
  5. import java.util.*;
  6. class UserFactory
  7. {
  8. private List<User> users = new ArrayList<User>();
  9. private int id;
  10. public UserFactory()
  11. {
  12. id = 0;
  13. }
  14. public User newUser(String name, int limit)
  15. {
  16. User temp = new User(name, id, limit);
  17. users.add(temp);
  18. id++;
  19. return temp;
  20. }
  21. public User getUser(String name)
  22. {
  23. for (int i = 0; i < users.size(); i++)
  24. {
  25. User temp = users.get(i);
  26. if(temp.getName() == name)
  27. {
  28. return temp;
  29. }
  30. }
  31. throw new NullPointerException();
  32. }
  33. public User getUser(int index)
  34. {
  35. for (int i = 0; i < users.size(); i++)
  36. {
  37. User temp = users.get(i);
  38. if(temp.getId() == index)
  39. {
  40. return temp;
  41. }
  42. }
  43. throw new NullPointerException();
  44. }
  45. public void update(User oldUser, User newUser)
  46. {
  47. for (int i = 0; i < users.size(); i++)
  48. {
  49. User temp = users.get(i);
  50. if(temp.getId() == oldUser.getId())
  51. {
  52. users.set(i, newUser);
  53. }
  54. }
  55. }
  56. }