Code Java de la Calculatrice graphique (MVP + Commande)

/*
* Copyright (c) 2019
* ENSICAEN, École publique d'ingénieurs et centres de recherche, Caen, FRANCE.
* https://www.ensicaen.fr
*
* This file is licenced under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
public class Vue {
    private Presentation _presentation;
    @FXML
    private TextField _ecranNombre;
    @FXML
    private TextArea _ecranResultat;
    public void setPresentation( Presentation presentation ) {
        _presentation = presentation;
    }
    public void afficheEcranResultat( float resultat ) {
        _ecranResultat.setText(String.valueOf(resultat));
    }
    public void afficheEcranNombre( String text ) {
        _ecranNombre.setText(text);
    }
    @FXML
    public void handleBoutonChiffre( ActionEvent actionEvent ) {
        String chiffre = ((Button) actionEvent.getSource()).getText();
        _presentation.valideChiffre(chiffre);
    }
    @FXML
    public void handleBoutonOperation( ActionEvent actionEvent ) {
        String operation = ((Button) actionEvent.getSource()).getText();
        _presentation.valideOperation(operation);
    }
    @FXML
    public void handleDefaire() {
        _presentation.defaire();
    }
    @FXML
    public void handleRefaire() {
        _presentation.refaire();
    }
    @FXML
    public void handleBoutonEfface() {
        _presentation.efface();
    }
    @FXML
    public void handleBoutonAnnule() {
        _presentation.annule();
    }
}
public final class Presentation {
    private Modele _modele;
    private Vue _vue;
    private String _nombreCourant = "";
    private int _prochainDefaire = -1;
    private List<Commande> _historyList = new ArrayList<>();
    public void setModele( Modele modele ) {
        _modele = modele;
    }
    public void setVue( Vue vue ) {
        _vue = vue;
    }
    public void valideChiffre( String chiffre ) {
        _nombreCourant += chiffre;
        _vue.afficheEcranNombre(_nombreCourant);
    }
    public void valideOperation( String operation ) {
        float operande = Float.parseFloat(_nombreCourant);
        switch (operation) {
            case "+":
                executeCommande(new CommandeAjout(operande, _modele));
                break;
            case "-":
                executeCommande(new CommandeSoustrait(operande, _modele));
                break;
            case "*":
                executeCommande(new CommandeMultiplie(operande, _modele));
                break;
            case "/":
                executeCommande(new CommandeDivise(operande, _modele));
                break;
        }
        _nombreCourant = "";
    }
    public void efface() {
        _nombreCourant = "";
        _vue.afficheEcranNombre(_nombreCourant);
        _modele.annule();
    }
    public void annule() {
        if (!_nombreCourant.isEmpty()) {
            _nombreCourant = _nombreCourant.substring(0, _nombreCourant.length() - 1);
        }
        _vue.afficheEcranNombre(_nombreCourant);
    }
    public void executeCommande( Commande commande ) {
        trimHistoryList();
        _historyList.add(commande);
        _prochainDefaire++;
        float resultat = commande.execute();
        _vue.afficheEcranResultat(resultat);
    }
    public void defaire() {
        if (_prochainDefaire < 0) {
            return;
        }
        Commande commande = _historyList.get(_prochainDefaire);
        float resultat = commande.defaire();
        _vue.afficheEcranResultat(resultat);
        _prochainDefaire--;
    }
    public void refaire() {
        if (_prochainDefaire > _historyList.size() - 1) {
            return;
        }
        Commande commande = _historyList.get(_prochainDefaire);
        float resultat = commande.refaire();
        _vue.afficheEcranResultat(resultat);
        _prochainDefaire++;
    }
    private void trimHistoryList() {
        List<Commande> listEcourtee = new ArrayList<>();
        for (int i = 0; i < _prochainDefaire + 1; i++) {
            listEcourtee.add(_historyList.get(i));
        }
        _historyList = listEcourtee;
    }
}
abstract class Commande {
    private float _operande;
    private Modele _modele;
    public Commande( float operande, Modele modele ) {
        _operande = operande;
        _modele = modele;
    }
    public abstract float execute();
    public abstract float defaire();
    public float refaire() {
        return execute();
    }
    protected Modele getModele() {
        return _modele;
    }
    protected float getOperande() {
        return _operande;
    }
}
class CommandeAjout extends Commande {
    public CommandeAjout( float operande, Modele modele ) {
        super(operande, modele);
    }
    @Override
    public float execute() {
        return getModele().ajoute(getOperande());
    }
    @Override
    public float defaire() {
        return getModele().soustrait(getOperande());
    }
}
class CommandeSoustrait extends Commande {
    public CommandeSoustrait( float operande, Modele modele ) {
        super(operande, modele);
    }
    @Override
    public float execute() {
        return getModele().soustrait(getOperande());
    }
    @Override
    public float defaire() {
        return getModele().ajoute(getOperande());
    }
}
class CommandeMultiplie extends Commande {
    public CommandeMultiplie( float operande, Modele modele ) {
        super(operande, modele);
    }
    @Override
    public float execute() {
        return getModele().multiplie(getOperande());
    }
    @Override
    public float defaire() {
        return getModele().divise(getOperande());
    }
}
class CommandeDivise extends Commande {
    public CommandeDivise( float operande, Modele modele ) {
        super(operande, modele);
    }
    @Override
    public float execute() {
        return getModele().divise(getOperande());
    }
    @Override
    public float defaire() {
        return getModele().multiplie(getOperande());
    }
}
public final class Modele {
    private float _register = 0;
    public void reinitialise() {
        _register = 0;
    }
    public float ajoute( float operande ) {
        return _register += operande;
    }
    public float soustrait( float operande ) {
        return _register -= operande;
    }
    public float multiplie( float operande ) {
        return _register *= operande;
    }
    public float divise( float operande ) {
        return _register /= operande;
    }
}



Last modified: Monday, 28 October 2019, 10:24 AM