|
@ -1,6 +1,7 @@ |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.io.InputStream; |
|
|
import java.io.InputStream; |
|
|
import java.io.OutputStream; |
|
|
import java.io.OutputStream; |
|
|
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
import java.lang.reflect.Method; |
|
|
import java.lang.reflect.Method; |
|
|
import java.lang.reflect.Modifier; |
|
|
import java.lang.reflect.Modifier; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.charset.StandardCharsets; |
|
@ -13,6 +14,8 @@ public class PasswordManager implements PasswordManagerInterface { |
|
|
InputStream inputStream = System.in; |
|
|
InputStream inputStream = System.in; |
|
|
OutputStream outputStream = System.out; |
|
|
OutputStream outputStream = System.out; |
|
|
ArrayList<Vault> vaults = new ArrayList<>(); |
|
|
ArrayList<Vault> vaults = new ArrayList<>(); |
|
|
|
|
|
int selectedVaultId = -1; |
|
|
|
|
|
ArrayList<String> vaultOperationOptions = new ArrayList<>(); |
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
public static void main(String[] args) { |
|
|
PasswordManager pm = new PasswordManager(); |
|
|
PasswordManager pm = new PasswordManager(); |
|
@ -63,7 +66,8 @@ public class PasswordManager implements PasswordManagerInterface { |
|
|
sb.append("Menu:\n"); |
|
|
sb.append("Menu:\n"); |
|
|
sb.append("- list vaults: l\n"); |
|
|
sb.append("- list vaults: l\n"); |
|
|
sb.append("- create new vault: v\n"); |
|
|
sb.append("- create new vault: v\n"); |
|
|
sb.append("- select vault: v x (replace x with vault id)\n"); |
|
|
|
|
|
|
|
|
sb.append("- select vault and show operations: v x (replace x with vault id)\n"); |
|
|
|
|
|
sb.append("- select vault operation: o x (replace o with a operation id for the selected vault)\n"); |
|
|
sb.append("- exit: e\n"); |
|
|
sb.append("- exit: e\n"); |
|
|
|
|
|
|
|
|
println(sb.toString()); |
|
|
println(sb.toString()); |
|
@ -87,14 +91,16 @@ public class PasswordManager implements PasswordManagerInterface { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (vaultId >= 0) { |
|
|
if (vaultId >= 0) { |
|
|
|
|
|
selectedVaultId = vaultId; |
|
|
|
|
|
println("\nVault " + vaultId + " selected."); |
|
|
try { |
|
|
try { |
|
|
int internalOptionCounter = 0; |
|
|
int internalOptionCounter = 0; |
|
|
ArrayList<String> options = new ArrayList<>(); |
|
|
|
|
|
|
|
|
println("\nAvailable vault operations:"); |
|
|
|
|
|
|
|
|
for (Method m : Class.forName("Vault").getDeclaredMethods()) { // returns all declared methods including private or protected |
|
|
for (Method m : Class.forName("Vault").getDeclaredMethods()) { // returns all declared methods including private or protected |
|
|
try { |
|
|
try { |
|
|
if (Modifier.isPublic(this.getClass().getDeclaredMethod(m.getName()).getModifiers())) { // filter out methods that are not public (using Modifier) or have arguments (using getDeclaredMethod) |
|
|
|
|
|
options.add(m.getName()); |
|
|
|
|
|
|
|
|
if (Modifier.isPublic(Class.forName("Vault").getDeclaredMethod(m.getName()).getModifiers())) { // filter out methods that are not public (using Modifier) or have arguments (using getDeclaredMethod) |
|
|
|
|
|
vaultOperationOptions.add(m.getName()); |
|
|
println(internalOptionCounter + " - " + m.getName()); |
|
|
println(internalOptionCounter + " - " + m.getName()); |
|
|
internalOptionCounter++; |
|
|
internalOptionCounter++; |
|
|
} |
|
|
} |
|
@ -106,6 +112,30 @@ public class PasswordManager implements PasswordManagerInterface { |
|
|
println("-- vaults not implemented yet"); |
|
|
println("-- vaults not implemented yet"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} else if (input.startsWith("o")) { |
|
|
|
|
|
String operation = (input.replace('o', ' ')).trim(); |
|
|
|
|
|
int operationId = -1; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
operationId = Integer.parseInt(operation); |
|
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
|
println("-- please enter a valid operation id"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (selectedVaultId < 0) { |
|
|
|
|
|
println("-- a vault needs to be selected"); |
|
|
|
|
|
} else if (operationId >= 0 && operationId < vaultOperationOptions.size()) { |
|
|
|
|
|
Vault vault = vaults.get(selectedVaultId); |
|
|
|
|
|
try { |
|
|
|
|
|
println("Run selected operation '" + vaultOperationOptions.get(operationId) + "':"); |
|
|
|
|
|
vault.getClass().getDeclaredMethod(vaultOperationOptions.get(operationId)).invoke(vault); |
|
|
|
|
|
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
println("-- selected operation failed"); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
println("-- please enter a valid operation id"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |