I have a half complete coding which basically comes down to a gui interface. The problem is that I don't know how to get it working as ticket vending machine with fixed rates and fixed amount of cash being able to put in. So, far, I've done up to calculating total where nomatter what I do, it always comes up with a £0.00 as the totalamount answer. I am in desperate need of help. This is very frustrating. Below is the code for the TicketCalculation Class

import java.awt.*;
import java.text.*;
import java.awt.event.*;
import javax.swing.*;


public class TicketCalculation extends JFrame implements ActionListener {

    DecimalFormat pounds = new DecimalFormat("£#,##0.00");


    //creating and naming buttons and textfields
    private JButton twentypenceBtn = new JButton("20p");
    private JButton fiftypenceBtn = new JButton("50p");
    private JButton onepoundBtn = new JButton("£1");
    private JButton twopoundsBtn = new JButton("£2");
    private JButton fivepoundsBtn = new JButton("£5");
    private JButton tenpoundsBtn = new JButton("£10");
    private JButton twentypoundsBtn = new JButton("£20");
    private JButton C = new JButton("Calculate");

    private JButton frontBtn = new JButton("<<Front Stalls>>");
    private JButton private1Btn = new JButton("<<Private Box>>");
    private JButton middleBtn = new JButton("<<Middle Stalls>>");
    private JButton backBtn = new JButton("<<Back Stalls>>");
    private JButton calcBtn = new JButton("Calculate Bill");

    private JTextField tickettypeTxt = new JTextField(14);
    private JTextField stalltypeTxt = new JTextField(25);
    private JTextField amountticketsTxt = new JTextField(14);
    private JTextField totalamountTxt = new JTextField(10);
    private JTextField amountdueTxt = new JTextField(13);
    private JTextField amountpaidTxt = new JTextField(10);


    //creating labels
    private JLabel pickstall = new JLabel();
    private JLabel tictype = new JLabel ();
    private JLabel amontic = new JLabel();
    private JLabel ttamon = new JLabel();
    private JLabel amondue = new JLabel();
    private JLabel amonpaid = new JLabel();
    private JLabel label5 = new JLabel();
    private JLabel spacing6 = new JLabel();
    private JLabel spacing7 = new JLabel();
    private JLabel spacing8 = new JLabel();
    private JLabel spacing9 = new JLabel();
    private JLabel spacing10 = new JLabel();


    //image icon declarations
    private ImageIcon paycount = new ImageIcon(getClass().getResource("paycount.jpg"));

    double middleprice;
    double privateprice;
    double backprice;
    double frontprice;
    double type; 
    int number;

    public static void main(String[] args){
        new TicketCalculation();
    }


