public class DistribObGuiTest {
public static void main(String args[]) {
DistribObGui distrib = new DistribObGui();
distrib.show();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DistribObGui extends JFrame implements ActionListener {
private JTextField text = new JTextField();
private JLabel label = new JLabel();
private DistribOb distrib;
public DistribObGui() {
distrib = new DistribOb();
init();
}
public void init() {
setTitle("Distributeur de café");
setSize(200,200);
Container contentPane = this.getContentPane();
JLabel titre = new JLabel("Saisir la valeur de la piece ("+distrib.pieces()
+")");
contentPane.add(titre, java.awt.BorderLayout.NORTH);
contentPane.add(text, java.awt.BorderLayout.CENTER);
contentPane.add(label, java.awt.BorderLayout.SOUTH);
text.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
int piece = Integer.parseInt(text.getText());
if (distrib.controlePiece(piece))
{
label.setText(distrib.traitement(piece));
}
else
label.setText("Piece non acceptée");
text.setText("0");
}
}
public class DistribOb {
private static final int PRIX = 40;
private static final int P1 = 20;
private static final int P2= 10;
private static final int P3 = 5;
private int somme;
public DistribOb() {
somme = 0;
}
public void setSomme(int montant) {
somme += montant;
}
public int getSomme() {
return somme;
}
public boolean controlePiece(int montant) {
if (montant==P1 || montant==P2 || montant==P3)