import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
public class Fahrkartenautomat extends JFrame implements ActionListener
{
// left side of the table
JLabel mainHeader;
String main = " Fahrkartenautomat ";
JLabel fareZone;
String zone = " Tarifzone ";
JRadioButton rateA;
String rateAG = " Tarif A ";
JRadioButton rateB;
String rateBG = " Tarif B ";
JRadioButton rateC;
String rateCG = " Tarif C ";
JRadioButton rateD;
String rateDG = " Tarif D ";
// right side of the table
JLabel payScaleGroup;
String payScaleGroupG = " Tarifgruppe ";
JRadioButton childGroup;
String childGroupG = " Kindertarife ";
JRadioButton teenGroup;
String teenGroupG = " Jugendliche ";
JRadioButton adultGroup;
String adultGroupG = " Erwachsene ";
JRadioButton reducedGroup;
String reducedGroupG = " Ermäßigung ";
// the ticketprice Label and textfield
JLabel ticketPrice;
String ticketPriceG = " Fahrpreis: " + " (€)";
JTextField ticketPriceInput;
// the moneyinput Label and textfield
JLabel moneyInput;
String moneyInputG = " Einwurf: " + " (€)";
JTextField moneyInputField;
// creating the buttons
JButton enter;
String enterLabel1 = " Fahrkarte ";
JButton undo;
String undoLabel = " Rücksetzen ";
// creating the List components
JLabel ticket;
String ticketLabel = " Fahrkarte ";
JTextArea summary;
JLabel empty;
JLabel empty2;
JLabel empty3;
JLabel empty4;
JLabel change;
String changeLabel = " Wechselgeld";
JTextArea changeSummary;
private Container con;
// variables needed
double rateAPrice = 1.50;
double rateBPrice = 2.50;
double rateCPrice = 3.00;
double rateDPrice = 3.50;
double childPrice = 1.10;
double teenGroupPrice = 1.20;
double adultGroupPrice = 1.70;
double reducedPrice = 1.30;
double ratePrice;
double groupPrice;
double costs;
double coinInput;
double test;
double roundedChangeMoneyResult;
String rateDefinition;
String rateZoneDefinition;
String rateAString = "Tarif A ";
String rateBString = "Tarif B ";
String rateCString = "Tarif C ";
String rateDString = "Tarif D ";
String childGroupString = " Kinder ";
String teenGroupString = " Jugendliche ";
String adultGroupString = " Erwachsene ";
String reducedGroupString = " Ermäßigung ";
String ticketPriceOutPutString;
String ticketPriceOutPut;
String moneyFactorString;
double moneyFactorPriceDouble;
double moneyInputFactorDouble;
double changeMoneyResult;
// creating the frame with the objects
private Fahrkartenautomat()
{
super("Fahrkartenautomat");
mainHeader = new JLabel(main);
// left
fareZone = new JLabel(zone);
rateA = new JRadioButton(rateAG);
rateB = new JRadioButton(rateBG);
rateC = new JRadioButton(rateCG);
rateD = new JRadioButton(rateDG);
// right
payScaleGroup = new JLabel(payScaleGroupG);
childGroup = new JRadioButton(childGroupG);
teenGroup = new JRadioButton(teenGroupG);
adultGroup = new JRadioButton(adultGroupG);
reducedGroup = new JRadioButton(reducedGroupG);
// the ticket price components
ticketPrice = new JLabel(ticketPriceG);
ticketPriceInput = new JTextField();
ticketPriceInput.setHorizontalAlignment(JTextField.CENTER);
ticketPriceInput.setEditable(false);
// the moneyinput components
moneyInput = new JLabel(moneyInputG);
moneyInputField = new JTextField();
moneyInputField.setHorizontalAlignment(JTextField.CENTER);
// the button components
enter = new JButton(enterLabel1);
enter.setBorderPainted(true);
undo = new JButton(undoLabel);
undo.setBorderPainted(true);
// the textfieldsummary components
ticket = new JLabel(ticketLabel);
summary = new JTextArea(6,1);
summary.setEditable(false);
empty = new JLabel("");
empty2 = new JLabel("");
empty3 = new JLabel("");
empty4 = new JLabel("");
change = new JLabel(changeLabel);
changeSummary = new JTextArea(8,1);
changeSummary.setEditable(false);
// giving action commands to the diffrent components
rateA.setActionCommand("rateA");
rateB.setActionCommand("rateB");
rateC.setActionCommand("rateC");
rateD.setActionCommand("rateD");
childGroup.setActionCommand("childgroup");
teenGroup.setActionCommand("teengroup");
adultGroup.setActionCommand("adultgroup");
reducedGroup.setActionCommand("reducedgroup");
enter.setActionCommand("enter");
undo.setActionCommand("undo");
// using the GridBagLayout
GridBagLayout gbl = new GridBagLayout();
this.con = this.getContentPane();
this.con.setLayout( gbl );
// adding the diffrent Components to the GridBagLayout
addComponent(this.con, gbl ,this.mainHeader ,0, 0, 1, 1, 0.5, 0.5, GridBagConstraints.CENTER , GridBagConstraints.HORIZONTAL);
addComponent(this.con, gbl ,this.fareZone ,0, 2, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.payScaleGroup,2, 2, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.rateA ,0, 3, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.childGroup ,2, 3, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.rateB ,0, 4, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.teenGroup ,2, 4, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.rateC, 0, 5, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.adultGroup, 2, 5, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.rateD, 0, 6, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.ticketPrice, 1, 7, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
addComponent(this.con, gbl ,this.ticketPriceInput,2, 7, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
addComponent(this.con, gbl ,this.moneyInput, 1, 8, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
addComponent(this.con, gbl ,this.moneyInputField, 2, 8, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
addComponent(this.con, gbl ,this.enter, 0, 9, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.undo, 2, 9, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.ticket, 0, 10, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.summary, 2, 10, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
addComponent(this.con, gbl ,this.change, 0, 12, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.changeSummary, 2, 12, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
moneyInputField.addMouseListener( new java.awt.event.MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
moneyInputField.setText("");
}
});
// adding the ActionListener
rateA.addActionListener(this);
rateB.addActionListener(this);
rateC.addActionListener(this);
rateD.addActionListener(this);
childGroup.addActionListener(this);
teenGroup.addActionListener(this);
adultGroup.addActionListener(this);
reducedGroup.addActionListener(this);
enter.addActionListener(this);
undo.addActionListener(this);
}
public static void addComponent(Container cont, GridBagLayout gbl, Component c, int x, int y, int width, int height, double weightx, double weighty, int anchor, int gbconst)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = gbconst;
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.anchor = anchor;
gbl.setConstraints(c, gbc);
cont.add(c);
}
private void costCheck()
{
double roundedCosts;
String costsToString;
try
{
costs = ratePrice * groupPrice;
roundedCosts = Math.rint( costs * 100 ) / 100;
costsToString = "" + roundedCosts;
ticketPriceInput.setText(costsToString);
}
catch ( NumberFormatException e )
{
System.out.println( "Bitte Felder ausfüllen!" );
}
}
private void costResult()
{ try
{
ticketPriceOutPut = ticketPriceInput.getText();
summary.setText("Fahrkarte \n " + "\n" + "Tarifzone: "
+ rateDefinition + "\n" + "Tarifgruppe:" + rateZoneDefinition + "\n " + "Fahrpreis: " + ticketPriceOutPut);
}
catch ( NumberFormatException e )
{
System.out.println( "Bitte Felder ausfüllen222!" );
}
}
private void changeMoneyMethod()
{
try
{
moneyFactorString = ticketPriceInput.getText();
moneyFactorPriceDouble = Double.parseDouble(moneyFactorString);
ticketPriceOutPutString = moneyInputField.getText();
moneyInputFactorDouble = Double.parseDouble(ticketPriceOutPutString);
changeMoneyResult = moneyInputFactorDouble - moneyFactorPriceDouble;
if(moneyInputFactorDouble >= moneyFactorPriceDouble )
{
roundedChangeMoneyResult = Math.rint( changeMoneyResult * 100 ) / 100;
changeSummary.setText( "Ihr Wechselgeld: " + "" + roundedChangeMoneyResult + " €");
}
else
changeSummary.setText("mehr Geld einwerfen," + "\n" + "bitte!");
}
catch ( NumberFormatException e )
{
moneyInputField.setText("????");
changeSummary.setText("");
summary.setText("");
}
}
private void undoMethod()
{
summary.setText(""); changeSummary.setText("");
ticketPriceInput.setText("");moneyInputField.setText("");
ratePrice = 0;
groupPrice = 0;
rateA.setSelected(false);rateC.setSelected(false);rateD.setSelected(false);
rateB.setSelected(false);childGroup.setSelected(false);teenGroup.setSelected(false);
adultGroup.setSelected(false);reducedGroup.setSelected(false);
}
// connection of the diffrent methods and the explicit ActionCommands
public void actionPerformed(ActionEvent e)
{
// check the kind of command
if (e.getActionCommand().equals("rateA"))
{
rateA.setSelected(true);rateB.setSelected(false);rateC.setSelected(false);rateD.setSelected(false);
ratePrice = rateAPrice; costCheck();rateDefinition = rateAString;
}
else if (e.getActionCommand().equals("rateB"))
{
rateA.setSelected(false);rateC.setSelected(false);rateD.setSelected(false);
rateB.setSelected(true);ratePrice = rateBPrice; rateDefinition = rateBString; costCheck();
}
else if (e.getActionCommand().equals("rateC"))
{
rateA.setSelected(false);rateB.setSelected(false);rateC.setSelected(true);rateD.setSelected(false);
ratePrice = rateCPrice; rateDefinition = rateCString; costCheck();
}
else if (e.getActionCommand().equals("rateD"))
{
rateD.setSelected(true);rateC.setSelected(false);rateA.setSelected(false);rateB.setSelected(false);
ratePrice = rateDPrice; rateDefinition = rateDString; costCheck();
}
else if (e.getActionCommand().equals("childgroup"))
{
childGroup.setSelected(true);teenGroup.setSelected(false);reducedGroup.setSelected(false);
adultGroup.setSelected(false);groupPrice = childPrice;
rateZoneDefinition = childGroupString ; costCheck();
}
else if (e.getActionCommand().equals("teengroup"))
{
childGroup.setSelected(false);teenGroup.setSelected(true);reducedGroup.setSelected(false);
adultGroup.setSelected(false);groupPrice = teenGroupPrice; rateZoneDefinition = teenGroupString;
costCheck();
}
else if (e.getActionCommand().equals("adultgroup"))
{
adultGroup.setSelected(true);childGroup.setSelected(false);teenGroup.setSelected(false);
reducedGroup.setSelected(false); groupPrice = adultGroupPrice;
rateZoneDefinition = adultGroupString; costCheck();
}
else if (e.getActionCommand().equals("reducedgroup"))
{
reducedGroup.setSelected(true);
childGroup.setSelected(false);teenGroup.setSelected(false);
adultGroup.setSelected(false);groupPrice = reducedPrice;
rateZoneDefinition = reducedGroupString;costCheck();
}
else if (e.getActionCommand().equals("enter"))
{
costResult();changeMoneyMethod();
}
else if (e.getActionCommand().equals("undo"))
{
undoMethod();
}
}
// main method to create an "automat" Object
public static void main(String[] args)
{
Fahrkartenautomat automat1 = new Fahrkartenautomat();
WindowQuitter wquit = new WindowQuitter();
automat1.addWindowListener(wquit);
automat1.setSize(355, 600);
automat1.setResizable(false);
automat1.setVisible(true);
}
}
// Window Closing Adapter
class WindowQuitter extends WindowAdapter
{
public void windowClosing(WindowEvent ex)
{
System.exit(0);
}
}