Menu

[Solved]Java Programming Need Create Panel Class Ever Classes Break Code Sure Thank Public Clas Q37130409

Java Programming: I need to create each panel into a class andwhat ever other classes I can break this code down to. I am notsure how to do this. Thank you.

public class OrderCalculator extends JFrame
{
   private JPanel breadPanel; // bread panel
   private JPanel toppingPanel; // Topping panel
   private JPanel coffeePanel; // Coffee panel
   private JPanel greetingPanel; // To display agreeting
   private JPanel buttonPanel; // To hold the buttons

   private JButton calcButton; // To calculate thecost
   private JButton exitButton; // To exit theapplication

   private JLabel greeting; // For the greetingPanel

   private JRadioButton whiteBread; // To selectwhite
   private JRadioButton wheatBread; // To selectwheat
   private ButtonGroup breadBg; // Radio button group

   private JRadioButton noCoffee; // To select nocoffee
   private JRadioButton regularCoffee; // To selectregular coffee
   private JRadioButton decafCoffee; // To selectdecaf
   private JRadioButton cappuccino; // To selectcappuccino
   private ButtonGroup coffeeBg; // Radio buttongroup

   private JCheckBox cheese; // To select cheese
   private JCheckBox roastBeef; // To select roastbeef
   private JCheckBox turkey; // To select turkey
   private JCheckBox ham; // To select ham

   private final double TAX_RATE = 0.07; // Sales taxrate

   {

       setTitle(“Order Calculator”);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       setLayout(new BorderLayout());

       buildGreetingPanel();
       buildBreadPanel();
       buildToppingPanel();
       buildCoffeePanel();

       buildButtonPanel();

       add(greetingPanel,BorderLayout.NORTH);
       add(breadPanel,BorderLayout.WEST);
       add(toppingPanel,BorderLayout.CENTER);
       add(coffeePanel,BorderLayout.EAST);
       add(buttonPanel,BorderLayout.SOUTH);

       pack();
       setVisible(true);
   }

   private void buildButtonPanel()
   {

       buttonPanel = new JPanel();

       calcButton = newJButton(“Calculate”);
       exitButton = newJButton(“Exit”);

       calcButton.addActionListener(newCalcButtonListener());
       exitButton.addActionListener(newExitButtonListener());

       buttonPanel.add(calcButton);
       buttonPanel.add(exitButton);
   }

   private void buildBreadPanel()
   {

       breadPanel = new JPanel();

  
       breadPanel.setLayout(newGridLayout(2, 1));
       whiteBread = newJRadioButton(“White”, true);
       wheatBread = newJRadioButton(“Wheat”);
       breadBg = new ButtonGroup();
       breadBg.add(whiteBread);
       breadBg.add(wheatBread);

breadPanel.setBorder(
              BorderFactory.createTitledBorder(“Bread”));

breadPanel.add(whiteBread);
       breadPanel.add(wheatBread);
   }

   private void buildGreetingPanel()
   {
greetingPanel = new JPanel();

greeting = new JLabel(“Welcome to Johnny’s Sandwhich Shop”);

greetingPanel.add(greeting);
   }

   private void buildCoffeePanel()
   {
coffeePanel = new JPanel();

coffeePanel.setLayout(new GridLayout(4, 1));

noCoffee = new JRadioButton(“None”);
       regularCoffee =
              new JRadioButton(“Regular coffee”, true);
       decafCoffee = newJRadioButton(“Decaf coffee”);
       cappuccino = newJRadioButton(“Cappuccino”);

coffeeBg = new ButtonGroup();
       coffeeBg.add(noCoffee);
       coffeeBg.add(regularCoffee);
       coffeeBg.add(decafCoffee);
       coffeeBg.add(cappuccino);

coffeePanel.setBorder(
              BorderFactory.createTitledBorder(“Coffee”));

coffeePanel.add(noCoffee);
      coffeePanel.add(regularCoffee);
       coffeePanel.add(decafCoffee);
       coffeePanel.add(cappuccino);
   }

   private void buildToppingPanel()
   {
       // Create a panel fortoppings
       toppingPanel = new JPanel();

       toppingPanel.setLayout(newGridLayout(4, 1));

       cheese = newJCheckBox(“Cheese”);
       roastBeef = new JCheckBox(“RoastBeef”);
       turkey = newJCheckBox(“Turkey”);
       ham = new JCheckBox(“Ham”);

       toppingPanel.setBorder(
             BorderFactory.createTitledBorder(“Meat/Cheese”));

       toppingPanel.add(cheese);
       toppingPanel.add(roastBeef);
       toppingPanel.add(turkey);
       toppingPanel.add(ham);
   }

private class CalcButtonListener implements ActionListener
   {
       public voidactionPerformed(ActionEvent e)
       {
double subtotal, tax, total;

subtotal = getBreadCost() +
                  getToppingCost() +
                  getCoffeeCost();

tax = subtotal * TAX_RATE;

total = subtotal + tax;

DecimalFormat dollar = new DecimalFormat(“0.00”);

           // Displaythe charges.
          JOptionPane.showMessageDialog(null, “Subtotal: $” +
                  dollar.format(subtotal) +”n” +
                  “Tax: $” + dollar.format(tax)+ “n” +
                  “Total: $” +dollar.format(total));
       }
   }

   private double getBreadCost()
   {

       final double WHITE_BREAD =1.25;
       final double WHEAT_BREAD =1.50;

       double breadCost = 0.0;

       if(whiteBread.isSelected())
           breadCost =WHITE_BREAD;
       else
           breadCost =WHEAT_BREAD;

       return breadCost;
   }

   private double getCoffeeCost()
   {
   final double NO_COFFEE = 0.0;
       final double REGULAR_COFFEE =1.25;
       final double DECAF_COFFEE =1.25;
       final double CAPPUCCINO = 2.00;

       double coffeeCost = 0.0;

       if (noCoffee.isSelected())
           coffeeCost =NO_COFFEE;
       else if(regularCoffee.isSelected())
           coffeeCost =REGULAR_COFFEE;
       else if(decafCoffee.isSelected())
           coffeeCost =DECAF_COFFEE;
       else if(cappuccino.isSelected())
           coffeeCost =CAPPUCCINO;

       return coffeeCost;
   }

   private double getToppingCost()
   {

       final double CHEESE = 0.50;
       final double ROAST_BEEF =0.25;
       final double TURKEY = 0.75;
       final double HAM = 0.75;

       double toppingCost = 0.0;

       if (cheese.isSelected())
           toppingCost +=CHEESE;
       if (roastBeef.isSelected())
           toppingCost +=ROAST_BEEF;
       if (turkey.isSelected())
           toppingCost +=TURKEY;
       if (ham.isSelected())
           toppingCost +=HAM;

       return toppingCost;
   }

   private class ExitButtonListener implementsActionListener
   {
       public voidactionPerformed(ActionEvent e)
       {
          System.exit(0);
       }
   }

   public static void main(String[] args)
   {
       OrderCalculator gui = newOrderCalculator();
   }

}

Expert Answer


Answer to Java Programming: I need to create each panel into a class and what ever other classes I can break this code down to. I … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *