-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlmacen1
108 lines (97 loc) · 2.51 KB
/
Almacen1
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
public class Almacen1 {
static private final int LIBRE = 0;
// Array con los valores almacenados
private int tvalores[];
private int valoresAlmacenados = 0;
private String valoresTabla;
// Constructores sin parámetros creo una tabla de 10 elementos
public Almacen1(){
this(10); // Llamo al constructor con parámetros
}
// Constructor donde se fija tamaño máximo del Almacén
public Almacen1( int tamaño){
tvalores = new int [tamaño];
init();
}
// Pone todas las posiciones a LIBRES
public void init (){
for (int i = 0; i < tvalores.length; i++) {
tvalores[i] = Almacen1.LIBRE;
}
valoresAlmacenados = 0;
}
// Muestra una cadena con los valores de la tabla
public String toString (){
this.valoresTabla = "";
for (int i = 0; i < this.tvalores.length; i++) {
this.valoresTabla = this.valoresTabla + this.tvalores[i];
}
return this.valoresTabla;
}
// Devuelve el números de posiciones libres
public int numPosicionesLibres(){
int libres = 0;
for (int i = 0; i < this.tvalores.length ; i++) {
if (this.tvalores[i] == 0) {
libres++;
}
}
return libres;
}
// Devuelve el número de posiones ocupadas
public int numPosicionesOcupadas(){
int ocupadas = 0;
for (int i = 0; i < this.tvalores.length ; i++) {
if (this.tvalores[i] != 0) {
ocupadas++;
}
}
return ocupadas;
}
// Devuelve verdadero o falso si está almacenado el valor en la tabla
public boolean estaValor (int num){
boolean almacenado = false;
for (int i = 0; i < this.tvalores.length; i++) {
if (this.tvalores[i] == num) {
almacenado = true;
break;
}
}
return almacenado;
}
// Almacena el valor el la tabla, devuelve false si no puede almacenarlo
public boolean ponValor (int num){
boolean almacenarvalor = false;
for(int i = 0; i<this.tvalores.length; i++) {
if(this.tvalores[i] == 0) {
this.tvalores[i] = num;
almacenarvalor=true;
break;
}
}
return almacenarvalor;
}
// Elimina el elemento de la tabla, si no esta devuelve false
public boolean sacarValor (int num){
boolean eliminarvalor = false;
for (int i = 0 ; i < this.tvalores.length; i++) {
if(this.tvalores[i] == num) {
this.tvalores[i] = 0;
eliminarvalor = true;
break;
}
}
return eliminarvalor;
}
// Indica si el almacén esta lleno
public boolean estaLleno (){
boolean lleno = true;
for (int i = 0; i < this.tvalores.length; i++) {
if (this.tvalores[i] == 0) {
lleno = false;
break;
}
}
return lleno;
}
}