Add the transformations stack code.
[TP_POO.git] / TP2 / Piletransformations.java
diff --git a/TP2/Piletransformations.java b/TP2/Piletransformations.java
new file mode 100644 (file)
index 0000000..6d25095
--- /dev/null
@@ -0,0 +1,60 @@
+
+class Piletransformations {
+    private Point[] transformations;
+    private int currentTransformation;
+
+    Piletransformations(int size) {
+        transformations = new Point[size];
+        for (int i = 0; i < transformations.length; i++) {
+            transformations[i] = new Point(0, 0);
+        }
+        currentTransformation = 0;
+    }
+
+    public Point getCurrentTransformation() {
+        if (isEmpty()) {
+            return transformations[currentTransformation];
+        } else {
+            return transformations[currentTransformation - 1];
+        }
+    }
+
+    private boolean isEmpty() {
+        return (currentTransformation == 0);
+    }
+
+    private boolean isFull() {
+        return (currentTransformation >= transformations.length);
+    }
+
+    public boolean empiler(Point p) {
+        boolean rtVal = false;
+        if (isEmpty()) {
+            transformations[currentTransformation] = p;
+            currentTransformation++;
+            rtVal = true;
+        } else if (!isFull()) {
+            transformations[currentTransformation] = transformations[currentTransformation - 1].additionner(p);
+            currentTransformation++;
+            rtVal = true;
+        }
+        return rtVal;
+    }
+
+    public Point depiler() {
+        if(!isEmpty()) {
+            currentTransformation--;
+            return transformations[currentTransformation];
+        } else {
+            return transformations[currentTransformation];
+        }
+    }
+
+    public void display() {
+        System.out.println("----");
+        for (int i = 0; i < transformations.length; i++) {
+            System.out.println(transformations[i].toString());
+        }
+    }
+
+}