I have to write a program that creates a GUI and asks the user to select from a group of buttons which time of the day they made the call, and input in a text field how many minutes the call was. for the day time, it's supposed to be $0.07 per minute. Evening is $0.12 per minute and for the off peak time is $0.05. I have the logic wrong, for instance, I put 1 in the text box and it told me the total charge was 10.07, what am I doing wrong?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class LongDistanceWindow extends JFrame
{
private JPanel panel; // a holding panel
private JTextField textField;
private JLabel messageLabel; // A message to the user
private JRadioButton dayTime;
private JRadioButton evening;
private JRadioButton offPeak;
private ButtonGroup radioButtonGroup; // To group the radio buttons
private final int WINDOW_WIDTH = 500; // window width
private final int WINDOW_HEIGHT = 200; // window height
/**
Constructor
*/
public LongDistanceWindow()
{
setTitle("Long Distance Calls");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel()
{
messageLabel = new JLabel("How many minutes were you talking?:");
textField = new JTextField(10);
dayTime = new JRadioButton("Daytime(6:00 A.M. through 5:59 P.M.");
evening = new JRadioButton("Evening(6:00 P.M. through 11:59 P.M.");
offPeak = new JRadioButton("12:00 A.M. through 5:59 A.M.");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(dayTime);
radioButtonGroup.add(evening);
radioButtonGroup.add(offPeak);
dayTime.addActionListener(new RadioButtonListener());
evening.addActionListener(new RadioButtonListener());
offPeak.addActionListener(new RadioButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(textField);
panel.add(dayTime);
panel.add(evening);
panel.add(offPeak);
}
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input;
double total = 0.0;
input = textField.getText();
if (e.getSource() == dayTime)
{
total = Double.parseDouble(input) * 0.07;
}
else if (e.getSource() == evening)
{
total = Double.parseDouble(input) * 0.12;
}
else if (e.getSource() == offPeak)
{
total = Double.parseDouble(input) * 0.05;
}
JOptionPane.showMessageDialog(null, input + total);
}
}
public static void main(String[] args)
{
LongDistanceWindow ldw = new LongDistanceWindow();
}
}