Java:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Automat implements ActionListener {
private AutomatModel atModel;
private AutomatGui atGui;
public Automat (AutomatGui atGui){
this.atGui = atGui;
this.atModel = new AutomatModel();
}
public void auswahl(int auswahl){
if (auswahl == 1)rueckgabe(atModel.getPreis1(),atModel.getEingabe());
if (auswahl == 2)rueckgabe(atModel.getPreis2(),atModel.getEingabe());
if (auswahl == 3)rueckgabe(atModel.getPreis3(),atModel.getEingabe());
if (auswahl == 4)rueckgabe(atModel.getPreis4(),atModel.getEingabe());
if (auswahl == 5)rueckgabe(atModel.getPreis5(),atModel.getEingabe());
if (auswahl == 6)rueckgabe(atModel.getPreis6(),atModel.getEingabe());
if (auswahl == 7)rueckgabe(atModel.getPreis7(),atModel.getEingabe());
if (auswahl == 8)rueckgabe(atModel.getPreis8(),atModel.getEingabe());
if (auswahl == 9)rueckgabe(atModel.getPreis9(),atModel.getEingabe());
}
public double rueckgabe(double ware, double eingabe){
if (ware > eingabe) return -1;
else return ware - eingabe;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == atGui.getSnickers())atGui.setText1("Snickers :" + atModel.getPreis1() +"€");
if(e.getSource() == atGui.getBounty())atGui.setText1("Bounty :" + atModel.getPreis2() +"€");
if(e.getSource() == atGui.getWasser())atGui.setText1("Wasser :" + atModel.getPreis3() +"€");
if(e.getSource() == atGui.getMars())atGui.setText1("Mars :" + atModel.getPreis4() +"€");
if(e.getSource() == atGui.getHaribo())atGui.setText1("Haribo :" + atModel.getPreis5() +"€");
if(e.getSource() == atGui.getCokaCola())atGui.setText1("CokaCola :" + atModel.getPreis6() +"€");
if(e.getSource() == atGui.getMentos())atGui.setText1("Mentos :" + atModel.getPreis7() +"€");
if(e.getSource() == atGui.getErdnuesse())atGui.setText1("Erdnüsse :" + atModel.getPreis8() +"€");
if(e.getSource() == atGui.getEistee())atGui.setText1("Eistee :" + atModel.getPreis9() +"€");
if(e.getSource() == atGui.getCent1()){
atModel.setEingabe(atModel.getEingabe()+0.01);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getCent2()){
atModel.setEingabe(atModel.getEingabe()+0.01);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getCent5()){
atModel.setEingabe(atModel.getEingabe()+0.01);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getCent10()){
atModel.setEingabe(atModel.getEingabe() +0.10);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getCent20()){
atModel.setEingabe(atModel.getEingabe() + 0.20);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getCent50()){
atModel.setEingabe(atModel.getEingabe() + 0.50);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getEuro1()){
atModel.setEingabe(atModel.getEingabe() + 1);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getEuro2()){
atModel.setEingabe(atModel.getEingabe() + 2);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getEuro5()){
atModel.setEingabe(atModel.getEingabe() + 5);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getEuro10()){
atModel.setEingabe(atModel.getEingabe() + 10);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
if(e.getSource() == atGui.getEuro20()){
atModel.setEingabe(atModel.getEingabe() + 20);
atGui.setText2("Eingabe :"+ atModel.getEingabe() +"€");
}
}
}
Java:
public class AutomatModel{
private double preis1 = 1.99;
private double preis2 = 1.99;
private double preis3 = 0.49;
private double preis4 = 1.99;
private double preis5 = 1.49;
private double preis6 = 2.49;
private double preis7 = 1.69;
private double preis8 = 0.69;
private double preis9 = 2.49;
private double eingabe = 0 ;
private String text = "Bitte ware auswählen !";
public double getPreis1() {
return preis1;
}
public double getPreis2() {
return preis2;
}
public double getPreis3() {
return preis3;
}
public double getPreis4() {
return preis4;
}
public double getPreis5() {
return preis5;
}
public double getPreis6() {
return preis6;
}
public double getPreis7() {
return preis7;
}
public double getPreis8() {
return preis8;
}
public double getPreis9() {
return preis9;
}
public double getEingabe() {
return eingabe;
}
public void setEingabe(double eingabe) {
this.eingabe = eingabe;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* Beschreibung
*
* @version 1.0 vom 20.10.2017
* @author
*/
public class AutomatGui extends JFrame {
// Anfang Attribute
private JButton bSnickers = new JButton();
private JButton bBounty = new JButton();
private JButton bWasser = new JButton();
private JButton bMars = new JButton();
private JButton bHaribo = new JButton();
private JButton bCokaCola = new JButton();
private JButton bMentos = new JButton();
private JButton bErdnuesse = new JButton();
private JButton bEistee = new JButton();
private JTextPane fenster = new JTextPane();
private JButton bCent1 = new JButton();
private JButton bCent2 = new JButton();
private JButton bCent5 = new JButton();
private JButton bCent10 = new JButton();
private JButton bCent20 = new JButton();
private JButton bCent50 = new JButton();
private JButton bEuro1 = new JButton();
private JButton bEuro2 = new JButton();
private JButton bEuro5 = new JButton();
private JButton bEuro10 = new JButton();
private JButton bEuro20 = new JButton();
private JTextPane eingabef = new JTextPane();
private JButton bBestaetigen = new JButton("Best\u00E4tigen");
// Ende Attribute
public AutomatGui() {
// Frame-Initialisierung
super();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 327;
int frameHeight = 468;
setSize(480, 512);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setTitle("Automat");
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
bSnickers.setFont(new Font("Tahoma", Font.BOLD, 11));
// Anfang Komponenten
bSnickers.setBounds(24, 48, 65, 65);
bSnickers.setText("Snickers");
bSnickers.setMargin(new Insets(2, 2, 2, 2));
bSnickers.addActionListener(new Automat(this));
cp.add(bSnickers);
bBounty.setFont(new Font("Tahoma", Font.BOLD, 11));
bBounty.setBounds(104, 48, 65, 65);
bBounty.setText("Bounty");
bBounty.setMargin(new Insets(2, 2, 2, 2));
bBounty.addActionListener(new Automat(this));
cp.add(bBounty);
bWasser.setFont(new Font("Tahoma", Font.BOLD, 11));
bWasser.setBounds(184, 48, 65, 65);
bWasser.setText("Wasser");
bWasser.setMargin(new Insets(2, 2, 2, 2));
bWasser.addActionListener(new Automat(this));
bWasser.setIconTextGap(40);
cp.add(bWasser);
bMars.setFont(new Font("Tahoma", Font.BOLD, 11));
bMars.setBounds(24, 128, 65, 65);
bMars.setText("Mars");
bMars.setMargin(new Insets(2, 2, 2, 2));
bMars.addActionListener(new Automat(this));
cp.add(bMars);
bHaribo.setFont(new Font("Tahoma", Font.BOLD, 11));
bHaribo.setBounds(104, 128, 65, 65);
bHaribo.setText("Haribo");
bHaribo.setMargin(new Insets(2, 2, 2, 2));
bHaribo.addActionListener(new Automat(this));
cp.add(bHaribo);
bCokaCola.setBounds(184, 128, 65, 65);
bCokaCola.setText("CokaCola");
bCokaCola.setMargin(new Insets(2, 2, 2, 2));
bCokaCola.addActionListener(new Automat(this));
bCokaCola.setFont(new Font("Tahoma", Font.BOLD, 11));
cp.add(bCokaCola);
bMentos.setFont(new Font("Tahoma", Font.BOLD, 11));
bMentos.setBounds(24, 208, 65, 65);
bMentos.setText("Mentos");
bMentos.setMargin(new Insets(2, 2, 2, 2));
bMentos.addActionListener(new Automat(this));
cp.add(bMentos);
bErdnuesse.setFont(new Font("Tahoma", Font.BOLD, 11));
bErdnuesse.setBounds(104, 208, 65, 65);
bErdnuesse.setText("Erdnüsse");
bErdnuesse.setMargin(new Insets(2, 2, 2, 2));
bErdnuesse.addActionListener(new Automat(this));
cp.add(bErdnuesse);
bEistee.setFont(new Font("Tahoma", Font.BOLD, 11));
bEistee.setBounds(184, 208, 65, 65);
bEistee.setText("Eistee");
bEistee.setMargin(new Insets(2, 2, 2, 2));
bEistee.addActionListener(new Automat(this));
cp.add(bEistee);
JButton btnNewButton = new JButton("R");
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton.setBounds(259, 50, 50, 48);
getContentPane().add(btnNewButton);
JButton service = new JButton("Service");
service.addActionListener(new Automat(this));
service.setBounds(311, 208, 153, 50);
getContentPane().add(service);
service.setVisible(false);
fenster.setEditable(false);
fenster.setBounds(311, 48, 153, 23);
getContentPane().add(fenster);
fenster.setText("Bitte ware auswählen !");
JLabel label = new JLabel();
label.setText("M\u00FCnzen");
label.setFont(new Font("Dialog", Font.BOLD, 16));
label.setBounds(24, 303, 65, 23);
getContentPane().add(label);
bCent1.addActionListener(new Automat(this));
bCent1.setText("1ct");
bCent1.setMargin(new Insets(2, 2, 2, 2));
bCent1.setBounds(24, 343, 50, 25);
getContentPane().add(bCent1);
bCent2.addActionListener(new Automat(this));
bCent2.setText("2ct");
bCent2.setMargin(new Insets(2, 2, 2, 2));
bCent2.setBounds(24, 379, 50, 25);
getContentPane().add(bCent2);
bCent5.addActionListener(new Automat(this));
bCent5.setText("5ct");
bCent5.setMargin(new Insets(2, 2, 2, 2));
bCent5.setBounds(24, 415, 50, 25);
getContentPane().add(bCent5);
bCent10.addActionListener(new Automat(this));
bCent10.setText("10ct");
bCent10.setMargin(new Insets(2, 2, 2, 2));
bCent10.setBounds(24, 451, 50, 25);
getContentPane().add(bCent10);
bCent20.addActionListener(new Automat(this));
bCent20.setText("20ct");
bCent20.setMargin(new Insets(2, 2, 2, 2));
bCent20.setBounds(84, 343, 50, 25);
getContentPane().add(bCent20);
bCent50.addActionListener(new Automat(this));
bCent50.setText("50ct");
bCent50.setMargin(new Insets(2, 2, 2, 2));
bCent50.setBounds(84, 379, 50, 25);
getContentPane().add(bCent50);
bEuro1.addActionListener(new Automat(this));
bEuro1.setText("1 EUR");
bEuro1.setMargin(new Insets(2, 2, 2, 2));
bEuro1.setBounds(84, 416, 50, 25);
getContentPane().add(bEuro1);
bEuro2.addActionListener(new Automat(this));
bEuro2.setText("2 EUR");
bEuro2.setMargin(new Insets(2, 2, 2, 2));
bEuro2.setBounds(84, 452, 50, 25);
getContentPane().add(bEuro2);
JLabel label_1 = new JLabel();
label_1.setText("Scheine");
label_1.setFont(new Font("Dialog", Font.BOLD, 16));
label_1.setBounds(184, 303, 70, 23);
getContentPane().add(label_1);
bEuro5.addActionListener(new Automat(this));
bEuro5.setText("5 EUR");
bEuro5.setMargin(new Insets(2, 2, 2, 2));
bEuro5.setBounds(184, 343, 50, 25);
getContentPane().add(bEuro5);
bEuro10.addActionListener(new Automat(this));
bEuro10.setText("10 EUR");
bEuro10.setMargin(new Insets(2, 2, 2, 2));
bEuro10.setBounds(184, 380, 50, 25);
getContentPane().add(bEuro10);
bEuro20.addActionListener(new Automat(this));
bEuro20.setText("20 EUR");
bEuro20.setMargin(new Insets(2, 2, 2, 2));
bEuro20.setBounds(184, 416, 50, 25);
getContentPane().add(bEuro20);
bBestaetigen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
bBestaetigen.setBounds(311, 100, 153, 23);
getContentPane().add(bBestaetigen);
eingabef.setText("Einwurf :");
eingabef.setEditable(false);
eingabef.setBounds(311, 75, 153, 23);
getContentPane().add(eingabef);
// Ende Komponenten
setVisible(true);
} // end of public Automat
// Anfang Methoden
public static void main(String[] args) {
new AutomatGui();
} // end of main
public JButton getSnickers() {
return bSnickers;
}
public JButton getBounty() {
return bBounty;
}
public JButton getWasser() {
return bWasser;
}
public JButton getMars() {
return bMars;
}
public JButton getHaribo() {
return bHaribo;
}
public JButton getCokaCola() {
return bCokaCola;
}
public JButton getMentos() {
return bMentos;
}
public JButton getErdnuesse() {
return bErdnuesse;
}
public JButton getEistee() {
return bEistee;
}
public JButton getCent1() {
return bCent1;
}
public JButton getCent2() {
return bCent2;
}
public JButton getCent5() {
return bCent5;
}
public JButton getCent10() {
return bCent10;
}
public JButton getCent20() {
return bCent20;
}
public JButton getCent50() {
return bCent50;
}
public JButton getEuro1() {
return bEuro1;
}
public JButton getEuro2() {
return bEuro2;
}
public JButton getEuro5() {
return bEuro5;
}
public JButton getEuro10() {
return bEuro10;
}
public JButton getEuro20() {
return bEuro20;
}
public void setText1(String t) {
fenster.setText(t);
}
public void setText2(String v) {
eingabef.setText(v);
}
} // end of class Automat
Ich hab die Aufgabe bekommen über die Ferien ein Automaten zu erstellen.
Jetzt hab ich aber ein Problem von zeile 41 bis 84 in Automat. Die Eingabe setzt sich außerhalb der if abfrage wieder auf 0 d.h. wenn ich den 1 Cent button drücke addiert sich das wie ich will aber wenn ich den 1 Cent button und danach den 2 cent button drücke ist eingabe wieder 0.
Ich bedanke mich für jegliche Hilfe .