Parameter passing problem !!!

Discussion in 'Java' started by javacarrot, Nov 14, 2004.

  1. javacarrot

    javacarrot Guest


    I have a simple problem with passing a variable from one class to another.
    Basically the variable is calculated in a listerner event, passed from that
    class to another and displayed.

    I have the program running and compiling, although the end result is not
    what it should be. I always get the end result of 0 instead of a
    calculated result. The user must choose a type either adult, child,
    student then enter a number in the text field then click confirm which
    will bring up result with how much it will cost.

    First class CalculcatePanel
    Code (Text):

    public class CalculatePanel extends JPanel{

    public int totalcost;
    public int ticketcost;
    public int number;
    public JTextField TicketText;
    public JButton TicketButton;
    public String typechosen;
    public String[] type = {"Select", "Adult", "Child", "Student"};
    public JComboBox typecombo;

    /** Creates a new instance of addpanel */
    public void addPanel() {

    JPanel typeinfo = new JPanel();

    typeinfo.setBackground (Color.yellow);
    typeinfo.setPreferredSize(new Dimension(200,200));

    typecombo = new JComboBox(type);
    typecombo.addItemListener(new TypeInfoComboBox());

    TicketText = new JTextField (5);
    TicketText.addActionListener(new TicketButton());


    public class TypeInfoComboBox implements ItemListener  {

    public void actionPerformed(ActionEvent e) {}

    public void itemStateChanged( ItemEvent event )
    typechosen = (String) typecombo.getSelectedItem();

    if (typechosen == "Adult")
    ticketcost = 5;
    else if (typechosen == "Child")
    ticketcost = 3;
    else if (typechosen == "Student")
    ticketcost = 4;

    public class TicketButton implements ActionListener
    public void actionPerformed(ActionEvent e)
    String text = TicketText.getText();
    number = Integer.parseInt(text);

    //total cost calculated here
    totalcost = ticketcost * number;
    JOptionPane.showMessageDialog(null, "Total Booking Cost Will
    be : £" + totalcost);
    //method getcost to pass totalcost variable to class ConfirmBooking

    public int getcost()
    //i also put the calculation here to see if this worked but it didnt

    totalcost = ticketcost * number;
    return totalcost;
    Second Class ConfirmBooking
    Code (Text):

    public class ConfirmBooking {

    /** Creates a new instance of ConfirmBooking */
    public void ConfirmBooking() {

    CalculatePanel cost = new CalculatePanel();

    JOptionPane.showMessageDialog(null, "Your Booking Has Been
    Confirmed.\n\nPayment Of £"
    + cost.getcost() + " Has been Transacted.\nThankyou for booking
    with us",
    "***** Congratulations *****",+JOptionPane.INFORMATION_MESSAGE);
    System.exit( 0 ); // Exits Program
    Third Class Panel - main frame
    Code (Text):

    public class Panel extends JFrame
    public JPanel cost;

    public static void main(String[] args)
    Panel frame = new Panel();
    public Panel() {

    JButton     okButton;
    //set up main frame
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    //set up panel with compound border

    CalculatePanel cost;
    cost = new CalculatePanel();

    JPanel okPanel = new JPanel();
    okButton = new JButton("CONFIRM");
    okButton.addActionListener(new confirmButton());

    frame.getContentPane().add(okPanel, BorderLayout.CENTER);
    private class confirmButton implements ActionListener
    public void actionPerformed(ActionEvent e)
    ConfirmBooking confirm;
    confirm = new ConfirmBooking();
    Anybody got any ideas.

    javacarrot, Nov 14, 2004
    1. Advertisements

  2. On Sun, 14 Nov 2004 12:38:21 -0500, javacarrot wrote:

    > Hi,


    I started looking at your code and gave up in disgust..

    Well, not really. But after putting in effort to correct the
    lines broken by line wrap, then compile and run it, it was not
    immediately clear what your classes, methods and attributes were
    since you seemed to follow an unconventional indentation strategy,
    and did not stick to the common nomenclature. I might have fixed
    it to the point where (even my addled brain) could understand it,
    but could not be bothered.

    Some tips for you though..

    - Use the nomenclature that other Java programmers use..
    ClassName, methodName(), attributeName (unless it is a CONSTANT).

    - Never compare your strings with '==', instead use..
    if ( theFirstString.equals(theSecondString) )

    - When you do not know what is happening, put System.out.println
    statements *everywhere*. Put them at the beginning and end of each
    method, also print out the value of critical attributes, especially
    just after they have been set, but also before they are used.
    (for example, your itemStateChanged code is never invoked.)

    - You seem to sub-class various components for no apparent reason, don't.
    (Your TypeInfoComboBox *is* a JComboBox, so use a JComboBox and be
    done with it)

    - Check my document on preparing examples for others to see..
    you will get more, and better help, if people do not have
    to invest effort to get it to run.

    - Consider posting to a group more geared to your level..

    - Don't end your subject line with !!! as it makes you appear 'needy'.


    Andrew Thompson Web & IT Help Open-source software suite Science & Technology Images that escape the mundane
    Andrew Thompson, Nov 14, 2004
    1. Advertisements

  3. Andy Flowers

    Andy Flowers Guest

    javacarrot wrote:
    > Hi,
    > ...
    > public void itemStateChanged( ItemEvent event )
    > {
    > typechosen = (String) typecombo.getSelectedItem();
    > if (typechosen == "Adult")
    > {
    > ticketcost = 5;
    > }
    > else if (typechosen == "Child")
    > {
    > ticketcost = 3;
    > }
    > else if (typechosen == "Student")
    > {
    > ticketcost = 4;
    > }
    > }
    > ...

    Look into the equals(...) function of Object (and String in this particular

    Take a look at the API documentation

    Using typechosen == "Adult" says 'does the object reference stored in
    typechosen equal the object reference generated for "Adult"', and this will
    be false in your case and in most appliations written this way.

    Using typechosen.equals("Adult"), or better still "Adult".equals(typechosen)
    to remove the [missing] check for a null typechosen, says 'does the String
    stored in typechosen equal the String "Adult"', and this has the possibility
    of being true if that is what your user has selected.
    Andy Flowers, Nov 14, 2004
  4. javacarrot

    javacarrot Guest


    I have changed the way i compare strings now and have adhered to common

    Thanks for taking a look
    javacarrot, Nov 16, 2004
  5. Andrew Thompson, Nov 16, 2004
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
  2. Replies:
  3. andy
    Jonathan Turkanis
    Feb 21, 2004
  4. Eduardo Biano

    Function parameter passing problem

    Eduardo Biano, May 17, 2005, in forum: Python
    Eduardo Biano
    May 17, 2005
  5. Mister B
    Nick Keighley
    Aug 26, 2010

Share This Page