exam modal dev aout 2023
This commit is contained in:
@ -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());
|
||||
}
|
||||
}
|
@ -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 d’un 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 d’une zone spécifique du stock une quantité donnée d’un produit donné pour l’envoyer 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 d’un 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 d’un 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];
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package question1;
|
||||
|
||||
public class StockOutException extends Exception{
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,4 @@
|
||||
package question1;
|
||||
|
||||
public class ZoneFullException extends Exception {}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
@ -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
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user