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

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Modifier;
  6. import java.nio.charset.StandardCharsets;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9. public class PasswordManager implements PasswordManagerInterface {
  10. boolean running = true;
  11. InputStream inputStream = System.in;
  12. OutputStream outputStream = System.out;
  13. public static void main(String[] args) {
  14. PasswordManager pm = new PasswordManager();
  15. while (pm.running) {
  16. pm.showMenu();
  17. }
  18. System.exit(0);
  19. }
  20. public PasswordManager() {
  21. println("Hello World");
  22. }
  23. @Override
  24. public void listVaults() {
  25. println("Vaults:");
  26. }
  27. private void println(String output) {
  28. try {
  29. outputStream.write((output + "\n").getBytes(StandardCharsets.UTF_8));
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. @Override
  35. public void openVault() {
  36. }
  37. @Override
  38. public void showMenu() {
  39. StringBuilder sb = new StringBuilder();
  40. Scanner scan = new Scanner(inputStream);
  41. running = true;
  42. sb.append("\nciip Gruppe 8 - Password Manager\n\n");
  43. sb.append("Menu:\n");
  44. sb.append("- list vaults: l:\n");
  45. sb.append("- select vault: v x (replace x with vault id)\n");
  46. sb.append("- exit: e\n");
  47. println(sb.toString());
  48. String input = scan.nextLine();
  49. if (input.equals("e")) {
  50. running = false;
  51. } else if (input.equals("l")) {
  52. listVaults();
  53. } else if (input.startsWith("v")) {
  54. String vaultAction = (input.replace('v', ' ')).trim();
  55. int vaultId = -1;
  56. try {
  57. vaultId = Integer.parseInt(vaultAction);
  58. } catch (NumberFormatException e) {
  59. println("-- please enter a correct vault id");
  60. }
  61. if (vaultId >= 0) {
  62. try {
  63. int internalOptionCounter = 0;
  64. ArrayList<String> options = new ArrayList<>();
  65. for (Method m : Class.forName("Vault").getDeclaredMethods()) { // returns all declared methods including private or protected
  66. try {
  67. if (Modifier.isPublic(this.getClass().getDeclaredMethod(m.getName()).getModifiers())) { // filter out methods that are not public (using Modifier) or have arguments (using getDeclaredMethod)
  68. options.add(m.getName());
  69. println(internalOptionCounter + " - " + m.getName());
  70. internalOptionCounter++;
  71. }
  72. } catch (NoSuchMethodException ignore) {
  73. // println("No method with name " + m.getName() + " found");
  74. }
  75. }
  76. } catch (ClassNotFoundException e) {
  77. println("-- vaults not implemented yet");
  78. }
  79. }
  80. }
  81. }
  82. }