From acb8d49498554a217aa5a16b21f6a36495c7fe21 Mon Sep 17 00:00:00 2001
From: "stephane.malandai" <stephane.malandain@hesge.ch>
Date: Fri, 18 Oct 2024 10:26:32 +0200
Subject: [PATCH] Update 7 files
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- /Exercices/corrigé/serie2/Account.java
- /Exercices/corrigé/serie2/Container.java
- /Exercices/corrigé/serie2/ContainerHelper.java
- /Exercices/corrigé/serie2/DynArray.java
- /Exercices/corrigé/serie2/DynArrayStat.java
- /Exercices/corrigé/serie2/MainAccount.java
- /Exercices/corrigé/serie2/MainContainer.java
---
 .../corrig\303\251/serie2/Account.java"       |  69 +++++++++++
 .../corrig\303\251/serie2/Container.java"     |  68 +++++++++++
 .../serie2/ContainerHelper.java"              |  31 +++++
 .../corrig\303\251/serie2/DynArray.java"      | 114 ++++++++++++++++++
 .../corrig\303\251/serie2/DynArrayStat.java"  |  41 +++++++
 .../corrig\303\251/serie2/MainAccount.java"   |  28 +++++
 .../corrig\303\251/serie2/MainContainer.java" |  23 ++++
 7 files changed, 374 insertions(+)
 create mode 100644 "Exercices/corrig\303\251/serie2/Account.java"
 create mode 100644 "Exercices/corrig\303\251/serie2/Container.java"
 create mode 100644 "Exercices/corrig\303\251/serie2/ContainerHelper.java"
 create mode 100644 "Exercices/corrig\303\251/serie2/DynArray.java"
 create mode 100644 "Exercices/corrig\303\251/serie2/DynArrayStat.java"
 create mode 100644 "Exercices/corrig\303\251/serie2/MainAccount.java"
 create mode 100644 "Exercices/corrig\303\251/serie2/MainContainer.java"

