-
Notifications
You must be signed in to change notification settings - Fork 0
/
Biscottiera.java
58 lines (49 loc) · 1.44 KB
/
Biscottiera.java
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
package Esercizi_Esami;
public class Biscottiera {
private int capacita;
private int biscotti;
public Biscottiera(int capacita) {
this.capacita = capacita;
biscotti = 0;
}
public boolean piena() {
return (capacita <= biscotti);
}
public boolean vuota() {
return (capacita == 0);
}
public void aggiungiBiscotto() throws Exception {
if(capacita != 0) {
biscotti = biscotti + 1;
capacita = capacita - 1;
} else {
throw new Exception("Riempita!");
}
}
public void togliBiscotto() throws Exception {
if(!vuota()) {
biscotti = biscotti - 1;
capacita = capacita + 1;
} else {
throw new Exception("Vuota!");
}
}
public int getCapacita() {
return capacita;
}
public static void main(String[] args) throws Exception {
Biscottiera cookie = new Biscottiera(2);
try {
cookie.aggiungiBiscotto();
cookie.aggiungiBiscotto();
cookie.aggiungiBiscotto();
} catch (Exception e) {
System.out.println(e.getMessage());
try {
cookie.togliBiscotto();
} catch (Exception e1) {
System.out.println("Capacità disponibile: " + cookie.getCapacita());
}
}
}
}