diff --git a/src/main/java/com/mycompany/gestorpalavrapasses/GestorPalavraPasses.java b/src/main/java/com/mycompany/gestorpalavrapasses/GestorPalavraPasses.java index 7eab83d..e71d6eb 100644 --- a/src/main/java/com/mycompany/gestorpalavrapasses/GestorPalavraPasses.java +++ b/src/main/java/com/mycompany/gestorpalavrapasses/GestorPalavraPasses.java @@ -37,19 +37,31 @@ public class GestorPalavraPasses { switch (op) { case 1: - System.out.println("Criar conta..."); + System.out.print("Aplicação/Site: "); + String app = scanner.nextLine(); + System.out.print("Nome/Email: "); + String user = scanner.nextLine(); + System.out.print("Palavra-passe: "); + String pass = scanner.nextLine(); + + Account acc = new Account(app, user, pass); + FileManager.save(acc); break; case 2: - System.out.println("Listar contas..."); + FileManager.readAll(); break; case 3: - System.out.println("Procurar conta..."); + System.out.print("Pesquisar: "); + String termo = scanner.nextLine(); + FileManager.search(termo); break; case 4: - System.out.println("Apagar conta..."); + System.out.print("Apagar (Nome/Email, Palavra-passe): "); + String apagar = scanner.nextLine(); + FileManager.delete(apagar); break; case 5: - System.out.println("Gerar password..."); + System.out.println("Palavra-passe gerada: " + PasswordGenerator.generate()); break; case 0: System.out.println("A sair..."); @@ -65,14 +77,140 @@ public class GestorPalavraPasses { class Account { - + String app; + String username; + String password; + + public Account(String app, String username, String password) { + this.app = app; + this.username = username; + this.password = password; + } } class FileManager { - -} - - class PasswordGenerator { - + + public static void readAll() { + try { + java.io.BufferedReader reader = new java.io.BufferedReader( + new java.io.FileReader("data.txt") + ); + + String line; + + while ((line = reader.readLine()) != null) { + String[] parts = line.split(";"); + System.out.println("\nAplicação/Site: " + parts[0] + + " | Nome/Email: " + parts[1] + + " | Palavra-Passe: " + parts[2]); + } + + reader.close(); + + } catch (Exception e) { + System.out.println("Erro ao ler ficheiro."); + } +} + public static void save(Account acc) { + try { + java.io.BufferedWriter writer = new java.io.BufferedWriter( + new java.io.FileWriter("data.txt", true) + ); + + writer.write(acc.app + ";" + acc.username + ";" + acc.password); + writer.newLine(); + + writer.close(); + + System.out.println("Conta guardada!"); + + } catch (Exception e) { + System.out.println("Erro ao guardar ficheiro."); + } + } + + public static void search(String termo) { + try { + java.io.BufferedReader reader = new java.io.BufferedReader( + new java.io.FileReader("data.txt") + ); + + String line; + boolean encontrado = false; + + while ((line = reader.readLine()) != null) { + if (line.toLowerCase().contains(termo.toLowerCase())) { + String[] parts = line.split(";"); + System.out.println("\nAplicação/Site: " + parts[0] + + " | Nome/Email: " + parts[1] + + " | Palavra-passe: " + parts[2]); + encontrado = true; + } + } + + if (!encontrado) { + System.out.println("Nenhuma conta encontrada."); + } + + reader.close(); + + } catch (Exception e) { + System.out.println("Erro ao procurar."); + } +} + public static void delete(String termo) { + try { + java.io.File inputFile = new java.io.File("data.txt"); + java.io.File tempFile = new java.io.File("temp.txt"); + + java.io.BufferedReader reader = new java.io.BufferedReader( + new java.io.FileReader(inputFile) + ); + + java.io.BufferedWriter writer = new java.io.BufferedWriter( + new java.io.FileWriter(tempFile) + ); + + String line; + boolean apagado = false; + + while ((line = reader.readLine()) != null) { + if (line.toLowerCase().contains(termo.toLowerCase())) { + apagado = true; + continue; + } + writer.write(line); + writer.newLine(); + } + + writer.close(); + reader.close(); + + inputFile.delete(); + tempFile.renameTo(inputFile); + + if (apagado) { + System.out.println("Conta(s) apagada(s)!"); + } else { + System.out.println("Nenhuma conta encontrada."); + } + + } catch (Exception e) { + System.out.println("Erro ao apagar."); + } +} +} +class PasswordGenerator { + public static String generate() { + String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + StringBuilder pass = new StringBuilder(); + java.util.Random rand = new java.util.Random(); + + for (int i = 0; i < 10; i++) { + pass.append(chars.charAt(rand.nextInt(chars.length()))); + } + + return pass.toString(); + } }