Skip to content

Instantly share code, notes, and snippets.

@MatonAnthony
Created January 17, 2017 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatonAnthony/ee5d4a6b3575e144f9fbe686bff944a3 to your computer and use it in GitHub Desktop.
Save MatonAnthony/ee5d4a6b3575e144f9fbe686bff944a3 to your computer and use it in GitHub Desktop.
public class MachineACafe {
private int montantEnCours = 0;
private ToucheBoisson boisson = null;
private State etatCourant = State.INACTIF;
private static enum State{
INACTIF{
public void selectionnerBoisson(MachineACafe m,ToucheBoisson toucheBoisson) {
m.afficherPasAssez(toucheBoisson);
}
},
COLLECTE{
public void selectionnerBoisson(MachineACafe m,ToucheBoisson toucheBoisson) {
if (toucheBoisson.getPrix() > m.montantEnCours) {
m.boisson = toucheBoisson;
m.afficherPasAssez(m.boisson);
m.boisson = toucheBoisson;
m.etatCourant = PASASSEZ;
return;
}
m.montantEnCours -= toucheBoisson.getPrix();
m.afficherBoisson(toucheBoisson);
m.afficherMontant();
if (m.montantEnCours == 0)
m.etatCourant = INACTIF;
else
m.etatCourant = COLLECTE;
}
},
PASASSEZ{
public void entrerMonnaie(MachineACafe m,Piece piece) {
m.etatCourant = COLLECTE;
}
};
public void entrerMonnaie(MachineACafe m,Piece piece) {
if (m.boisson.getPrix() > m.montantEnCours) {
m.afficherPasAssez(m.boisson);
} else {
m.montantEnCours -= m.boisson.getPrix();
m.afficherBoisson(m.boisson);
m.boisson = null;
m.afficherMontant();
if (m.montantEnCours == 0)
m.etatCourant = INACTIF;
else
m.etatCourant = COLLECTE;
}
}
public void selectionnerBoisson(MachineACafe m,ToucheBoisson toucheBoisson) {
}
public void rendreMonnaie(MachineACafe m) {
if (m.etatCourant != INACTIF) {
m.afficherRetour();
m.montantEnCours = 0;
m.boisson = null;
}
m.etatCourant = INACTIF;
}
};
public void afficherMontant() {
System.out.println(montantEnCours + " cents disponibles");
}
public void afficherRetour() {
System.out.println(montantEnCours + " cents rendus");
}
public void afficherPasAssez(ToucheBoisson toucheBoisson) {
System.out.println("Vous n'avez pas introduit un montant suffisant pour un " + toucheBoisson);
System.out.println("Il manque encore " + (toucheBoisson.getPrix() - montantEnCours) + " cents");
}
public void afficherBoisson(ToucheBoisson toucheBoisson) {
System.out.println("Voici un " + toucheBoisson);
}
public void entrerMonnaie(Piece piece) {
etatCourant.entrerMonnaie(this, piece);
}
public void selectionnerBoisson(ToucheBoisson toucheBoisson) {
etatCourant.selectionnerBoisson(this,toucheBoisson);
}
public void rendreMonnaie() {
etatCourant.rendreMonnaie(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment