package src.fr.impl; import src.fr.api.Category; import src.fr.api.PartType; import src.fr.api.CompatibilityManager; import src.fr.api.Configuration; import src.fr.api.Part; import java.util.Optional; import java.util.Set; public class ConfigurationImpl implements Configuration { private Set selectedParts; private CompatibilityManager compatibilityManager; private static int NB_CATEGORY = 4; public ConfigurationImpl(CompatibilityManager cm){ this.compatibilityManager=cm; } @Override public boolean isValid() { for(Part pt: selectedParts){ //Verifier Requirements for(PartType require : compatibilityManager.getRequirements(pt.getType())){ //if(!selectedParts.contains(require)) return false; //IN V2, instead of verify if it's in selectedParts, //we will compare its type and selectedParts //boolean no_error = true; for(Part selectPart : selectedParts){ if (selectPart.getType().equals(require)){ return false; //no_error= true; //break; } } } //Verifier Incompabilities for(PartType incompa : compatibilityManager.getIncompatibilities(pt.getType())){ for (Part selectPart : selectedParts){ if(selectPart.getType().equals(incompa)){ return false; } } } } return true; } public int getPriceTotal(){ int prix_final=0; if(this.isValid()){ for(Part pt: selectedParts){ Optional price_opt =pt.getProperty("prix"); if(price_opt.isPresent()){ int prix_piece= Integer.parseInt(price_opt.get()); prix_final += prix_piece; } } return prix_final; }else{ return -1; } } @Override public boolean isComplete() { if(selectedParts.size()==NB_CATEGORY){ return true; } return false; } @Override public Set getSelectedParts() { return selectedParts; } @Override public void selectPart(PartType chosenPart) { Category category_chosenPart = chosenPart.getCategory(); //Vérifier s'il y a des pièces dans la même catégorie, si oui return for(Part pt: selectedParts){ if(pt.getCategory().equals(category_chosenPart)){ System.out.println("Il y a une pièce dans la même catégorie dans la configuration"); selectedParts.remove(pt); selectedParts.add(((PartTypeImpl)chosenPart).newInstance()); return; } } selectedParts.add( ((PartTypeImpl)chosenPart).newInstance() ); } @Override public Optional getSelectionForCategory(Category category) { for( Part pt: selectedParts){ if(pt.getCategory().equals(category)){ return Optional.of(pt); } } return Optional.empty(); } @Override public void unselectPartType(Category categoryToClear) { for( Part pt: selectedParts){ if(pt.getCategory().equals(categoryToClear)){ selectedParts.remove(pt); } } System.out.println("Il n'y a pas des pièces dans cette catégorie"); } @Override public void clear() { this.selectedParts.clear();; } }