Commit 31520476 authored by Alan's avatar Alan

Ajout chronometre

parent 330348bb
public class ArreteState implements ChronometreState {
private Chronometre chronometre;
public ArreteState(Chronometre chronometre) {
this.chronometre = chronometre;
}
@Override
public void demarrer() {
chronometre.setState(new DemarreState(chronometre));
}
@Override
public void pause() {
// Ne fait rien
}
@Override
public void arreter() {
// Ne fait rien
}
@Override
public void reset() {
chronometre.setTempsEcoule(0);
}
}
public class Chronometre {
private ChronometreState state;
private long tempsEcoule;
public Chronometre() {
this.state = new ArreteState(this);
this.tempsEcoule = 0;
}
public void setState(ChronometreState state) {
this.state = state;
}
public long getTempsEcoule() {
return tempsEcoule;
}
public void demarrer() {
state.demarrer();
}
public void pause() {
state.pause();
}
public void arreter() {
state.arreter();
}
public void reset() {
state.reset();
}
}
public interface ChronometreState {
void demarrer();
void pause();
void arreter();
void reset();
}
public class DemarreState implements ChronometreState {
private Chronometre chronometre;
public DemarreState(Chronometre chronometre) {
this.chronometre = chronometre;
}
@Override
public void demarrer() {
// Ne fait rien
}
@Override
public void pause() {
chronometre.setState(new PauseState(chronometre));
}
@Override
public void arreter() {
chronometre.setState(new ArreteState(chronometre));
}
@Override
public void reset() {
chronometre.setTempsEcoule(0);
}
}
\ No newline at end of file
public class Main {
public static void main(String[] args) {
Chronometre chronometre = new Chronometre();
chronometre.demarrer();
// Le chronomètre est en marche
System.out.println("Temps écoulé : " + chronometre.getTempsEcoule());
chronometre.pause();
// Le chronomètre est en pause
System.out.println("Temps écoulé : " + chronometre.getTempsEcoule());
chronometre.arreter();
// Le chronomètre est arrêté
System.out.println("Temps écoulé : " + chronometre.getTempsEcoule());
chronometre.reset();
// Le chronomètre est remis à zéro
System.out.println("Temps écoulé : " + chronometre.getTempsEcoule());
}
}
\ No newline at end of file
public class PauseState implements ChronometreState {
private Chronometre chronometre;
public PauseState(Chronometre chronometre) {
this.chronometre = chronometre;
}
@Override
public void demarrer() {
chronometre.setState(new DemarreState(chronometre));
}
@Override
public void pause() {
// Ne fait rien
}
@Override
public void arreter() {
chronometre.setState(new ArreteState(chronometre));
}
@Override
public void reset() {
chronometre.setTempsEcoule(0);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment