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.

138 lines
3.0 KiB

  1. import java.util.ArrayList;
  2. /**
  3. * Credential Repository for handling user credentials
  4. * @author Claudia Metzler
  5. */
  6. public class CredentialRepository implements CredentialRepositoryInterface{
  7. private int idCounter = 0;
  8. private ArrayList<Credential> credentials;
  9. /**
  10. * Konstruktor
  11. *
  12. * Initializes instance variable ArrayList
  13. */
  14. public CredentialRepository()
  15. {
  16. this.credentials = new ArrayList<Credential>();
  17. }
  18. /**
  19. * Create a new Set of User credentials
  20. * @param name
  21. * @param password
  22. * @return size of repositoy - arraylist for checking
  23. */
  24. public int createNewCredential(String name, String password)
  25. {
  26. try {
  27. this.credentials.add(new Credential(name, password, this.idCounter++));
  28. }
  29. catch(Exception fail)
  30. {
  31. System.err.println(fail.getMessage());
  32. }
  33. return this.getListSize();
  34. }
  35. /**
  36. * Function to return a specific Credential via Name input
  37. * @param needle [ aka to-search-for ]
  38. * @return usercredential | NULL
  39. */
  40. @Override
  41. public Credential getCredentialsViaName(String needle) {
  42. for(int c = 0; c < this.getListSize(); c++)
  43. {
  44. Credential credential = this.credentials.get(c);
  45. if(credential.getName().equals(needle))
  46. return credential;
  47. }
  48. return null;
  49. }
  50. /**
  51. * Function to return a specific Credential via ID
  52. * @param id
  53. * @return usercredential | NULL
  54. */
  55. @Override
  56. public Credential getCredentialsViaId(int id) {
  57. try{
  58. return this.credentials.get(id);
  59. }
  60. catch (IndexOutOfBoundsException outOfBoundsException)
  61. {
  62. return null;
  63. }
  64. }
  65. /**
  66. * Function to update userpassword, assuming we already know the userid
  67. * @param index
  68. * @param newPassword
  69. */
  70. @Override
  71. public void updatePassword(int index, String newPassword) {
  72. try{
  73. this.credentials.get(index).upDatePassword(newPassword);
  74. }
  75. catch (IndexOutOfBoundsException outOfBoundsException)
  76. {
  77. System.err.println("No Credential with Index" + index + " found");
  78. }
  79. }
  80. /**
  81. * Function to update username analoge to updatePassword
  82. * @param index
  83. * @param newUsername
  84. */
  85. public void updateUsername(int index, String newUsername){
  86. try{
  87. this.credentials.get(index).updateUsername(newUsername);
  88. }
  89. catch (IndexOutOfBoundsException outOfBoundsException)
  90. {
  91. System.err.println("No Credential with Index" + index + " found");
  92. }
  93. }
  94. /**
  95. * Deletion function for credentials
  96. * Will immediately delete - double check for user permission
  97. * @param index
  98. */
  99. @Override
  100. public void delete(int index) {
  101. this.credentials.remove(index);
  102. }
  103. private int getListSize()
  104. {
  105. return this.credentials.size();
  106. }
  107. }