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.

100 lines
3.1 KiB

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;
public class PasswordManager implements PasswordManagerInterface {
boolean running = true;
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
public static void main(String[] args) {
PasswordManager pm = new PasswordManager();
while (pm.running) {
pm.showMenu();
}
System.exit(0);
}
public PasswordManager() {
println("Hello World");
}
@Override
public void listVaults() {
println("Vaults:");
}
private void println(String output) {
try {
outputStream.write((output + "\n").getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void openVault() {
}
@Override
public void showMenu() {
StringBuilder sb = new StringBuilder();
Scanner scan = new Scanner(inputStream);
running = true;
sb.append("\nciip Gruppe 8 - Password Manager\n\n");
sb.append("Menu:\n");
sb.append("- list vaults: l:\n");
sb.append("- select vault: v x (replace x with vault id)\n");
sb.append("- exit: e\n");
println(sb.toString());
String input = scan.nextLine();
if (input.equals("e")) {
running = false;
} else if (input.equals("l")) {
listVaults();
} else if (input.startsWith("v")) {
String vaultAction = (input.replace('v', ' ')).trim();
int vaultId = -1;
try {
vaultId = Integer.parseInt(vaultAction);
} catch (NumberFormatException e) {
println("-- please enter a correct vault id");
}
if (vaultId >= 0) {
try {
int internalOptionCounter = 0;
ArrayList<String> options = new ArrayList<>();
for (Method m : Class.forName("Vault").getDeclaredMethods()) { // returns all declared methods including private or protected
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());
println(internalOptionCounter + " - " + m.getName());
internalOptionCounter++;
}
} catch (NoSuchMethodException ignore) {
// println("No method with name " + m.getName() + " found");
}
}
} catch (ClassNotFoundException e) {
println("-- vaults not implemented yet");
}
}
}
}
}