comeso da introducao da moeda
This commit is contained in:
475
src/main/java/com/mycompany/mavenproject25/Mavenproject25.java
Normal file
475
src/main/java/com/mycompany/mavenproject25/Mavenproject25.java
Normal file
@@ -0,0 +1,475 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
package com.mycompany.mavenproject25;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 250413
|
||||
*/
|
||||
public class Mavenproject25 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
boolean desligar = false;
|
||||
while (true) {
|
||||
|
||||
|
||||
System.out.println("══════════════════════════════════════\n" +
|
||||
" Seleciona uma das opções\n" +
|
||||
"══════════════════════════════════════\n" +
|
||||
" [1] Contas clientes\n" +
|
||||
" [2] Emissão de cheques/letras\n" +
|
||||
" [3] Moeda\n" +
|
||||
" [4] Sair\n" +
|
||||
"══════════════════════════════════════"
|
||||
|
||||
+ "\n");
|
||||
int opcao = scanner.nextInt();
|
||||
switch (opcao) {
|
||||
case 1 ->
|
||||
contasClientes();
|
||||
// case 2 ->
|
||||
|
||||
// case 3 ->
|
||||
|
||||
case 4 -> desligar = true;
|
||||
default ->
|
||||
System.out.println("Opção inválida");
|
||||
}
|
||||
if (desligar) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void mostrarTodosClientes() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String nomeFicheiro = "clientes.txt";
|
||||
|
||||
String nome[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String idTitular[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String numeroTel[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String email[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String NIF[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String saldoTxt[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
|
||||
leExtraiCsvFicheiro(nomeFicheiro, nome, idTitular, numeroTel, email, NIF, saldoTxt);
|
||||
|
||||
for (int i = 0; i < nome.length; i++) {
|
||||
System.out.println("Nome: " + nome[i]);
|
||||
System.out.println("ID: " + idTitular[i]);
|
||||
System.out.println("Telefone: " + numeroTel[i]);
|
||||
System.out.println("Email: " + email[i]);
|
||||
System.out.println("NIF: " + NIF[i] + "\n");
|
||||
System.out.println("Saldo:" + saldoTxt[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private static void extraiDadosCsv(String[] nome,
|
||||
String[] idTitular,
|
||||
String[] numeroTel,
|
||||
String[] email,
|
||||
String[] NIF,
|
||||
String[] saldoTxt,
|
||||
int numeroLinha,
|
||||
String linha) {
|
||||
int posicaoUltimaVirgula = -1;
|
||||
int numeroVirgulas = 0;
|
||||
|
||||
for (int i = 0; i < linha.length(); i++) {
|
||||
if (linha.charAt(i) == ',') {
|
||||
System.out.println(linha.substring(posicaoUltimaVirgula + 1, i));
|
||||
switch (numeroVirgulas) {
|
||||
case 0 ->
|
||||
nome[numeroLinha] = linha.substring(posicaoUltimaVirgula + 1, i);
|
||||
case 1 ->
|
||||
idTitular[numeroLinha] = linha.substring(posicaoUltimaVirgula + 1, i);
|
||||
case 2 ->
|
||||
numeroTel[numeroLinha] = linha.substring(posicaoUltimaVirgula + 1, i);
|
||||
case 3 ->
|
||||
email[numeroLinha] = linha.substring(i + 1);
|
||||
case 4 ->
|
||||
NIF[numeroLinha] = linha.substring(i + 1);
|
||||
case 5 ->
|
||||
saldoTxt[numeroLinha] = linha.substring(i + 1);
|
||||
|
||||
}
|
||||
posicaoUltimaVirgula = i;
|
||||
numeroVirgulas++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void escreveNoFicheiro(String texto, String nomeFicheiro) {
|
||||
|
||||
FileWriter fileWriter = null;
|
||||
try {
|
||||
fileWriter = new FileWriter(new File(nomeFicheiro));
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
|
||||
bufferedWriter.write(texto);
|
||||
bufferedWriter.close();
|
||||
fileWriter.close();
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Mavenproject25.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Area dos clientes
|
||||
private static String leFicheiro(String nomeFicheiro) {
|
||||
|
||||
FileReader fileReader = null;
|
||||
String textoLido = "";
|
||||
try {
|
||||
fileReader = new FileReader(new File(nomeFicheiro));
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
String linha = "";
|
||||
while ((linha = bufferedReader.readLine()) != null) {
|
||||
textoLido += linha + "\n";
|
||||
}
|
||||
bufferedReader.close();
|
||||
fileReader.close();
|
||||
} catch (FileNotFoundException ex) {
|
||||
escreveNoFicheiro("", nomeFicheiro);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Mavenproject25.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return textoLido;
|
||||
}
|
||||
|
||||
private static String leExtraiCsvFicheiro(String nomeFicheiro,
|
||||
String[] nome,
|
||||
String[] idTitular,
|
||||
String[] numeroTel,
|
||||
String[] email,
|
||||
String[] nif,
|
||||
String[] saldoTxt) {
|
||||
|
||||
FileReader fileReader = null;
|
||||
String textoLido = "";
|
||||
try {
|
||||
fileReader = new FileReader(new File(nomeFicheiro));
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
String linha = "";
|
||||
int numeroLinha = 0;
|
||||
while ((linha = bufferedReader.readLine()) != null) {
|
||||
extraiDadosCsv(nome, idTitular, numeroTel, email, nif, saldoTxt, numeroLinha, linha);
|
||||
numeroLinha++;
|
||||
}
|
||||
bufferedReader.close();
|
||||
fileReader.close();
|
||||
} catch (FileNotFoundException ex) {
|
||||
escreveNoFicheiro("", nomeFicheiro);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Mavenproject25.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return textoLido;
|
||||
}
|
||||
|
||||
private static int getNumeroLinhasFicheiro(String nomeFicheiro) {
|
||||
|
||||
FileReader fileReader = null;
|
||||
int numeroLinhas = 0;
|
||||
try {
|
||||
fileReader = new FileReader(new File(nomeFicheiro));
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
String linha = "";
|
||||
while ((linha = bufferedReader.readLine()) != null) {
|
||||
numeroLinhas++;
|
||||
}
|
||||
bufferedReader.close();
|
||||
fileReader.close();
|
||||
} catch (FileNotFoundException ex) {
|
||||
escreveNoFicheiro("", nomeFicheiro);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Mavenproject25.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return numeroLinhas;
|
||||
}
|
||||
|
||||
private static void adicionaTextoAoFicheiro(String texto, String nomeFicheiro) {
|
||||
String textoAntigo = leFicheiro(nomeFicheiro);
|
||||
String textoNovo = textoAntigo + texto;
|
||||
escreveNoFicheiro(textoNovo, nomeFicheiro);
|
||||
}
|
||||
|
||||
/**
|
||||
* *****************************************opções da conta dos clientes****************************************************
|
||||
*/
|
||||
|
||||
|
||||
|
||||
public static void contasClientes() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
boolean sair = false;
|
||||
while (true) {
|
||||
|
||||
|
||||
System.out.println("\n"
|
||||
|
||||
+ "╔════════════════════════════════════╗\n" +
|
||||
"║ Introduza opção: ║\n" +
|
||||
"╠════════════════════════════════════╣\n" +
|
||||
"║ ▸ 1 Adicionar cliente ║\n" +
|
||||
"║ ▸ 2 Mostrar todos os clientes ║\n" +
|
||||
"║ ▸ 3 Procurar cliente ║\n" +
|
||||
"║ ▸ 4 Sair ║\n" +
|
||||
"╚════════════════════════════════════╝"
|
||||
+ "\n");
|
||||
|
||||
int opcaoCliente = scanner.nextInt();
|
||||
switch (opcaoCliente) {
|
||||
case 1 ->
|
||||
adicionarNovoCliente();
|
||||
case 2 ->
|
||||
mostrarTodosClientes();
|
||||
case 3 -> procurarCliente();
|
||||
case 4 -> sair = true;
|
||||
|
||||
default ->
|
||||
System.out.println("Opção invalida");
|
||||
}
|
||||
if (sair) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void verificarNIF(String NIF) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
while (!NIF.matches("\\d{9}")) {
|
||||
String NIFValido = "";
|
||||
System.out.println("NIF inválido (tem de ter 9 dígitos)\n"
|
||||
+ "Introduza o NIF novamente:");
|
||||
}
|
||||
// Separar os dígitos
|
||||
|
||||
int d1 = Character.getNumericValue(NIF.charAt(0));
|
||||
int d2 = Character.getNumericValue(NIF.charAt(1));
|
||||
int d3 = Character.getNumericValue(NIF.charAt(2));
|
||||
int d4 = Character.getNumericValue(NIF.charAt(3));
|
||||
int d5 = Character.getNumericValue(NIF.charAt(4));
|
||||
int d6 = Character.getNumericValue(NIF.charAt(5));
|
||||
int d7 = Character.getNumericValue(NIF.charAt(6));
|
||||
int d8 = Character.getNumericValue(NIF.charAt(7));
|
||||
int d9 = Character.getNumericValue(NIF.charAt(8));
|
||||
int produto = (d8 * 2) + (d7 * 3) + (d6 * 4) + (d5 * 5) +
|
||||
(d4 * 6) + (d3 * 7) + (d2 * 8) + (d1 * 9);
|
||||
int resto = produto % 11;
|
||||
int digitoControlo;
|
||||
if (resto == 0 || resto == 1){
|
||||
digitoControlo = 0;
|
||||
} else {
|
||||
digitoControlo = 11 - resto;
|
||||
}
|
||||
if(digitoControlo == d9){
|
||||
System.out.println("O NIF é válido");
|
||||
|
||||
} else {
|
||||
System.out.println("O NIF é inválido\n"
|
||||
+ "Introduza um válido:");
|
||||
NIF = scanner.nextLine();
|
||||
System.out.println("O NIF é válido");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void procurarCliente() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String nomeFicheiro = "clientes.txt";
|
||||
|
||||
if (getNumeroLinhasFicheiro(nomeFicheiro) == 0) {
|
||||
System.out.println("Nenhum cliente cadastrado.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.print("Digite o nome do cliente que deseja procurar: ");
|
||||
String nomeProcurar = scanner.nextLine();
|
||||
|
||||
String nome[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String idTitular[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String numeroTel[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String email[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String NIF[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String saldoTxt[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
|
||||
leExtraiCsvFicheiro(nomeFicheiro, nome, idTitular, numeroTel, email, NIF, saldoTxt);
|
||||
|
||||
boolean encontrado = false;
|
||||
|
||||
for (int i = 0; i < nome.length; i++) {
|
||||
if (nome[i].toLowerCase().contains(nomeProcurar.toLowerCase())) {
|
||||
System.out.println("\n--- Cliente encontrado ---");
|
||||
System.out.println("Nome: " + nome[i]);
|
||||
System.out.println("ID: " + idTitular[i]);
|
||||
System.out.println("Telefone: " + numeroTel[i]);
|
||||
System.out.println("Email: " + email[i]);
|
||||
System.out.println("NIF: " + NIF[i] + "\n");
|
||||
System.out.println("Saldo: " + saldoTxt[i] + "\n");
|
||||
encontrado = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!encontrado) {
|
||||
System.out.println("Cliente não encontrado.");
|
||||
}
|
||||
}
|
||||
|
||||
public static void editarDinheiroCliente() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String nomeFicheiro = "clientes.txt";
|
||||
|
||||
if (getNumeroLinhasFicheiro(nomeFicheiro) == 0) {
|
||||
System.out.println("Nenhum cliente cadastrado para editar.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.print("Digite o nome do cliente que deseja editar: ");
|
||||
String nomeEditar = scanner.nextLine();
|
||||
|
||||
String nome[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String idTitular[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String numeroTel[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String email[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String NIF[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
String saldo[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||
|
||||
leExtraiCsvFicheiro(nomeFicheiro, nome, idTitular, numeroTel, email, NIF, saldo);
|
||||
|
||||
int indiceEditar = -1;
|
||||
|
||||
for (int i = 0; i < nome.length; i++) {
|
||||
if (nome[i].toLowerCase().contains(nomeEditar.toLowerCase())) {
|
||||
indiceEditar = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (indiceEditar == -1) {
|
||||
System.out.println("Cliente não encontrado.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("\n--- Editando cliente: " + nome[indiceEditar] + " ---");
|
||||
|
||||
System.out.print("Novo nome (atual: " + nome[indiceEditar] + "): ");
|
||||
String novoNome = scanner.nextLine();
|
||||
if (!novoNome.isEmpty()) nome[indiceEditar] = novoNome;
|
||||
|
||||
System.out.print("Novo ID (atual: " + idTitular[indiceEditar] + "): ");
|
||||
String novoId = scanner.nextLine();
|
||||
if (!novoId.isEmpty()) idTitular[indiceEditar] = novoId;
|
||||
|
||||
System.out.print("Novo telefone (atual: " + numeroTel[indiceEditar] + "): ");
|
||||
String novoTelefone = scanner.nextLine();
|
||||
if (!novoTelefone.isEmpty()) numeroTel[indiceEditar] = novoTelefone;
|
||||
|
||||
System.out.print("Novo email (atual: " + email[indiceEditar] + "): ");
|
||||
String novoEmail = scanner.nextLine();
|
||||
if (!novoEmail.isEmpty()) email[indiceEditar] = novoEmail;
|
||||
|
||||
System.out.print("Novo NIF (atual: " + NIF[indiceEditar] + "): ");
|
||||
String novoNIF = scanner.nextLine();
|
||||
if (!novoNIF.isEmpty()) NIF[indiceEditar] = novoNIF;
|
||||
|
||||
// Reescrever o ficheiro com os dados atualizados
|
||||
StringBuilder conteudoAtualizado = new StringBuilder();
|
||||
for (int i = 0; i < nome.length; i++) {
|
||||
conteudoAtualizado.append(nome[i]).append(",")
|
||||
.append(idTitular[i]).append(",")
|
||||
.append(numeroTel[i]).append(",")
|
||||
.append(email[i]).append(",")
|
||||
.append(NIF[i]).append("\n")
|
||||
.append(saldo[i]).append("\n");
|
||||
}
|
||||
|
||||
escreveNoFicheiro(conteudoAtualizado.toString(), nomeFicheiro);
|
||||
System.out.println("Cliente atualizado com sucesso!");
|
||||
}
|
||||
private static boolean verificarEmail (String email){
|
||||
boolean emailValido = (email.contains("@")
|
||||
&& email.contains(".pt") || email.contains(".com"));
|
||||
|
||||
return emailValido;
|
||||
}
|
||||
|
||||
|
||||
private static boolean verificarSaldo(double saldo){ //verificar saldo
|
||||
boolean vrSaldo = false;
|
||||
|
||||
if (saldo < 60) {
|
||||
vrSaldo = true;
|
||||
}
|
||||
return vrSaldo;
|
||||
}
|
||||
|
||||
private static void adicionarNovoCliente() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
System.out.print("\n"
|
||||
+ "╔════════════════════════════════════════\n"
|
||||
+ "║Introduza o nome: ");
|
||||
String nome = scanner.next();
|
||||
System.out.print("╠════════════════════════════════════════\n");
|
||||
System.out.print("║Introduza o ID: ");
|
||||
String idTitular = scanner.next();
|
||||
System.out.print("╠════════════════════════════════════════\n");
|
||||
System.out.print("║Introduza NIF: ");
|
||||
String nif = scanner.next();
|
||||
System.out.print("╠════════════════════════════════════════\n");
|
||||
System.out.print("║Introduza número: ");
|
||||
String NumeroTel = scanner.next();
|
||||
System.out.print("╠════════════════════════════════════════\n");
|
||||
|
||||
String email;
|
||||
System.out.print("║Introduza email: ");
|
||||
email = scanner.next();
|
||||
|
||||
while(!verificarEmail(email)) {
|
||||
System.out.println("║Email inválido!");
|
||||
System.out.println("║Introduza email novamente:");
|
||||
email = scanner.next();
|
||||
System.out.print("╠════════════════════════════════════════\n");
|
||||
|
||||
}
|
||||
|
||||
double saldo;
|
||||
System.out.print("║Introduza saldo(Min: 60€): ");
|
||||
saldo = scanner.nextDouble();
|
||||
while(verificarSaldo(saldo)) {
|
||||
System.out.println("║saldo minimo insuficiente");
|
||||
System.out.println("║Introduza saldo novamente:");
|
||||
saldo = scanner.nextDouble();
|
||||
System.out.print("╠════════════════════════════════════════\n");
|
||||
}
|
||||
String saldoTxt = Double.toString(saldo);
|
||||
|
||||
System.out.println("║Conta criada co sucesso!");
|
||||
System.out.print("╚════════════════════════════════════════\n"
|
||||
+ "\n");
|
||||
String cliente = nome + (",") + idTitular + (",") + nif + (",") + NumeroTel + (",") + email + (",") + saldoTxt + (".");
|
||||
adicionaTextoAoFicheiro(cliente, "clientes.txt");
|
||||
}
|
||||
|
||||
/**
|
||||
* ****************************************************Moeda****************************************************
|
||||
*/
|
||||
|
||||
// private static void moeda(){
|
||||
// procurarCliente();
|
||||
//
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user