    public TicketCalculationt(){
        setLayout(new BorderLayout());
        setSize(650,750);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        //naming labels
        pickstall = new JLabel("==> Pick a Stall Type: ");
        tictype = new JLabel ("==> Price of a Ticket: £");
        amontic = new JLabel("==> Amount of Tickets: ");
        ttamon = new JLabel("==> Total Amount: ");
        amondue = new JLabel("==> Amount Due: £");
        amonpaid = new JLabel("==> Amount Paid: £");
        label5 = new JLabel(paycount);
        spacing6 = new JLabel("                        ");
        spacing7 = new JLabel("                                                                                                                     ");
        spacing8 = new JLabel("                                                                                                                                                         ");
        spacing9 = new JLabel("                                             ");
        spacing10 = new JLabel("                               ");



        //setting font for buttons, textfields and labels
        pickstall.setFont(new Font("Rockwell", Font.BOLD, 20));
        frontBtn.setFont(new Font("System", Font.BOLD, 22)); 
        middleBtn.setFont(new Font("System", Font.BOLD, 22)); 
        backBtn.setFont(new Font("System", Font.BOLD, 22)); 
        private1Btn.setFont(new Font("System", Font.BOLD, 22)); 
        tictype.setFont(new Font("Rockwell", Font.BOLD, 20)); 
        amontic.setFont(new Font("Rockwell", Font.BOLD, 20)); 
        ttamon.setFont(new Font("Rockwell", Font.BOLD, 20)); 
        amondue.setFont(new Font("Rockwell", Font.BOLD, 20)); 
        amonpaid.setFont(new Font("Rockwell", Font.BOLD, 20));
        stalltypeTxt.setFont(new Font("Verdana", Font.BOLD, 20));
        tickettypeTxt.setFont(new Font("Verdana", Font.BOLD, 20));
        amountticketsTxt.setFont(new Font("Verdana", Font.BOLD, 20));
        totalamountTxt.setFont(new Font("Verdana", Font.BOLD, 20));
        amountdueTxt.setFont(new Font("Verdana", Font.BOLD, 20));
        amountpaidTxt.setFont(new Font("Verdana", Font.BOLD, 20));

        twentypenceBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
        fiftypenceBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
        onepoundBtn.setFont(new Font("Century Gothic", Font.BOLD, 28));
        twopoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
        fivepoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
        tenpoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28)); 
        twentypoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28));

        C.setFont(new Font("Serif", Font.BOLD, 28));

        stalltypeTxt.setEditable(false);
        tickettypeTxt.setEditable(false);
        totalamountTxt.setEditable(false);
        amountdueTxt.setEditable(false);
        amountpaidTxt.setEditable(false);



        //positioning all buttons, textfields and labels
        JPanel top = new JPanel(); 
        top.add(label5);
        add("North", top);

        JPanel mid = new JPanel();
        mid.add(pickstall);
        mid.add(stalltypeTxt);
        mid.add(spacing6);

        mid.add(private1Btn);
        mid.add(frontBtn);
        mid.add(middleBtn);
        mid.add(backBtn);

        mid.add(tictype);
        mid.add(tickettypeTxt);
        mid.add(spacing7);

        mid.add(amontic);
        mid.add(amountticketsTxt);
        mid.add(spacing8);

        mid.add(ttamon);
        mid.add(totalamountTxt);
        mid.add(calcBtn);
        mid.add(spacing10);

        mid.add(amonpaid);
        mid.add(amountpaidTxt);
        mid.add(spacing9);

        mid.add(twentypenceBtn);
        mid.add(fiftypenceBtn);
        mid.add(onepoundBtn);
        mid.add(twopoundsBtn);
        mid.add(fivepoundsBtn);
        mid.add(tenpoundsBtn);
        mid.add(twentypoundsBtn);

        mid.add(amondue);
        mid.add(amountdueTxt);
        mid.add(C);
        add("Center", mid);




        calcBtn.addActionListener(this);
        private1Btn.addActionListener(this);
        frontBtn.addActionListener(this);
        middleBtn.addActionListener(this);
        backBtn.addActionListener(this);

        twentypenceBtn.addActionListener(this);
        fiftypenceBtn.addActionListener(this);
        onepoundBtn.addActionListener(this);
        twopoundsBtn.addActionListener(this);
        fivepoundsBtn.addActionListener(this);
        tenpoundsBtn.addActionListener(this);
        twentypoundsBtn.addActionListener(this);

        C.addActionListener(this);


        setVisible(true);

    }


     public void actionPerformed(ActionEvent e){
         // event handler for the buttons



        if (e.getSource() == private1Btn)
        {
            stalltypeTxt.setText("Private Stall Location is Chosen");
            tickettypeTxt.setText("30.85");
        }

        else if (e.getSource() == frontBtn)
        {
            stalltypeTxt.setText("Front Stall Location is Chosen");
            tickettypeTxt.setText("15.00");
        }
        else if (e.getSource() == middleBtn)
        {
            stalltypeTxt.setText("Middle Stall Location is Chosen");
            tickettypeTxt.setText("10.20");
        }
        else if (e.getSource() == backBtn)
        {
            stalltypeTxt.setText("Back Stall Location is Chosen");
            tickettypeTxt.setText("5.70");

        }
        else if (e.getSource() == tickettypeTxt)
        {

             type = Double.parseDouble(tickettypeTxt.getText());
        }
        else if (e.getSource() == amountticketsTxt)
        {
            number = Integer.parseInt(amountticketsTxt.getText());
        }
        else if (e.getSource() == calcBtn)
        {

        Workings c = new Workings();
        w.setType(type);
        w.setNumber(number);
        double total = w.calculateBill();

        totalamountTxt.setText(pounds.format(total));
        }


     }
     }

with a halfway done auxiliary class called Workings

public class Workings {
    // private instance variables
    private double type, number;

    // public no-argument constructor
    public Workings() { }

    // public constructor with four arguments
    public Workings(double t, int n)
    {
        type = t;
        number = n;


    }

    // public 'set' methods
    public void setType(double t) { type = t; }
    public void setNumber(int n) { number = n; }


    // public method to calculate the bill
    public double calculateBill()
    {
        double total = type * number ;
        return total;
    }
}

You rely on actionPerformed beuing called for tickettypeTxt and amountticketsTxt but do you add action listeners to those fields? And what if the user doesn't click them at the right time?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.