Module Server, clss FileStockDao : centralisation du code de sauvegarde des données, modif algo méthd applySale (voir desc)

Modif de l'algo de la méthode FileStockDao::applySale :
L'attribut Product::amountSold n'est modifié que pour les produits directement contenu dans la commande, et n'est donc plus appliqué récursivement aux composants de ces produits.
This commit is contained in:
Notmoo 2017-07-30 22:25:40 +02:00
parent 6e593ec188
commit 8d2d46d453

View File

@ -57,7 +57,7 @@ public class FileStockDao implements IStockDao {
product.setId(nextProductId); product.setId(nextProductId);
this.products.put(nextProductId, product); this.products.put(nextProductId, product);
generateNextProductId(); generateNextProductId();
save(this.products); saveToFile();
} }
/** /**
@ -67,7 +67,7 @@ public class FileStockDao implements IStockDao {
Product product = getProduct(id); Product product = getProduct(id);
if(product!=null){ if(product!=null){
this.products.remove(product); this.products.remove(product);
save(this.products); saveToFile();
} }
} }
@ -78,23 +78,38 @@ public class FileStockDao implements IStockDao {
if(this.products.containsKey(id)){ if(this.products.containsKey(id)){
product.setId(id); product.setId(id);
this.products.put(id, product); this.products.put(id, product);
saveToFile();
} }
} }
@Override @Override
public void applySale(SaleContent saleContent) throws IllegalArgumentException { public void applySale(SaleContent saleContent) throws IllegalArgumentException {
try { try {
saleContent.getProductList().forEach(product -> applyRecursiveStockRemoval(product, saleContent.getProductAmount(product))); saleContent.getProductList().forEach(product -> {
applyRecursiveStockRemoval(product, saleContent.getProductAmount(product));
applySoldCounterIncrease(product, saleContent.getProductAmount(product));
});
saveToFile();
}catch (IllegalStateException e){ }catch (IllegalStateException e){
loadFromFile(); loadFromFile();
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
} }
private void applySoldCounterIncrease(Product product, Integer amount) {
Product correspondingProduct = getProduct(product.getId());
if(correspondingProduct!=null){
correspondingProduct.setAmountSold(correspondingProduct.getAmountSold() + amount);
}else{
StringBuffer sb = new StringBuffer("StockService>StockDao : Un produit vendu ne correspond pas à un produit connu : ");
sb.append(product.getId()).append(" - ").append(product.getName()).append("(").append(product.getCategory()).append(")");
throw new IllegalStateException(sb.toString());
}
}
private void applyRecursiveStockRemoval(Product product, int amount)throws IllegalStateException{ private void applyRecursiveStockRemoval(Product product, int amount)throws IllegalStateException{
Product correspondingProduct = getProduct(product.getId()); Product correspondingProduct = getProduct(product.getId());
if(correspondingProduct!=null) { if(correspondingProduct!=null) {
correspondingProduct.setAmountSold(correspondingProduct.getAmountSold() + amount);
correspondingProduct.setAmountRemaining(correspondingProduct.getAmountRemaining() - amount); correspondingProduct.setAmountRemaining(correspondingProduct.getAmountRemaining() - amount);
correspondingProduct.getComponents().forEach(component -> applyRecursiveStockRemoval(component, amount)); correspondingProduct.getComponents().forEach(component -> applyRecursiveStockRemoval(component, amount));
}else{ }else{
@ -142,6 +157,10 @@ public class FileStockDao implements IStockDao {
return loadedData; return loadedData;
} }
private void saveToFile() {
save(this.products);
}
private void save(Map<Long, Product> products){ private void save(Map<Long, Product> products){
try{ try{
FileUtil.createFileIfNotExist(STOCK_FILE_NAME); FileUtil.createFileIfNotExist(STOCK_FILE_NAME);