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.

123 lines
2.7 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. @Override
  51. public Credential getCredentialsViaId(int id) {
  52. try{
  53. return this.credentials.get(id);
  54. }
  55. catch (IndexOutOfBoundsException outOfBoundsException)
  56. {
  57. return null;
  58. }
  59. }
  60. /*
  61. The next 3 functions assume you already have successfully
  62. pulled a credential from the repository and now want to edit it.
  63. Thus, these functions require you to pass the desired credential's index
  64. */
  65. @Override
  66. public void updatePassword(int index, String newPassword) {
  67. try{
  68. this.credentials.get(index).upDatePassword(newPassword);
  69. }
  70. catch (IndexOutOfBoundsException outOfBoundsException)
  71. {
  72. System.err.println("No Credential with Index" + index + " found");
  73. }
  74. }
  75. public void updateUsername(int index, String newUsername){
  76. try{
  77. this.credentials.get(index).updateUsername(newUsername);
  78. }
  79. catch (IndexOutOfBoundsException outOfBoundsException)
  80. {
  81. System.err.println("No Credential with Index" + index + " found");
  82. }
  83. }
  84. @Override
  85. public void delete(int index) {
  86. this.credentials.remove(index);
  87. }
  88. private int getListSize()
  89. {
  90. return this.credentials.size();
  91. }
  92. }