Parameter passing problem !!!

J

javacarrot

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
 
A

Andrew Thompson


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
 
A

Andy Flowers

javacarrot said:
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.
 
J

javacarrot

Hi,

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

Thanks for taking a look
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top