Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Parameter passing problem !!!

Reply
Thread Tools

Parameter passing problem !!!

 
 
javacarrot
Guest
Posts: n/a
 
      11-14-2004
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




 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      11-14-2004
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
 
Reply With Quote
 
 
 
 
Andy Flowers
Guest
Posts: n/a
 
      11-14-2004
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/...va.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.


 
Reply With Quote
 
javacarrot
Guest
Posts: n/a
 
      11-16-2004
Hi,

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

Thanks for taking a look


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-16-2004
On Tue, 16 Nov 2004 14:16:42 -0500, javacarrot wrote:

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


Most importantly, you got it working?

> Thanks for taking a look


You're welcome.

--
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
Newbe Problem with parameter passing to href in jsp document. Vladimir.Sakharuk@gmail.com Java 4 02-24-2006 08:23 PM
Function parameter passing problem Eduardo Biano Python 0 05-17-2005 09:16 AM
Parameter Passing Problem with JSP 2.0 Tag Library eschreiber@gmail.com Java 0 01-25-2005 02:55 PM
problem in passing parameter to dll function andy C++ 4 02-21-2004 05:25 PM



Advertisments