fazendo os sistemas de clientes e do admin

This commit is contained in:
250413
2026-05-11 11:53:31 +01:00
parent f9bca7c9ca
commit 59ff5e00c5

View File

@@ -537,7 +537,7 @@ private static void procurarClienteAdmin() {
} }
if (!encontrado) { if (!encontrado) {
System.out.println("Cliente não encontrado."); System.out.println("Cliente não encontrado.");
} }
} }
@@ -550,7 +550,7 @@ private static void editarClienteAdmin() {
String[] dadosCliente = procurarClientePorId(id); String[] dadosCliente = procurarClientePorId(id);
if (dadosCliente == null) { if (dadosCliente == null) {
System.out.println("Cliente não encontrado!"); System.out.println("Cliente não encontrado!");
return; return;
} }
@@ -567,10 +567,10 @@ private static void editarClienteAdmin() {
if (!telefoneJaExiste(novoTelefone) || dadosCliente[2].equals(novoTelefone)) { if (!telefoneJaExiste(novoTelefone) || dadosCliente[2].equals(novoTelefone)) {
dadosCliente[2] = novoTelefone; dadosCliente[2] = novoTelefone;
} else { } else {
System.out.println("Telefone já existe! Mantido o original."); System.out.println("Telefone já existe! Mantido o original.");
} }
} else { } else {
System.out.println("Telefone inválido! Mantido o original."); System.out.println("Telefone inválido! Mantido o original.");
} }
} }
@@ -579,11 +579,11 @@ private static void editarClienteAdmin() {
if (!novoEmail.isEmpty() && verificarEmail(novoEmail)) { if (!novoEmail.isEmpty() && verificarEmail(novoEmail)) {
dadosCliente[3] = novoEmail; dadosCliente[3] = novoEmail;
} else if (!novoEmail.isEmpty()) { } else if (!novoEmail.isEmpty()) {
System.out.println("Email inválido! Mantido o original."); System.out.println("Email inválido! Mantido o original.");
} }
atualizarClienteCompleto(dadosCliente); atualizarClienteCompleto(dadosCliente);
System.out.println("Cliente atualizado com sucesso!"); System.out.println("1Cliente atualizado com sucesso!");
} }
private static void removerCliente() { private static void removerCliente() {
@@ -734,7 +734,7 @@ private static void atualizarClienteCompleto(String[] dados) {
return false; return false;
} }
// Separar os dígitos
int d1 = Character.getNumericValue(nif.charAt(0)); int d1 = Character.getNumericValue(nif.charAt(0));
int d2 = Character.getNumericValue(nif.charAt(1)); int d2 = Character.getNumericValue(nif.charAt(1));
int d3 = Character.getNumericValue(nif.charAt(2)); int d3 = Character.getNumericValue(nif.charAt(2));
@@ -745,7 +745,7 @@ private static void atualizarClienteCompleto(String[] dados) {
int d8 = Character.getNumericValue(nif.charAt(7)); int d8 = Character.getNumericValue(nif.charAt(7));
int d9 = Character.getNumericValue(nif.charAt(8)); int d9 = Character.getNumericValue(nif.charAt(8));
// Cálculo do dígito de controlo
int produto = (d8 * 2) + (d7 * 3) + (d6 * 4) + (d5 * 5) + int produto = (d8 * 2) + (d7 * 3) + (d6 * 4) + (d5 * 5) +
(d4 * 6) + (d3 * 7) + (d2 * 8) + (d1 * 9); (d4 * 6) + (d3 * 7) + (d2 * 8) + (d1 * 9);
int resto = produto % 11; int resto = produto % 11;
@@ -757,7 +757,7 @@ private static void atualizarClienteCompleto(String[] dados) {
digitoControlo = 11 - resto; digitoControlo = 11 - resto;
} }
// Verificar se o dígito de controlo está correto
if (digitoControlo == d9) { if (digitoControlo == d9) {
return true; return true;
} else { } else {
@@ -864,7 +864,7 @@ public static void editarDinheiroCliente() {
String novoNIF = scanner.nextLine(); String novoNIF = scanner.nextLine();
if (!novoNIF.isEmpty()) nif[indiceEditar] = novoNIF; if (!novoNIF.isEmpty()) nif[indiceEditar] = novoNIF;
// Reescrever o ficheiro com os dados atualizados
StringBuilder conteudoAtualizado = new StringBuilder(); StringBuilder conteudoAtualizado = new StringBuilder();
for (int i = 0; i < nome.length; i++) { for (int i = 0; i < nome.length; i++) {
conteudoAtualizado.append(nome[i]).append(",") conteudoAtualizado.append(nome[i]).append(",")
@@ -908,33 +908,33 @@ public static void editarDinheiroCliente() {
int indice = random.nextInt(CARACTERES.length()); int indice = random.nextInt(CARACTERES.length());
idNum.append(CARACTERES.charAt(indice)); idNum.append(CARACTERES.charAt(indice));
} }
// ver se id já existe
String idTxt = idNum.toString(); String idTxt = idNum.toString();
if(!idJaExiste(idTxt)){ if(!idJaExiste(idTxt)){
return idTxt; return idTxt;
// } }
} }
} }
private static boolean nifJaExiste(String nif) { private static boolean nifJaExiste (String nif) {
String nomeFicheiro = "clientes.txt"; String nomeFicheiro = "clientes.txt";
// 1. Começamos assumindo que NÃO existe
try (BufferedReader reader = new BufferedReader(new FileReader(nomeFicheiro))) { try (BufferedReader reader = new BufferedReader(new FileReader(nomeFicheiro))) {
String linha; String linha;
while ((linha = reader.readLine()) != null) { while ((linha = reader.readLine()) != null) {
String[] campos = linha.split(","); String[] campos = linha.split(",");
// 2. Verificamos se a linha tem dados e se o NIF coincide
if (campos.length > 0 && campos[0].trim().equals(nif.trim())) { if (campos.length > 0 && campos[0].trim().equals(nif.trim())) {
return true; // Encontrou? Retorna imediatamente e fecha o reader return true;
} }
} }
} catch (IOException e) { } catch (IOException e) {
// Opcional: imprimir erro para saber por que a leitura falhou
System.err.println("Erro ao ler o ficheiro: " + e.getMessage()); System.err.println("Erro ao ler o ficheiro: " + e.getMessage());
} }
return false; // Se percorreu tudo e não achou, retorna false return false;
} }
@@ -1020,7 +1020,7 @@ private static boolean nifJaExiste(String nif) {
} while (!verificarNIF(nif) || nif.isEmpty()); } while (!verificarNIF(nif) || nif.isEmpty());
String telefone; String telefone;
do {
System.out.print("║ Introduza telefone (9 dígitos): "); System.out.print("║ Introduza telefone (9 dígitos): ");
telefone = scanner.nextLine(); telefone = scanner.nextLine();
@@ -1031,20 +1031,20 @@ private static boolean nifJaExiste(String nif) {
System.out.println("║ Por favor, insira um telefone diferente."); System.out.println("║ Por favor, insira um telefone diferente.");
telefone = ""; telefone = "";
} }
} while (telefone.isEmpty() || telefone.length() != 9 || !telefone.matches("\\d+")); while (telefone.isEmpty() || telefone.length() != 9 || !telefone.matches("\\d+"));
String email; String email;
do {
System.out.print("║ Introduza email: "); System.out.print("║ Introduza email: ");
email = scanner.nextLine(); email = scanner.nextLine();
if (!verificarEmail(email)) { if (!verificarEmail(email)) {
System.out.println("║ Email inválido! Deve conter @ e .pt ou .com"); System.out.println("║ Email inválido! Deve conter @ e .pt ou .com");
} }
} while (!verificarEmail(email)); while (!verificarEmail(email));
double saldo; double saldo;
do {
System.out.print("║ Introduza saldo (mínimo 60€): "); System.out.print("║ Introduza saldo (mínimo 60€): ");
saldo = scanner.nextDouble(); saldo = scanner.nextDouble();
scanner.nextLine(); // limpar buffer scanner.nextLine(); // limpar buffer
@@ -1052,7 +1052,7 @@ private static boolean nifJaExiste(String nif) {
if (saldo < 60) { if (saldo < 60) {
System.out.println("║ Saldo mínimo insuficiente! Mínimo 60€."); System.out.println("║ Saldo mínimo insuficiente! Mínimo 60€.");
} }
} while (saldo < 60); while (saldo < 60);
String saldoTxt = Double.toString(saldo); String saldoTxt = Double.toString(saldo);