quarto commit
This commit is contained in:
1
TrabalhoFicheiros/inscritos.txt
Normal file
1
TrabalhoFicheiros/inscritos.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Vianinha Fofinha,69,Portas fronhas numero 74,VianaFofiinhaemail.epvc,67
|
||||||
13
TrabalhoFicheiros/pom.xml
Normal file
13
TrabalhoFicheiros/pom.xml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.mycompany</groupId>
|
||||||
|
<artifactId>TrabalhoFicheiros</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.release>24</maven.compiler.release>
|
||||||
|
<exec.mainClass>com.mycompany.trabalhoficheiros.TrabalhoFicheiros</exec.mainClass>
|
||||||
|
</properties>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,388 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mycompany.trabalhoficheiros;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
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 250418
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class TrabalhoFicheiros {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
//Vaga de Emprego
|
||||||
|
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
System.out.println("╔════════════════════════════════════╗");
|
||||||
|
System.out.println("║ ║");
|
||||||
|
System.out.println("║ 🚛 CARGOWAVE 🚛 ║");
|
||||||
|
System.out.println("║ ║");
|
||||||
|
System.out.println("║ Transporte & Logística ║");
|
||||||
|
System.out.println("║ ║");
|
||||||
|
System.out.println("╚════════════════════════════════════╝");
|
||||||
|
|
||||||
|
System.out.println("╔══════════════════════════════════════════════╗");
|
||||||
|
System.out.println("║ ║");
|
||||||
|
System.out.println("║ Candidate-se a vaga de camioneiro ║");
|
||||||
|
System.out.println("║ ║");
|
||||||
|
System.out.println("╚══════════════════════════════════════════════╝");
|
||||||
|
|
||||||
|
while(true){
|
||||||
|
|
||||||
|
System.out.println("Selecione uma das opções:\n"
|
||||||
|
|
||||||
|
+ "|-------------------------------------|\n"
|
||||||
|
+ "|1 - Candidatar-se |\n"
|
||||||
|
+ "|-------------------------------------|\n"
|
||||||
|
+ "|2 - Consultar todos os candidatos |\n"
|
||||||
|
+ "|-------------------------------------|\n"
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
int opcao = scanner.nextInt();
|
||||||
|
|
||||||
|
switch(opcao){
|
||||||
|
|
||||||
|
case 1 -> inserirDados();
|
||||||
|
|
||||||
|
case 2 -> mostrarTodosCandidatos();
|
||||||
|
|
||||||
|
default -> System.out.println("Opção inválida");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void mostrarTodosCandidatos() {
|
||||||
|
|
||||||
|
String nomeFicheiro = "inscritos.txt";
|
||||||
|
|
||||||
|
String nome[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||||
|
|
||||||
|
String idade[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||||
|
|
||||||
|
String morada[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||||
|
|
||||||
|
String email[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||||
|
|
||||||
|
String experiencia[] = new String[getNumeroLinhasFicheiro(nomeFicheiro)];
|
||||||
|
|
||||||
|
leExtraiCsvFicheiro(nomeFicheiro, nome, idade, morada, email, experiencia);
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < nome.length; i++) {
|
||||||
|
|
||||||
|
System.out.println("Nome: " + nome[i]);
|
||||||
|
|
||||||
|
System.out.println("Idade: " + idade[i]);
|
||||||
|
|
||||||
|
System.out.println("Morada: " + morada[i]);
|
||||||
|
|
||||||
|
System.out.println("Email:" + email[i]);
|
||||||
|
|
||||||
|
System.out.println("Anos de experiência: " + experiencia[i] + "\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void extraiDadosCsv(String[] nome,
|
||||||
|
|
||||||
|
String[] idade,
|
||||||
|
|
||||||
|
String[] morada,
|
||||||
|
|
||||||
|
String[] email,
|
||||||
|
|
||||||
|
String[] experiencia,
|
||||||
|
|
||||||
|
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 ->
|
||||||
|
|
||||||
|
idade[numeroLinha] = linha.substring(posicaoUltimaVirgula + 1, i);
|
||||||
|
|
||||||
|
case 2 ->
|
||||||
|
|
||||||
|
morada[numeroLinha] = linha.substring(posicaoUltimaVirgula + 1, i);
|
||||||
|
|
||||||
|
case 3 -> {
|
||||||
|
|
||||||
|
email[numeroLinha] = linha.substring(posicaoUltimaVirgula + 1, i);
|
||||||
|
|
||||||
|
experiencia[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(TrabalhoFicheiros.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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(TrabalhoFicheiros.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return textoLido;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String leExtraiCsvFicheiro(String nomeFicheiro,
|
||||||
|
|
||||||
|
String[] nome,
|
||||||
|
|
||||||
|
String[] idade,
|
||||||
|
|
||||||
|
String[] morada,
|
||||||
|
|
||||||
|
String[] email,
|
||||||
|
|
||||||
|
String[] experiencia) {
|
||||||
|
|
||||||
|
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, idade, morada, email, experiencia, numeroLinha, linha);
|
||||||
|
|
||||||
|
numeroLinha++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bufferedReader.close();
|
||||||
|
|
||||||
|
fileReader.close();
|
||||||
|
|
||||||
|
} catch (FileNotFoundException ex) {
|
||||||
|
|
||||||
|
escreveNoFicheiro("", nomeFicheiro);
|
||||||
|
|
||||||
|
} catch (IOException ex) {
|
||||||
|
|
||||||
|
Logger.getLogger(TrabalhoFicheiros.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(TrabalhoFicheiros.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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void inserirDados(){
|
||||||
|
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Insira o seu nome");
|
||||||
|
|
||||||
|
String nome = scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Insira sua idade");
|
||||||
|
|
||||||
|
int idade = scanner.nextInt();
|
||||||
|
|
||||||
|
scanner.nextLine();
|
||||||
|
|
||||||
|
if (idade < 25) {
|
||||||
|
|
||||||
|
System.out.println("não está apto para a vaga");
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Insira sua morada");
|
||||||
|
|
||||||
|
String morada = scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Insira seu email");
|
||||||
|
|
||||||
|
String email = scanner.nextLine();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("Insira quantos anos tens de experiência");
|
||||||
|
|
||||||
|
String experiencia = scanner.nextLine();
|
||||||
|
|
||||||
|
String dados = nome + "," + idade + "," + morada + "," + email + "," + experiencia + "\n";
|
||||||
|
|
||||||
|
adicionaTextoAoFicheiro(dados, "inscritos.txt");
|
||||||
|
|
||||||
|
System.out.println("*******************************************************");
|
||||||
|
System.out.println("* *");
|
||||||
|
System.out.println("* Dentro de 7 dias uma resposta chegará no seu email *");
|
||||||
|
System.out.println("* *");
|
||||||
|
System.out.println("*******************************************************");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user