Code cleanup : fix C style array declaration.
[TP_POO.git] / Piles / Pile.java
CommitLineData
d3d2d15c 1
bc48a285
JB
2/**
3 *
4 */
9b4b86b9 5public class Pile {
fa0086ca 6 private int[] int_array;
d3d2d15c 7 private int array_size;
c3a259ee 8 private int head_index;
d3d2d15c 9
bc48a285 10 /**
8122cb54
JB
11 * set the size of the internal array
12 * @param int size the size of the array
bc48a285 13 */
d3d2d15c
JB
14 private void setSize(int size) {
15 array_size = size;
16 }
17
bc48a285 18 /**
8122cb54
JB
19 * get the size of the internal array
20 * @return the integer size of the internal array
bc48a285 21 */
d3d2d15c
JB
22 private int getSize() {
23 return array_size;
24 }
25
bc48a285 26 /**
8122cb54
JB
27 * set the stack head index
28 * @param int index the stack head index
bc48a285 29 */
d3d2d15c 30 private void setHeadIndex(int index) {
c3a259ee 31 head_index = index;
d3d2d15c
JB
32 }
33
bc48a285 34 /**
8122cb54
JB
35 * get the stack head current index
36 * @return the integer stack head index
bc48a285 37 */
d3d2d15c 38 private int getHeadIndex() {
c3a259ee 39 return head_index;
d3d2d15c
JB
40 }
41
bc48a285
JB
42 /**
43 * [Pile description]
44 * @param int size [description]
45 */
d3d2d15c
JB
46 Pile(int size) {
47 int_array = new int[size];
48 setSize(size);
49 setHeadIndex(0);
50 }
51
bc48a285
JB
52 /**
53 * [empiler description]
54 * @param int value [description]
55 */
d3d2d15c
JB
56 public void empiler(int value) {
57 if (!plein()) {
c3a259ee
JB
58 int_array[head_index] = value;
59 head_index++;
d3d2d15c
JB
60 } else {
61 System.out.println("La pile est pleine");
62 }
63 }
64
bc48a285
JB
65 /**
66 * [depiler description]
67 * @return [description]
68 */
d3d2d15c
JB
69 public int depiler() {
70 if (!vide()) {
c3a259ee
JB
71 head_index--;
72 return int_array[head_index];
d3d2d15c
JB
73 } else {
74 return -1;
75 }
76 }
77
bc48a285
JB
78 /**
79 * [plein description]
80 * @return [description]
81 */
82 private boolean plein() {
d3d2d15c
JB
83 return (getHeadIndex() >= getSize());
84 }
85
bc48a285
JB
86 /**
87 * [vide description]
88 * @return [description]
89 */
90 private boolean vide() {
d3d2d15c
JB
91 return (getHeadIndex() == 0);
92 }
93
bc48a285
JB
94 /**
95 * [afficher description]
96 */
d3d2d15c 97 public void afficher() {
30632042 98 for (int i = 0; i < getHeadIndex(); i++) {
d3d2d15c
JB
99 System.out.println("element " + i + " " + int_array[i]);
100 }
101 }
102
bc48a285
JB
103 /**
104 * The main() function
27869e5b 105 * @param String[] args main() function arguments array
bc48a285 106 */
d3d2d15c 107 public static void main(String[] args) {
b48ca56a
JB
108 Pile stack = new Pile(5);
109
110 stack.empiler(3);
111 stack.empiler(5);
112 stack.empiler(4);
113 stack.empiler(7);
114 stack.empiler(8);
115
116 stack.afficher();
117
bc48a285
JB
118 System.out.println("Stack index " + stack.getHeadIndex());
119 System.out.println("Stack head value " + stack.depiler());
120 System.out.println("Stack index " + stack.getHeadIndex());
121 System.out.println("Stack head value " + stack.depiler());
122 System.out.println("Stack index " + stack.getHeadIndex());
123 System.out.println("Stack head value " + stack.depiler());
124 System.out.println("Stack index " + stack.getHeadIndex());
125 System.out.println("Stack head value " + stack.depiler());
126 System.out.println("Stack index " + stack.getHeadIndex());
127 System.out.println("Stack head value " + stack.depiler());
128 System.out.println("Stack index " + stack.getHeadIndex());
129 System.out.println("Stack head value " + stack.depiler());
130 System.out.println("Stack index " + stack.getHeadIndex());
b48ca56a
JB
131
132 stack.afficher();
d3d2d15c
JB
133 }
134}