import java.util.ArrayList; /** * Credential Repository for handling user credentials * @author Claudia Metzler */ public class CredentialRepository implements CredentialRepositoryInterface{ private int idCounter = 0; private ArrayList credentials; /** * Konstruktor * * Initializes instance variable ArrayList */ public CredentialRepository() { this.credentials = new ArrayList(); } /** * Create a new Set of User credentials * @param name * @param password * @return size of repositoy - arraylist for checking */ public int createNewCredential(String name, String password) { try { this.credentials.add(new Credential(name, password, this.idCounter++)); } catch(Exception fail) { System.err.println(fail.getMessage()); } return this.getListSize(); } /** * Function to return a specific Credential via Name input * @param needle [ aka to-search-for ] * @return usercredential | NULL */ @Override public Credential getCredentialsViaName(String needle) { for(int c = 0; c < this.getListSize(); c++) { Credential credential = this.credentials.get(c); if(credential.getName().equals(needle)) return credential; } return null; } /** * Function to return a specific Credential via ID * @param id * @return usercredential | NULL */ @Override public Credential getCredentialsViaId(int id) { try{ return this.credentials.get(id); } catch (IndexOutOfBoundsException outOfBoundsException) { return null; } } /** * Function to update userpassword, assuming we already know the userid * @param index * @param newPassword */ @Override public void updatePassword(int index, String newPassword) { try{ this.credentials.get(index).upDatePassword(newPassword); } catch (IndexOutOfBoundsException outOfBoundsException) { System.err.println("No Credential with Index" + index + " found"); } } /** * Function to update username analoge to updatePassword * @param index * @param newUsername */ public void updateUsername(int index, String newUsername){ try{ this.credentials.get(index).updateUsername(newUsername); } catch (IndexOutOfBoundsException outOfBoundsException) { System.err.println("No Credential with Index" + index + " found"); } } /** * Deletion function for credentials * Will immediately delete - double check for user permission * @param index */ @Override public void delete(int index) { this.credentials.remove(index); } private int getListSize() { return this.credentials.size(); } }