diff --git a/src/main/java/Credential.java b/src/main/java/Credential.java new file mode 100644 index 0000000..506fdb7 --- /dev/null +++ b/src/main/java/Credential.java @@ -0,0 +1,31 @@ +public class Credential { + + private String name, password; + private int id; + + + public Credential(String name, String password, int id) throws Exception { + if(name == null || password == null) + throw new Exception("kein leeres Objekt erstellen"); + + this.name = name; + this.password = password; + } + + public String getName() + { + return this.name; + } + public String getPassword() { return this.password; } + public int getId() { return this.id; } + + public void upDatePassword(String newPassword) + { + this.password = newPassword; + } + + public void updateUsername(String newUsername) + { + this.name = newUsername; + } +} diff --git a/src/main/java/CredentialInterface.java b/src/main/java/CredentialInterface.java deleted file mode 100644 index f6e0ead..0000000 --- a/src/main/java/CredentialInterface.java +++ /dev/null @@ -1,11 +0,0 @@ -public interface CredentialInterface { - - public void show(); - public void edit(); - public void delete(); - public void getCredentials(); - public void setCredentials(); - public void getAsJSON(); - public void loadCredentialsFromJSON(); - -} diff --git a/src/main/java/CredentialRepository.java b/src/main/java/CredentialRepository.java new file mode 100644 index 0000000..6a048cc --- /dev/null +++ b/src/main/java/CredentialRepository.java @@ -0,0 +1,116 @@ +import java.util.ArrayList; + +public class CredentialRepository implements CredentialRepositoryInterface{ + + private int idCounter = 0; + private ArrayList credentials; + + public CredentialRepository() + { + this.credentials = new ArrayList(); + } + + 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(); + } + + @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; + } + + + @Override + public Credential getCredentialsViaId(int id) { + + try{ + return this.credentials.get(id); + } + catch (IndexOutOfBoundsException outOfBoundsException) + { + return null; + } + + } + + /* + The next 3 functions assume you already have successfully + pulled a credential from the repository and now want to edit it. + Thus, these functions require you to pass the desired credential's index + */ + + + @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"); + } + + } + + 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"); + } + } + + @Override + public void delete(int index) { + + this.credentials.remove(index); + + } + +/* + this function will be required in the future for catching the left-shift in the arrayList + after one entry was deleted. welp, maybe it won't. can't tell and i've no time atm to + finish this + + + private int mapIndexToListCounter(int needle) { + for(int c = 0; c