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();
	}
}

Recommended Answers

All 5 Replies

Your calculations appear correct, your method of displaying them appears incorrect.

JOptionPane.showMessageDialog(null, input + total);

Saying input + total concatenates input and total into a String which then gets displayed to the user. So for example if the user inputs "10" and the total money should be .70 cents, your code might display "100.70" to the user.

oh ok, So I just take out input in the output.

... Yes. And if that answers your question, mark this thread solved.

how would you add the coding in if a letter was in the text field or nothing at all?

i tried the following:

else
{
JOptionPane.showMessageDialog(null, "invalid input");
}

//Display the result
JOptionPane.showMessageDialog(null, input + " minutes at the " + timeOfDay + " is $" + formatter.format(cost));

System.exit(0);
}

i tried the following:

And did it work?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.