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.

99 lines
2.2 KiB

  1. import java.util.ArrayList;
  2. public class CredentialRepository implements CredentialRepositoryInterface{
  3. private int idCounter = 0;
  4. private ArrayList<Credential> credentials;
  5. public CredentialRepository()
  6. {
  7. this.credentials = new ArrayList<Credential>();
  8. }
  9. public int createNewCredential(String name, String password)
  10. {
  11. try {
  12. this.credentials.add(new Credential(name, password, this.idCounter++));
  13. }
  14. catch(Exception fail)
  15. {
  16. System.err.println(fail.getMessage());
  17. }
  18. return this.getListSize();
  19. }
  20. @Override
  21. public Credential getCredentialsViaName(String needle) {
  22. for(int c = 0; c < this.getListSize(); c++)
  23. {
  24. Credential credential = this.credentials.get(c);
  25. if(credential.getName().equals(needle))
  26. return credential;
  27. }
  28. return null;
  29. }
  30. @Override
  31. public Credential getCredentialsViaId(int id) {
  32. try{
  33. return this.credentials.get(id);
  34. }
  35. catch (IndexOutOfBoundsException outOfBoundsException)
  36. {
  37. return null;
  38. }
  39. }
  40. /*
  41. The next 3 functions assume you already have successfully
  42. pulled a credential from the repository and now want to edit it.
  43. Thus, these functions require you to pass the desired credential's index
  44. */
  45. @Override
  46. public void updatePassword(int index, String newPassword) {
  47. try{
  48. this.credentials.get(index).upDatePassword(newPassword);
  49. }
  50. catch (IndexOutOfBoundsException outOfBoundsException)
  51. {
  52. System.err.println("No Credential with Index" + index + " found");
  53. }
  54. }
  55. public void updateUsername(int index, String newUsername){
  56. try{
  57. this.credentials.get(index).updateUsername(newUsername);
  58. }
  59. catch (IndexOutOfBoundsException outOfBoundsException)
  60. {
  61. System.err.println("No Credential with Index" + index + " found");
  62. }
  63. }
  64. @Override
  65. public void delete(int index) {
  66. this.credentials.remove(index);
  67. }
  68. private int getListSize()
  69. {
  70. return this.credentials.size();
  71. }
  72. }