diff --git "a/Exercices/corrig\303\251/serie2/Account.java" "b/Exercices/corrig\303\251/serie2/Account.java"
new file mode 100644
index 0000000..c06a582
--- /dev/null
+++ "b/Exercices/corrig\303\251/serie2/Account.java"
@@ -0,0 +1,69 @@
+import java.util.Objects;
+
+public class Account {
+
+    private String owner;
+    private int amount;
+
+    public Account(String fullname) {
+        this.owner = fullname;
+    }
+    public Account(String fullname, int amount) {
+        this(fullname);
+        this.deposit(amount);
+    }
+
+    public int amount() {
+        return this.amount;
+    }
+
+    public boolean canWithdraw(int value) {
+        return amount() >= value;
+    }
+
+    public String fullname() {
+        return this.owner;
+    }
+
+    private static void checkPositiveOrThrow(int i) {
+        if (i < 0) {
+            throw new RuntimeException("Amount should be positive");
+        }
+    }
+    public void deposit(int amountToPlace) {
+        Account.checkPositiveOrThrow(amountToPlace);
+        this.amount += amountToPlace;
+    }
+
+    public void withdraw(int amountToWithdraw) {
+        Account.checkPositiveOrThrow(amountToWithdraw);
+        if ( this.amount - amountToWithdraw <= -2000 ) {
+            throw new RuntimeException("withdraw unpossible");
+        }
+        this.amount -= amountToWithdraw;
+    }
+
+    public void transferTo(Account destination, int amountToTransfer) {
+        this.withdraw(amountToTransfer);
+        destination.deposit(amountToTransfer);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || o.getClass() != this.getClass()) {
+            return false;
+        }
+        Account that = (Account)o;
+        return that.owner.equals(this.owner) && that.amount == this.amount;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(this.owner, this.amount);
+    }
+
+
+}
\ No newline at end of file
diff --git "a/Exercices/corrig\303\251/serie2/Container.java" "b/Exercices/corrig\303\251/serie2/Container.java"
new file mode 100644
index 0000000..3e988ae
--- /dev/null
+++ "b/Exercices/corrig\303\251/serie2/Container.java"
@@ -0,0 +1,68 @@
+public class Container {
+   private final int totalCapacity;
+   private int currentValue;
+
+   public Container(int capacity) {
+      ensureCapacity(capacity);
+      this.totalCapacity = capacity;
+   }
+
+   private void ensureCapacity(int capacity) {
+      if (capacity < 0) throw new IllegalStateException("Capacity should be positive or null");
+   }
+
+   public void fillToTheMax() {
+      this.currentValue = totalCapacity;
+   }
+
+   /* Try to add `value` to the container
+    * @return the quantity that could have been added
+    */
+   public int fillWith(int value) {
+      ensureCapacity(value);
+      final int fillable = fillableAccordingTo(value);
+      this.currentValue += fillable;
+      return fillable;
+   }
+
+   public int remaining() {
+      return this.totalCapacity - this.currentValue;
+   }
+
+   public int currentValue() {
+      return this.currentValue;
+   }
+
+   public void flush() {
+      this.currentValue = 0;
+   }
+
+   public int quantity() {
+        return this.currentValue;
+    }
+
+   public void remove(int value) {
+        if (this.currentValue - value >= 0) this.currentValue -= value;
+    }
+
+   public boolean isFull() {
+      return this.currentValue == this.totalCapacity;
+   }
+
+   public void fillTo(Container dest1, Container... dests) {
+      int taken = dest1.fillWith(this.currentValue);
+      this.currentValue -= taken;
+      for(Container d: dests) {
+         taken = d.fillWith(this.currentValue);
+         this.currentValue -= taken;
+      }
+   }
+
+   public int fillableAccordingTo(int value) {
+      return ContainerHelper.canTransfer(value, remaining());
+   }
+
+   public static Container withCapacity(int capacity) {
+      return new Container(capacity);
+   }
+}
\ No newline at end of file
diff --git "a/Exercices/corrig\303\251/serie2/ContainerHelper.java" "b/Exercices/corrig\303\251/serie2/ContainerHelper.java"
new file mode 100644
index 0000000..0236b89
--- /dev/null
+++ "b/Exercices/corrig\303\251/serie2/ContainerHelper.java"
@@ -0,0 +1,31 @@
+public class ContainerHelper {
+    private static void ensurePositiveOrNull(int... values) { 
+        for (int v: values) {
+            ensurePositiveOrNull(v);
+        }
+    }
+    private static void ensurePositiveOrNull(int value) {
+        if (value < 0) {
+            throw new UnsupportedOperationException("Value should be positive: " + value);
+        }
+    }
+
+    public static int canTransfer(int origin, int destination) {
+        return Math.min(origin, destination);
+    }
+
+    public static int[] transfer(int origin, final int dest1, final int... dests) {
+        ensurePositiveOrNull(new int[]{origin, dest1});
+        ensurePositiveOrNull(dests);
+        final int[] res = new int[dests.length+1];
+        int quantityToTransfer = canTransfer(origin, dest1);
+        origin -= quantityToTransfer;
+        res[0] = quantityToTransfer;
+        for(int i = 0; i < dests.length; i += 1) {
+            quantityToTransfer = canTransfer(origin, dests[i]);
+            origin -= quantityToTransfer;
+            res[i+1] = quantityToTransfer;
+        }
+        return res;
+    }
+}
\ No newline at end of file
diff --git "a/Exercices/corrig\303\251/serie2/DynArray.java" "b/Exercices/corrig\303\251/serie2/DynArray.java"
new file mode 100644
index 0000000..c75d2ca
--- /dev/null
+++ "b/Exercices/corrig\303\251/serie2/DynArray.java"
@@ -0,0 +1,114 @@
+public class DynArray {
+
+    private int[] values = {};
+    
+    private DynArray() {}
+    private DynArray(int[] values) {
+        this.values = values;
+    }
+
+    public static DynArray of(int... values) {
+        return new DynArray(values);
+    }
+
+    public DynArray append(int value) {
+        DynArray res = new DynArray(this.values);
+        res.values = append(res.values, value);
+        return res;
+    }
+    public int head() {
+        return head(this.values);
+    }
+    public DynArray pop() {
+        return new DynArray(pop(this.values));
+    }
+    public DynArray concat(DynArray that) {
+        return new DynArray(concat(this.values, that.values));
+    }
+    public int size() {
+        return this.values.length;
+    }
+    public DynArray reversed() {
+        return new DynArray(reversed(this.values));
+    }
+    public String toString() {
+        String res = "[";
+        for (int i = 0; i < this.values.length; i += 1 ) {
+            if (i == 0) {
+                res += this.values[i];
+            } else {
+                res += "," + this.values[i];
+            }
+        }
+        return res + "]";
+    }
+    public int hashCode() {
+        return this.values.hashCode();
+    }
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || this.getClass() != o.getClass()) {
+            return false;
+        }
+        DynArray d = (DynArray)o;
+        return Arrays.equals(this.values, d.values);
+    }
+
+    private static int[] reversed(int[] values) {
+        int[] result = new int[values.length];
+        for (int i = 0; i < values.length; i += 1) {
+            result[values.length-1-i] = values[i];
+        }
+        return result;
+    }
+
+    private static int[] append(int[] values, int value) {
+        int[] result = new int[values.length+1];
+        result[0] = value;
+        for (int i = 0; i < values.length; i += 1) {
+            result[i+1] = values[i];
+        }
+        return result;
+    }
+    private static int head(int[] values) {
+        return values[0];
+    }
+    private static int[] pop(int[] values) {
+        int[] result = new int[values.length-1];
+        for (int i = 0; i < result.length; i += 1) {
+            result[i] = values[i+1];
+        }
+        return result;
+    }
+    private static int[] concat(int[] is, int[] js) { 
+        for (int i = is.length-1; i >= 0; i -= 1) {
+            js = append(js, is[i]);
+        }
+        return js;
+    }
+
+
+    public static void main(String[] args) {
+        DynArray is = DynArray.of(1, 2, 2, -1, 5); 
+        // is = [1, 2, 2, -1, 5]; 
+        DynArray is2 = is.append(10);
+        // is2 = [10, 1, 2, 2, -1, 5]; 
+        int j = is2.head();
+        // j = 10
+        DynArray tail = is2.pop();
+        // tail = [1, 2, 2, -1, 5];
+        DynArray rev = tail.reversed();
+        // tail = [5, -1, 2, 2, 1]
+        int size = rev.size();
+        // size = 5
+        boolean areEquals = is.equals(tail);
+        // areEquals = true
+        DynArray result = is2.concat( DynArray.of(4,3,2) ); 
+        // result = [10, 1, 2, 2, -1, 5, 4, 3, 2]
+        String representation = result.toString(); 
+        // representation = [10, 1, 2, 2, -1, 5, 4, 3, 2]
+    }
+
+}
\ No newline at end of file
diff --git "a/Exercices/corrig\303\251/serie2/DynArrayStat.java" "b/Exercices/corrig\303\251/serie2/DynArrayStat.java"
new file mode 100644
index 0000000..e3a71d5
--- /dev/null
+++ "b/Exercices/corrig\303\251/serie2/DynArrayStat.java"
@@ -0,0 +1,41 @@
+public class DynArray {
+
+    public static int[] append(int[] values, int value) {
+        int[] result = new int[values.length+1];
+        result[0] = value;
+        for (int i = 0; i < values.length; i += 1) {
+            result[i+1] = values[i];
+        }
+        return result;
+    }
+    public static int head(int[] values) {
+        return values[0];
+    }
+    public static int[] pop(int[] values) {
+        int[] result = new int[values.length-1];
+        for (int i = 0; i < result.length; i += 1) {
+            result[i] = values[i+1];
+        }
+        return result;
+    }
+    public static int[] concat(int[] is, int[] js) { 
+        for (int i = is.length-1; i >= 0; i -= 1) {
+            js = append(js, is[i]);
+        }
+        return js;
+    }
+
+
+    public static void main(String[] args) {
+        int[] is = {1, 2, 2, -1, 5}; 
+        int[] is2 = append(is, 10);
+        // is2 = {10, 1, 2, 2, -1, 5}; 
+        int j = head(is2);
+        // j = 10
+        int[] tail = pop(is2);
+        // tail = {1, 2, 2, -1, 5};
+        int[] result = concat(is, new int[]{4,3,2}); 
+        // result = {1, 2, 2, -1, 5, 4, 3, 2}
+    }
+
+}
\ No newline at end of file
diff --git "a/Exercices/corrig\303\251/serie2/MainAccount.java" "b/Exercices/corrig\303\251/serie2/MainAccount.java"
new file mode 100644
index 0000000..c21c7cc
--- /dev/null
+++ "b/Exercices/corrig\303\251/serie2/MainAccount.java"
@@ -0,0 +1,28 @@
+public class MainAccount {
+
+    public static void main(String[] args) {
+
+        Account a1 = new Account("Stephane Malandain", 1000_00);
+        Account a2 = new Account("Yassin Rekik", 1000000_00);
+
+        int coffeePriceInCents = 350;
+
+        
+        if ( a2.canWithdraw(coffeePriceInCents) ) {
+            a2.transferTo(a1, coffeePriceInCents);
+        }
+        
+
+        System.out.println( a1.amount() );
+        System.out.println( a2.amount() );
+
+
+        /* to meditate:
+         * could be fun to use something like :
+         * 
+         *   a2.transferTo(a1).amount(350);
+         *  
+         */
+
+    }
+}
\ No newline at end of file
diff --git "a/Exercices/corrig\303\251/serie2/MainContainer.java" "b/Exercices/corrig\303\251/serie2/MainContainer.java"
new file mode 100644
index 0000000..02b2646
--- /dev/null
+++ "b/Exercices/corrig\303\251/serie2/MainContainer.java"
@@ -0,0 +1,23 @@
+public class MainContainer{
+
+    public static void main(String[] args) {
+
+        Container origin = Container.withCapacity(10); // récipient vide par défaut
+        origin.fillToTheMax(); // remplissage
+        Container destination1 = new Container(5); // idem que Container.withCapacity(5);
+        destination1.fillWith(2);
+        Container destination2 = Container.withCapacity(3);
+        Container destination3 = Container.withCapacity(10);
+        origin.fillTo(destination1, destination2, destination3);
+        assert destination1.isFull() ;
+        assert destination2.isFull() ;
+        assert destination2.remaining() == 0 ;
+        destination2.remove(2);
+        assert destination2.remaining() == 2 : " remain more or less than 2"; 
+        assert !destination3.isFull() ;
+        assert destination3.quantity() == 4;
+        destination3.flush();
+        assert destination3.quantity() == 0;
+    }
+}
+        
-- 
GitLab