I am to create a menu application for a cell phone company. The user is to select their package, phone, and any additional add ons they would like, and it is to disply the prices. This is by far the most advanced programming I have done to this point and am confused with the listeners for each of the menus. Here is what I have so far:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingConstants;
public class MenuWindow extends JFrame
{
private JLabel messageLabel; // to display a message
private final int LABEL_WIDTH = 400; // to diplay labels width
private final int LABEL_HEIGHT = 200; // to display labels height
private JMenuBar menuBar;
private JMenu packageMenu;
private JMenu phoneMenu;
private JMenu addonMenu;
private JRadioButtonMenuItem package1;
private JRadioButtonMenuItem package2;
private JRadioButtonMenuItem package3;
private JRadioButtonMenuItem phone1;
private JRadioButtonMenuItem phone2;
private JRadioButtonMenuItem phone3;
private JRadioButtonMenuItem vMail;
private JRadioButtonMenuItem textMessaging;
public MenuWindow()
{
super("Cell Solutions");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
messageLabel = new JLabel("Use Menu to select the plan and phone you wish to purchase, as well as any other add ons that you would like.",
SwingConstants.CENTER);
messageLabel.setPreferredSize(
new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
messageLabel.setForeground(Color.BLACK);
add(messageLabel);
buildMenuBar();
pack();
setVisible(true);
}
private void buildMenuBar()
{
menuBar = new JMenuBar();
buildPackageMenu();
buildPhoneMenu();
buildAddonMenu();
menuBar.add(packageMenu);
menuBar.add(phoneMenu);
menuBar.add(addonMenu);
setJMenuBar(menuBar);
}
private void buildPackageMenu()
{
package1 = new JRadioButtonMenuItem("300 min/ month: $45", true);
package1.setMnemonic(KeyEvent.VK_X);
package1.addActionListener(new PackageListener());
package2 = new JRadioButtonMenuItem("800 min/ month: $65");
package2.setMnemonic(KeyEvent.VK_X);
package2.addActionListener(new PackageListener());
package3 = new JRadioButtonMenuItem("1500 min/ month: $99");
package3.setMnemonic(KeyEvent.VK_X);
package3.addActionListener(new PackageListener());
packageMenu = new JMenu("Package");
packageMenu.setMnemonic(KeyEvent.VK_F);
packageMenu.add(package1);
packageMenu.add(package2);
packageMenu.add(package3);
}
private void buildPhoneMenu()
{
phone1 = new JRadioButtonMenuItem("Model 100: $29.95", true);
phone1.setMnemonic(KeyEvent.VK_B);
phone1.addActionListener(new PhoneListener());
phone2 = new JRadioButtonMenuItem("Model 110: $49.95", true);
phone2.setMnemonic(KeyEvent.VK_B);
phone2.addActionListener(new PhoneListener());
phone3 = new JRadioButtonMenuItem("Model 200: $99.95", true);
phone3.setMnemonic(KeyEvent.VK_B);
phone3.addActionListener(new PhoneListener());
ButtonGroup group = new ButtonGroup();
group.add(phone1);
group.add(phone2);
group.add(phone3);
phoneMenu = new JMenu("Phone");
phoneMenu.setMnemonic(KeyEvent.VK_T);
phoneMenu.add(phone1);
phoneMenu.add(phone2);
phoneMenu.add(phone3);
}
private void buildAddonMenu()
{
vMail = new JRadioButtonMenuItem("Voice Mail: $5.00 / month", true);
vMail.setMnemonic(KeyEvent.VK_B);
vMail.addActionListener(new AddonListener());
textMessaging = new JRadioButtonMenuItem("Text Messaging: $10.00 / month", true);
textMessaging.setMnemonic(KeyEvent.VK_B);
textMessaging.addActionListener(new AddonListener());
ButtonGroup group = new ButtonGroup();
group.add(vMail);
group.add(textMessaging);
addonMenu = new JMenu("Add-Ons");
addonMenu.setMnemonic(KeyEvent.VK_T);
addonMenu.add(vMail);
addonMenu.add(textMessaging);
}
}
class PackageListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int packagePrice;
if (package1.isSelected())
packagePrice = 45;
else if (package2.isSelected())
packagePrice = 65;
else if (package3.isSelected())
packagePrice = 99;
}
}
class PhoneListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int phonePrice;
if (phone1.isSelected())
phonePrice = 45;
else if (phone2.isSelected())
phonePrice = 65;
else if (phone3.isSelected())
phonePrice = 99;
}
}
class AddonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int vMailPrice;
if (vMail.isSelected())
vMailPrice = 5;
int textPrice;
if (textMessaging.isSelected())
textPrice = 10;
int addonPrice = vMailPrice + textPrice;
}
public static void main(String[] args)
{
new MenuWindow();
}
}