-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReserva.java
More file actions
128 lines (104 loc) · 3.78 KB
/
Reserva.java
File metadata and controls
128 lines (104 loc) · 3.78 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
// * Classe Reserva - Demonstra:
// * 1. IMPLEMENTAÇÃO DE INTERFACE: Implementa Cobravel
// * 2. COMPOSIÇÃO: Contém referências a Hospede, Quarto e lista de Servicos
// * 3. ENCAPSULAMENTO: Atributos privados com acesso controlado
// * 4. COESÃO: Classe com responsabilidade específica (gerenciar reserva)
// * 5. USO DE COLEÇÕES: ArrayList para gerenciar múltiplos serviços
// * 6. ATRIBUTO DE CLASSE (STATIC): Contador de IDs para garantir unicidade
public class Reserva implements Cobravel {
private static int contadorIds = 1;
private final int id;
private Hospede hospede;
private Quarto quarto;
private Date dataEntrada;
private Date dataSaida;
private final List<Servico> servicos;
private static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
public Reserva(Hospede hospede, Quarto quarto, Date dataEntrada, Date dataSaida) {
if (dataSaida.before(dataEntrada)) {
throw new IllegalArgumentException("Data de saída não pode ser antes da data de entrada.");
}
this.id = contadorIds++;
this.hospede = hospede;
this.quarto = quarto;
this.dataEntrada = dataEntrada;
this.dataSaida = dataSaida;
this.servicos = new ArrayList<>();
}
public int getId() {
return id;
}
public Hospede getHospede() {
return hospede;
}
public void setHospede(Hospede hospede) {
this.hospede = hospede;
}
public Quarto getQuarto() {
return quarto;
}
public void setQuarto(Quarto quarto) {
this.quarto = quarto;
}
public Date getDataEntrada() {
return dataEntrada;
}
public void setDataEntrada(Date dataEntrada) {
this.dataEntrada = dataEntrada;
}
public Date getDataSaida() {
return dataSaida;
}
public void setDataSaida(Date dataSaida) {
this.dataSaida = dataSaida;
}
public List<Servico> getServicos() {
return servicos;
}
public void adicionarServico(Servico servico) {
this.servicos.add(servico);
}
public void removerServico(Servico servico) {
this.servicos.remove(servico);
}
public int getNumeroDias() {
long diff = dataSaida.getTime() - dataEntrada.getTime();
int dias = (int) (diff / (1000 * 60 * 60 * 24));
return dias == 0 ? 1 : dias; // considera mínimo de 1 diária
}
public double calcularValorServicos() {
double total = 0;
for (Servico servico : servicos) {
total += servico.isPorPessoa() ? servico.getPreco() * quarto.getCapacidade() : servico.getPreco();
}
return total;
}
public double calcularValorTotal() {
return quarto.getPrecoPorNoite() * getNumeroDias() + calcularValorServicos();
}
@Override
public double calcularValor() {
return calcularValorTotal();
}
@Override
public String toString() {
StringBuilder servicosFormatados = new StringBuilder();
for (Servico s : servicos) {
servicosFormatados.append("- ").append(s).append("\n");
}
String servicosTexto = servicos.isEmpty()
? "Nenhum serviço adicionado."
: servicos.stream().map(s -> "- " + s).reduce("", (a, b) -> a + b + "\n").trim();
return "Reserva #" + id +
"\nHóspede: " + hospede.getNome() +
"\nQuarto: " + quarto.getDescricao() +
"\nEntrada: " + sdf.format(dataEntrada) +
"\nSaída: " + sdf.format(dataSaida) +
"\nServiços:\n" + servicosTexto +
"\nValor Total: R$" + String.format("%.2f", calcularValor());
}
}