/** Prints menu options. */ public void run() { while (true) { switch (showMenu()) { case PRINT: printRegister(); break; case ADD: addToRegister(); break; case UPDATE: updateRegister(); break; case REMOVE: removeFromRegister(); break; case FIND: findInRegister(); break; case SAVE: chooseSaveDestination(); break; case EXIT: save(); JPALoader.close(); return; } } }
/** * Loads every type of save register and shows which of them are available. Asks User in console * to choose load destination */ private void loadRegisterFrom() { int index = 0; int i = 0; String[] loader = new String[6]; String chosenLoader = ""; if (databaseLoader.load() != null) { loader[++i] = "JDBC Database"; } if (JPALoader.load() != null) { loader[++i] = "JPA Database"; } if (textfileLoader.load() != null) { loader[++i] = "TextFile"; } if (fileLoader.load() != null) { loader[++i] = "File (binary)"; } if (i == 0) { out.println( "There wasn't found any register. You have to create new one\n------------------------"); this.register = createRegister(); this.saveDestination = SaveOption.NONE; return; } out.println("There was found saved register, what do you want to load ?"); for (int j = 1; j <= i; j++) { out.println(j + ". " + loader[j]); } loader[++i] = "Create"; out.println(i + ". Create new Register"); do { try { index = Integer.parseInt(readLine()); } catch (NumberFormatException e) { System.err.println("Wrong format"); } if (index < 1 || index > i) { System.err.println("Wrong index"); continue; } try { chosenLoader = loader[index]; } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Wrong index"); } if (chosenLoader == "JDBC Database") { this.register = databaseLoader.load(); this.saveDestination = SaveOption.DATABASE; return; } if (chosenLoader == "JPA Database") { this.register = JPALoader.load(); this.saveDestination = SaveOption.DATABASEJPA; return; } if (chosenLoader == "TextFile") { this.register = textfileLoader.load(); this.saveDestination = SaveOption.TEXTFILE; return; } if (chosenLoader == "File (binary)") { this.register = fileLoader.load(); this.saveDestination = SaveOption.FILE; return; } if (chosenLoader == "Create") { this.register = createRegister(); this.saveDestination = SaveOption.NONE; return; } } while (index < 1 || index > i); }