Skip to content

Commit 323cd73

Browse files
committed
- Commit do Exercício 5 (enunciado na pasta 'exercise') do Capítulo 7
1 parent de8f47d commit 323cd73

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
25.5 KB
Binary file not shown.

EX-CAP-07-05/src/EX0705.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
3+
public class EX0705 {
4+
5+
public static void main(String[] args) {
6+
Sala[] ls = new Sala[1];
7+
8+
Scanner e = new Scanner(System.in);
9+
10+
int s;
11+
String lugar;
12+
boolean resp;
13+
14+
ls[0] = new Sala(1);
15+
16+
System.out.println("Informe a sala: ");
17+
s = e.nextInt();
18+
19+
if (s != ls[0].getNum())
20+
{
21+
System.out.println("Sala Inexistente");
22+
23+
e.close();
24+
return;
25+
}
26+
27+
System.out.println("Informe o lugar: ");
28+
lugar = e.next();
29+
30+
resp = ls[0].reserva(lugar);
31+
32+
if (resp)
33+
System.out.println("Reserva Efetuada");
34+
else
35+
System.out.println("Reserva Não Efetuada");
36+
37+
e.close();
38+
}
39+
}

EX-CAP-07-05/src/Lugar.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
public class Lugar {
3+
private String cod;
4+
private boolean livre;
5+
6+
public Lugar(String cod, boolean livre) {
7+
this.cod = cod;
8+
this.livre = livre;
9+
}
10+
11+
public String getCod() {
12+
return cod;
13+
}
14+
15+
public boolean getLivre() {
16+
return livre;
17+
}
18+
19+
public void reserva() {
20+
if (!livre)
21+
return;
22+
23+
livre = !livre;
24+
}
25+
}

EX-CAP-07-05/src/Sala.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
public class Sala {
3+
private int num;
4+
private Lugar[] lugares;
5+
6+
public Sala(int num) {
7+
this.num = num;
8+
9+
lugares = new Lugar[4];
10+
lugares[0] = new Lugar("A1", true);
11+
lugares[1] = new Lugar("A2", true);
12+
lugares[2] = new Lugar("A3", false);
13+
lugares[3] = new Lugar("A4", false);
14+
}
15+
16+
public int getNum() {
17+
return num;
18+
}
19+
20+
public boolean reserva(String c) {
21+
if (lugares == null)
22+
return false;
23+
24+
for (int i = 0; i < lugares.length; i++) {
25+
if (!lugares[i].getCod().equals(c))
26+
continue;
27+
28+
if (lugares[i].getLivre()) {
29+
lugares[i].reserva();
30+
return true;
31+
}
32+
}
33+
34+
return false;
35+
}
36+
}

0 commit comments

Comments
 (0)