exam modal dev aout 2023

This commit is contained in:
2024-01-14 10:00:35 +01:00
parent df9e43e534
commit a66a649b48
21 changed files with 703 additions and 0 deletions

View File

@ -0,0 +1,14 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package question1;
public class App {
public String getGreeting() {
return "Hello World!";
}
public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
}

View File

@ -0,0 +1,49 @@
package question1;
import java.util.HashMap;
public class InventoryManagement {
// cnt[zone][product] = quantity
HashMap<Integer, HashMap<Integer, Integer>> cnt = new HashMap<>();
// ajoute dans une zone du stock une quantité donnée dun produit donné
void replenish(int product, int quantity, int destination_zone) throws ZoneFullException{
cnt.putIfAbsent(destination_zone, new HashMap<>());
cnt.get(destination_zone).putIfAbsent(product, 0);
Integer q = cnt.get(destination_zone).get(product) + quantity;
if(q > 100)
throw new ZoneFullException();
cnt.get(destination_zone).put(product, q);
}
// retire dune zone spécifique du stock une quantité donnée dun produit donné pour lenvoyer vers le réseau de distribution
void distribute(int product, int source_zone, int quantity) throws StockOutException{
cnt.putIfAbsent(source_zone, new HashMap<>());
cnt.get(source_zone).putIfAbsent(product, 0);
Integer q = cnt.get(source_zone).get(product) - quantity;
if(q < 0)
throw new StockOutException();
cnt.get(source_zone).put(product, q);
}
// déplace une quantité donnée dun produit vers une autre zone
void move(int product, int quantity, int source_zone, int destination_zone) throws StockOutException, ZoneFullException {
distribute(product, source_zone, quantity);
try {
replenish(product, quantity, destination_zone);
}catch(ZoneFullException e){
replenish(product, quantity, source_zone);
throw e;
}
}
// retourne la quantité disponible dun produit donné dans le stock, toutes zones confondues
int quantity(int product){
int sum[] = {0};
cnt.forEach((key, value) -> {
sum[0] += value.getOrDefault(product, 0);
});
return sum[0];
}
}

View File

@ -0,0 +1,6 @@
package question1;
public class StockOutException extends Exception{
}

View File

@ -0,0 +1,4 @@
package question1;
public class ZoneFullException extends Exception {}

View File

@ -0,0 +1,14 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package question1;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class AppTest {
@Test void appHasAGreeting() {
App classUnderTest = new App();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
}
}

View File

@ -0,0 +1,55 @@
package question1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
public class InventoryManagementTest {
@Test
public void testExceptions() throws Exception {
InventoryManagement iv = new InventoryManagement();
iv.replenish(0, 10, 0);
assertThrows(ZoneFullException.class, () -> iv.replenish(0, 96, 0));
}
@Test
public void testExceptions2() throws Exception {
InventoryManagement iv = new InventoryManagement();
assertThrows(StockOutException.class, () -> iv.distribute(0, 0, 92));
}
@Test
public void testQuantity() throws Exception {
InventoryManagement iv = new InventoryManagement();
iv.replenish(2, 42, 0);
iv.replenish(2, 21, 1);
iv.replenish(2, 0, 3);
assertEquals(63, iv.quantity(2));
}
@Test
public void testMove() throws Exception {
InventoryManagement iv = new InventoryManagement();
iv.replenish(42, 50, 1);
iv.move(42, 21, 1, 2);
assertEquals(50, iv.quantity(42));
}
@Test
public void testFailure() throws Exception {
InventoryManagement iv = new InventoryManagement();
iv.replenish(42, 50, 1);
iv.replenish(42, 50, 2);
assertEquals(5050, iv.quantity(42)); // Should return a Failure
}
@Test
public void testError() throws Exception {
InventoryManagement iv = new InventoryManagement();
iv.replenish(42, 100, 1);
iv.replenish(42, 1, 1); // Should create an error
}
}