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
Second Class ConfirmBooking
Third Class Panel - main frame
Anybody got any ideas.
Thanks
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;
}
}
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
}
}
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