Long Distance Calls
A long-distance provider charges the following rates for telephone calls:
| Rate Category | Rate per Minute |
| Daytime (6:00 a.m. through 5:59 p.m. ) | $0.07 |
| Evening (6:00 p.m. through 11:59 p.m. ) | $0.12 |
| Off-Peak (12:00 a.m. through 5:59 a.m. ) | $0.05 |
Create a GUI application that allows the user to select a rate category (from a set of radio buttons), and enter the number of minutes of the call into a text field. A dialog box should display the charge for the call.
Program plan:
• Import all the required packages.
• Create a class named as LongDistancecall.
o Declare all the required private variables.
o Create a constructor.
o Set the size and title for the JFrame.
o Call the buttonPanel() method.
• Implement the buttonPanel()method.
• Define main method.
o .
• Create a class named as RadioButtonListener.
o Implement actionPerformed method.
• Calculate and display the call cost.
Program :
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class LongDistancecall extends JFrame
{
private JPanel panel; // A holding panel
private JLabel messageLabel; // A message to the user
private JTextField minutesTextField;
// To hold user input
private JRadioButton DayButton;
private JRadioButton EveningButton;
private JRadioButton Off_peakButton;
private ButtonGroup radioButtonGroup;
// To group radio buttons
private final int WINDOW_WIDTH = 250; // Window width
private final int WINDOW_HEIGHT = 250;
// Window height
/* Constructor */
public LongDistancecall()
{
// Set the title.
setTitle("Long Distance calls");
// Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Build the panel and add it to the frame.
buildPanel();
// Add the panel to the frame's content pane.
add(panel);
// Display the window.
setVisible(true);
}
/**
The buildPanel method adds a label, text field, and
and three buttons to a panel.
*/
private void buildPanel()
{
// Create the label, text field, and radio buttons.
messageLabel = new
JLabel(" Enter number of minutes ");
minutesTextField = new JTextField(10);
DayButton = new JRadioButton("Day Time");
EveningButton = new JRadioButton(" Evening Time");
Off_peakButton = new JRadioButton("Off-peak");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(DayButton);
radioButtonGroup.add(EveningButton);
radioButtonGroup.add(Off_peakButton);
// Add action listeners to the radio buttons.
DayButton.addActionListener
(new RadioButtonListener());
EveningButton.addActionListener
(new RadioButtonListener());
Off_peakButton.addActionListener
(new RadioButtonListener());
// Create a panel and add the components to it.
panel = new JPanel();
panel.add(messageLabel);
panel.add(minutesTextField);
panel.add(DayButton);
panel.add(Off_peakButton);
}
/**
Private inner class that handles the event when
the user clicks one of the radio buttons.
*/
private class RadioButtonListener
implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input; // To hold the user's input
double result = 0.0; // To hold the conversion
input = minutesTextField.getText();
// Determine which radio button was clicked.
if (e.getSource() == DayButton)
{
result = Double.parseDouble(input) * 0.07;
}
else if (e.getSource() == EveningButton)
{
result = Double.parseDouble(input) * 0.12;
}
else if (e.getSource() == Off_peakButton)
{
result = Double.parseDouble(input) * 0.05;
}
// Display the conversion.
JOptionPane.showMessageDialog
(null," Call charges is: $ " + result );
}
}
public static void main(String[] args)
{
new LongDistancecall();
}
}
Sample output:
Run1:
Run2: