-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticipante.java
More file actions
88 lines (78 loc) · 3.65 KB
/
Copy pathParticipante.java
File metadata and controls
88 lines (78 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.sql.*;
import java.util.ArrayList;
public class Participante extends Pessoa {
private String departamento;
private String email;
private String telefone;
// Lista estática para armazenar participantes em memória
private static ArrayList<Participante> participantes = new ArrayList<>();
public Participante(String nome, String departamento, String email, String telefone) {
super(nome, "Participante");
this.departamento = departamento;
this.email = email;
this.telefone = telefone;
}
// CRUD: Adicionar participante
public static void adicionarParticipante(Participante participante) {
String sql = "INSERT INTO Participante (nome, tipo, departamento, email, telefone) VALUES (?, ?, ?, ?, ?)";
try (Connection conn = ConnectionFactory.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, participante.getNome());
stmt.setString(2, participante.getTipo());
stmt.setString(3, participante.departamento);
stmt.setString(4, participante.email);
stmt.setString(5, participante.telefone);
stmt.executeUpdate();
participantes.add(participante); // Adiciona à lista em memória após inserir no banco
} catch (SQLException e) {
System.out.println("Erro ao adicionar participante: " + e.getMessage());
}
}
// CRUD: Listar participantes
public static ArrayList<Participante> listarParticipantes() {
return participantes;
}
// CRUD: Atualizar participante
public static void atualizarParticipante(int id, String novoNome, String novoDepto, String novoEmail, String novoTelefone) {
for (Participante participante : participantes) {
if (participante.getId() == id) {
participante.setNome(novoNome); // Atualiza o nome usando o setter
participante.departamento = novoDepto;
participante.email = novoEmail;
participante.telefone = novoTelefone;
// Atualiza no banco de dados
String sql = "UPDATE Participante SET nome=?, departamento=?, email=?, telefone=? WHERE id=?";
try (Connection conn = ConnectionFactory.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, novoNome);
stmt.setString(2, novoDepto);
stmt.setString(3, novoEmail);
stmt.setString(4, novoTelefone);
stmt.setInt(5, id);
stmt.executeUpdate();
} catch (SQLException e) {
System.out.println("Erro ao atualizar participante: " + e.getMessage());
}
break;
}
}
}
// CRUD: Deletar participante
public static void deletarParticipante(int id) {
participantes.removeIf(participante -> participante.getId() == id);
// Deletar no banco de dados
String sql = "DELETE FROM Participante WHERE id=?";
try (Connection conn = ConnectionFactory.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
System.out.println("Erro ao deletar participante: " + e.getMessage());
}
}
@Override
public String toString() {
return "Participante [id=" + getId() + ", nome=" + getNome() + ", departamento=" + departamento +
", email=" + email + ", telefone=" + telefone + "]";
}
}