Hello everyone,
I have a question regarding a Japplet that I am creating for a theatre. The program is supposed to take a yes or no response from the user, take which section the user will be sitting in, ask how many tickets they would like to purchase, and then calculate their total cost when the total button is clicked. The problem I am having is that when I click the total button nothing is happening. I think it may be because I have my while loops in the wrong place, but any help you can give me would be much appreciated!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
/**
The Test class is an applet that calulates
ticket prices for a theatre.
*/
public class Test extends JApplet
{
private JPanel mPanel; // To hold a text field
private JPanel aPanel; // To hold a text field
private JPanel bPanel; // To hold a text field
private JPanel tPanel; // To hold a text field
private JPanel buttonPanel; // To hold a button
private JTextField member; // members
private JTextField area; // seating area
private JTextField ticketnum; // tickets
private JTextField total; // total cost
/**
init method
*/
public void init()
{
// Build the panels.
buildmPanel();
buildaPanel();
buildbPanel();
buildtPanel();
buildButtonPanel();
// Create a layout manager.
setLayout(new GridLayout(5, 1));
// Add the panels to the content pane.
add(mPanel);
add(aPanel);
add(bPanel);
add(tPanel);
add(buttonPanel);
}
/**
The buildtPanel method creates a panel with a text
field in which the user can enter whether they are a member
or not.
*/
private void buildmPanel()
{
// Create the panel.
mPanel = new JPanel();
// Create a label to display a message.
JLabel message1 =
new JLabel("Are You a Member (Y/N)?: ");
// Create a text field for the Member temp.
member = new JTextField(10);
// Create a layout manager for the panel.
mPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// Add the label and text field to the panel.
mPanel.add(message1);
mPanel.add(member);
}
private void buildaPanel()
{
// Create the panel.
aPanel = new JPanel();
// Create a label to display a message.
JLabel message2 =
new JLabel("What Seating Area?: ");
// Create a text field for the Member temp.
area = new JTextField(10);
// Create a layout manager for the panel.
aPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// Add the label and text field to the panel.
aPanel.add(message2);
aPanel.add(area);
}
private void buildbPanel()
{
// Create the panel.
bPanel = new JPanel();
// Create a label to display a message.
JLabel message3 =
new JLabel("How many Tickets?: ");
// Create a text field for the Member temp.
ticketnum = new JTextField(10);
// Create a layout manager for the panel.
bPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// Add the label and text field to the panel.
bPanel.add(message3);
bPanel.add(ticketnum);
}
/**
The buildpPanel method creates a panel that
displays the pounds in a
read-only text field.
*/
private void buildtPanel()
{
// Create the panel.
tPanel = new JPanel();
// Create a label to display a message.
JLabel message4 =
new JLabel("Total: ");
//Create a text field for the pounds temp.
total = new JTextField(10);
// Make the text field read-only.
total.setEditable(false);
// Create a layout manager for the panel.
tPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// Add the label and text field to the panel.
tPanel.add(message4);
tPanel.add(total);
}
/**
The buildButtonPanel method creates a panel with
a button that converts the kgram temperature
to pounds.
*/
private void buildButtonPanel()
{
// Create the panel.
buttonPanel = new JPanel();
// Create a button with the text "Convert".
JButton convButton = new JButton("Total");
// Add an action listener to the button.
convButton.addActionListener(new ButtonListener());
// Add the button to the panel.
buttonPanel.add(convButton);
}
/**
Private inner class that handles the action event
that is generated when the user clicks the convert
*/
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double btemp, ttemp ; // To hold the
if (member.equals ("Y") || member.equals("y")){
boolean isMember = true;
double totals = 0;
while (isMember){
if ( area.equals ("A") || area.equals ("a")){
totals = 68;
} else if ( area.equals ("B") || area.equals ("b")){
totals = 42.50;
} else if ( area.equals ("C") || area.equals ("c")){
totals = 25.50;
} else if (member.equals ("N") || member.equals("n")){
isMember = false;
}
while (isMember = false){
if ( area.equals ("A") || area.equals ("a")){
totals = 80;
} else if ( area.equals ("B") || area.equals ("b")){
totals = 50;
} else if ( area.equals ("C") || area.equals ("c")){
totals = 30;
}
}
//Create a DecimalFormat object to format numbers.
DecimalFormat formatter = new DecimalFormat("0.0");
// Get the number of tickets and convert to a double.
btemp = Double.parseDouble(ticketnum.getText());
// Calculate the total.
ttemp = btemp * totals;
// Display the total .
total.setText(formatter.format(ttemp));
}
}
}
}
}