I am writing a program that prompts a user to enter the name of an item, the price of the item, and the percentage discount on that item. The program then calculates the sale price and spits that out in a display box.
The program is also supposed to contain a menu that lists "departments" that the user can choose from. The menu needs to be either in JComboBox format or using radio buttons. I failed miserably at getting the JComboBox option to work, so I did it using radio buttons.
When I run the program in NetBeans it works like a champ. The only issue is, NetBeans IDE is indicating a warning on each of the 5 radio button lines. The warning message is "Local variable hides a field". Using the Alt-Enter help feature, a menun pops up that says "Rename the local variable" and there are 3 sub-menu options where I can either Disable "Local variable hides a field" hint, Configure "Local variable hides a field" hint, or Suppress Warning - LocalVariableHidesMemberVariable.
i renamed the variables, and all that accomplished was renaming the variables. The warning messages still exist. I don't want to hide or suppress the warnings because that doesn't fix the underlying problem. Eventually, I want the radio buttons to actually work and not just appear on the menu.
So my question is, why am I getting this warning message, and how do I fix it?!
Please...If you respond, keep it WAY simple. I am not a Java developer! I am building this thing based on what I am reading in a book and finding online. So I am very new to this thing. Any other suggestions you may have to make thing thing more accurate/efficient/professional would be greatly appreciated.
Here is the code:
package retail.price.calculator; import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; /** * Author: Rick Sebastian */ public class RetailPriceCalculator extends JFrame { private JPanel panel; private JPanel selectedDeptPanel; //To hold department info private JPanel deptList; // A list of departments private JTextField selectedDept; // The selected department private JLabel label; // To display a message private JRadioButton meats, dairy, bakery, produce, pharmacy; private JLabel messageLabelA; private JTextField productNameTextField; private JLabel messageLabelB; private JTextField originalPriceTextField; private JLabel messageLabelC; private JTextField discountPercentageTextField; private JButton calcButton; public RetailPriceCalculator() { setTitle("Retail Price Calculator"); // Display name for frame setLayout (new BorderLayout()); setSize(950,200); // Sets display box size to 400x400 pixels setLocationRelativeTo(null); // Centers frame on the screen setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Ensures program terminates on exit buildPanel(); // Builds the main panel add(panel, BorderLayout.CENTER); // Centers items in the panel setVisible(true); // makes it visible JPanel p = new JPanel(); // Creates the panel for the radio buttons JRadioButtonMenuItem meats = new JRadioButtonMenuItem(); meats.setText("Meats"); JRadioButtonMenuItem dairy = new JRadioButtonMenuItem(); dairy.setText("Dairy"); JRadioButtonMenuItem bakery = new JRadioButtonMenuItem(); bakery.setText("Bakery"); JRadioButtonMenuItem produce = new JRadioButtonMenuItem(); produce.setText("Produce"); JRadioButtonMenuItem pharmacy = new JRadioButtonMenuItem(); pharmacy.setText("Pharmacy"); ButtonGroup group = new ButtonGroup(); group.add(meats); group.add(dairy); group.add(bakery); group.add(produce); group.add(pharmacy); panel.add(meats); panel.add(dairy); panel.add(bakery); panel.add(produce); panel.add(pharmacy); } private void buildPanel() { messageLabelA = new JLabel("Enter the product name"); productNameTextField = new JTextField(10); messageLabelB = new JLabel("Enter the original price"); originalPriceTextField = new JTextField(10); messageLabelC = new JLabel("Enter the percent to discount"); discountPercentageTextField = new JTextField(10); calcButton = new JButton ("Calculate"); calcButton.addActionListener(new CalcButtonListener()); panel = new JPanel(); panel.add(messageLabelA); panel.add(productNameTextField); panel.add(messageLabelB); panel.add(originalPriceTextField); panel.add(messageLabelC); panel.add(discountPercentageTextField); panel.add(calcButton); JTextArea textInput = new JTextArea(20, 40); JScrollPane scrollPane = new JScrollPane(textInput); } private class CalcButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String productNameInput; String originalPriceInput; String discountPercentageInput; double originalPrice; double discountPercentage; double salePrice; productNameInput = productNameTextField.getText(); originalPriceInput = originalPriceTextField.getText(); originalPrice = Double.parseDouble(originalPriceInput); discountPercentageInput = discountPercentageTextField.getText(); discountPercentage = Double.parseDouble(discountPercentageInput); salePrice = originalPrice - ((discountPercentage * originalPrice) * .01); JOptionPane.showMessageDialog(null, "The Sale Price of your " + productNameInput + " is $ "+ salePrice); } } public static void main(String[] args) { new RetailPriceCalculator(); } }