ascensore.java 05/Apr/2011 1 of 2 1

annuncio pubblicitario
ascensore.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
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
05/Apr/2011
/** @(#)TestAscensore.java
* Questo programma è la versione in Java di quello creato da
* Alessandro Bugatti in C++
*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** Classe che simula un ascensore in cui si possono aprire e chiudere
* le porte e salire e scendere di un piano per volta
* @author Giovanna Correddu
* @version 1.0 (5/4/2011
*
*
*/
class Ascensore
{
private int numeroPiani; // Numero di piani dell'ascensore
private int pianoCorrente;// Piano corrente a cui si trova l'ascensore
private boolean aperto; // Variabile che dice se l'ascensore ha le porte aperte o chiuse
/**
*
Costruttore della classe a cui vengono passati i piani
*
che deve servire l'ascensore.
*
@param piani Numero di piani che deve servire l'ascensore
*
*/
public Ascensore(int piani) {
this.pianoCorrente=0;
this.aperto=false;
if (piani<=0) this.numeroPiani=1; //Se il numero di piani passati è negativo viene posto a 1
else this.numeroPiani=piani;
}
/**
*
Restituisce il piano corrente
*
@return piano corrente
*/
public int getPianoCorrente(){
return pianoCorrente;
}
/**
*
Apre le porte se sono chiuse, se sono già aperte non fa niente
*/
public void apri(){
if(!porteAperte())
aperto=true;
}
/**
*
Chiude le porte se sono aperte, se sono già chiuse non fa niente
*/
public void chiudi() {
if(porteAperte())
aperto=false;
}
/**
* Sale di un piano, se non è possibile restituisce -1
* altrimenti il nuovo piano in cui si troverà dopo essere salito
* @return il numero del piano al quale sale
*/
public int sali(){
if(porteAperte()||ultimoPiano())
return -1;
else{
this.pianoCorrente++;
return this.pianoCorrente;
}
}
/**
* Scende di un piano, se non è possibile restituisce -1
* altrimenti il nuovo piano in cui si troverà dopo essere sceso
* @return il numero del piano al quale scende
*/
public int scendi(){
if(porteAperte()||pianoTerra())
return -1;
else{
this.pianoCorrente--;
return this.pianoCorrente;
}
}
/**
*
Visualizza lo stato in cui si trova l'ascensore
*/
public void visualizzaStato(){
System.out.println("palazzo di "+this.numeroPiani+ " piani - ascensore al piano "+this.pianoCorrente
porte aperte:" + this.porteAperte());
}
/*
* Metodi privati
**/
1 of 2
ascensore.java
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
129
130
131
132
133
134
135
136
137
05/Apr/2011
/**
*
Verifica se le porte sono aperte
* @return true se aperte, false altrimenti
*/
private boolean porteAperte(){
return this.aperto;
}
/**
*
Verifica se si è al piano terra
* @return true se al piano terra, false altrimenti
*/
private boolean pianoTerra(){
if (this.pianoCorrente==0)
return true;
else
return false;
}
/**
*
Verifica se si è all'ultimo piano
* @return true se all'ultimo piano, false altrimenti
*/
private boolean ultimoPiano(){
if (this.pianoCorrente==this.numeroPiani)
return true;
else
return false;
}
}
2 of 2
Scarica