Parameter passing problem !!!

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

  1. javacarrot

    javacarrot Guest

    Hi,

    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:
    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.setBorder(BorderFactory.createLoweredBevelBorder());
    
    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());
    
    add(typecombo);
    add(TicketText);
    }
    
    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:
    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:
    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.setTitle("");
    frame.setLocation(100,100);
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    //set up panel with compound border
    
    CalculatePanel cost;
    cost = new CalculatePanel();
    cost.addPanel();
    
    JPanel okPanel = new JPanel();
    okButton = new JButton("CONFIRM");
    okButton.addActionListener(new confirmButton());
    okPanel.add(okButton);
    okPanel.add(cost);
    
    frame.getContentPane().add(okPanel, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }
    private class confirmButton implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    ConfirmBooking confirm;
    confirm = new ConfirmBooking();
    confirm.ConfirmBooking();
    }
    }
    }
    Anybody got any ideas.

    Thanks
     
    javacarrot, Nov 14, 2004
    #1
    1. Advertising

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

    > Hi,


    Lo,

    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..
    <http://www.physci.org/codes/sscce.jsp>
    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..
    <http://www.physci.org/codes/javafaq.jsp#cljh>

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

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Nov 14, 2004
    #2
    1. Advertising

  3. javacarrot

    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
    case).

    Take a look at the API documentation
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#equals(java.lang.Object)

    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
    #3
  4. javacarrot

    javacarrot Guest

    Hi,

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

    Thanks for taking a look
     
    javacarrot, Nov 16, 2004
    #4
  5. Andrew Thompson, Nov 16, 2004
    #5
    1. Advertising

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:
    0
    Views:
    382
  2. Replies:
    4
    Views:
    6,340
  3. andy
    Replies:
    4
    Views:
    807
    Jonathan Turkanis
    Feb 21, 2004
  4. Eduardo Biano

    Function parameter passing problem

    Eduardo Biano, May 17, 2005, in forum: Python
    Replies:
    0
    Views:
    348
    Eduardo Biano
    May 17, 2005
  5. Mister B
    Replies:
    8
    Views:
    599
    Nick Keighley
    Aug 26, 2010
Loading...

Share This Page