1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem w/ java calculation

  1. May 7, 2008 #1
    I'm creating an application that allows the user to choose one variety of bagel and the quantity of bagels he wishes to purchase. The user can choose if he wishes to buy cream cheese and choose the variety he wishes to add to his purchase. My calculation isn't working correctly.



    Here is my presentation class:

    Code (Text):
    import javax.swing.*;
    Code (Text):


    import java.text.*;

    import java.awt.event.*;



    public class Ch6Bagel extends JFrame implements ActionListener, ItemListener {





       //create GUI objects

       JPanel mainPanel = new JPanel();

       JTextField quantityTextField = new JTextField(10);

       JTextField creamCheeseQuantityTextField = new JTextField(10);

       JTextField newVarietyTextField = new JTextField(10);

       JCheckBox checkBox = new JCheckBox("Add Cream Cheese");

       JButton orderButton = new JButton("Order");

       JButton clearButton = new JButton("Clear");  

       JButton summaryButton = new JButton("Summary");  

       JButton varietyButton = new JButton("Add Variety");

       JButton deleteVarietyButton = new JButton("Delete Variety");

       JLabel bagelLabel = new JLabel("Lee Bagels");

       JTextArea outputTextArea = new JTextArea("Your Total Cost:", 10, 15);





       //datatypes and objects

       String bagelString[] = {"Plain", "Egg", "Rye", "Salt", "Blueberry",

               "Garlic", "Onion", "Sesame", "Poppy Seed", "The Works"};

       String creamCheeseString[] = {"Plain", "Herb", "Garlic"};

       static int quantityBagelInteger, quantityCreamCheeseInteger, counterInteger, totalBagelsBoughtInteger;

       String bagelTypeString, creamCheeseTypeString, addVarietyString;



       //create comboboxes

       JComboBox bagelJComboBox = new JComboBox(bagelString);

       JComboBox creamCheeseJComboBox = new JComboBox(creamCheeseString);



       public static void main(String[] args)

       {

       Ch6Bagel myBagel = new Ch6Bagel();

       myBagel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



       }





       public Ch6Bagel()

       {

           designFrame();

       }

       public void designFrame()

       {



           creamCheeseJComboBox.setEnabled(false);

           creamCheeseQuantityTextField.setEditable(false);

           mainPanel.add(bagelLabel);

           mainPanel.add(new JLabel("       Type of Bagel            "));

           mainPanel.add(bagelJComboBox);

           mainPanel.add(new JLabel("       Quantity           "));

           mainPanel.add(quantityTextField);

           mainPanel.add(checkBox);

           mainPanel.add(new JLabel("       Type of Cream Cheese           "));

           mainPanel.add(creamCheeseJComboBox);

           mainPanel.add(new JLabel("           Quantity                   "));

           mainPanel.add(creamCheeseQuantityTextField);

           mainPanel.add(outputTextArea);

           mainPanel.add(orderButton);

           mainPanel.add(summaryButton);

           mainPanel.add(varietyButton);

           mainPanel.add(newVarietyTextField);

           mainPanel.add(deleteVarietyButton);



           //add listeners

           orderButton.addActionListener(this);

           summaryButton.addActionListener(this);

           checkBox.addItemListener(this);





         //add the panel to the frame

           add(mainPanel);

           setSize(200,600);

           setVisible(true);







       }









       public void actionPerformed(ActionEvent evt)

       {

           //retrieve the logical of the component that fired the action event

           Object sourceObject = evt.getSource();

           if(sourceObject == orderButton)

           {



               getInput();

               displayOutput();

               counterInteger++;

           }

           else if(sourceObject == summaryButton)

           {

               summary();

           }

           else if(sourceObject == clearButton)

           {

               //clear();

           }

           else if(sourceObject == varietyButton)

           {

               addVariety();

           }

           else if(sourceObject == deleteVarietyButton)

           {

               deleteVariety();

           }



       }





       public void itemStateChanged(ItemEvent evt)

       {

           if(checkBox.isSelected())

           {

           creamCheeseJComboBox.setEnabled(true);

           creamCheeseQuantityTextField.setEditable(true);



           }

           else

           {

           creamCheeseJComboBox.setEnabled(false);

           creamCheeseQuantityTextField.setEditable(false);

           }

       }





       public void getInput()

       {



           String bagelType = (String)bagelJComboBox.getSelectedItem();

           String creamCheeseType = (String)creamCheeseJComboBox.getSelectedItem();

           addVarietyString = newVarietyTextField.getText();

           quantityBagelInteger =  Integer.parseInt(quantityTextField.getText());

           quantityCreamCheeseInteger = Integer.parseInt(creamCheeseQuantityTextField.getText());



       }



       public void displayOutput()

       {

         DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");

         double bagelCostDouble, creamCheeseCostDouble, totalCostDouble;



         Calculate myBagel = new Calculate();



         bagelCostDouble = myBagel.getBagelCost();

         creamCheeseCostDouble = myBagel.getCheeseCost();

         totalCostDouble = myBagel.getTotalCost();



           outputTextArea.setText("Type of Bagel:" + bagelTypeString +

           '\n' + "Quantity:" + quantityBagelInteger +

           '\n' + "Cream Cheese:" + creamCheeseTypeString +

           '\n' + "Quantity:" + quantityCreamCheeseInteger +

           '\n' + "Bagels:" + formatDecimalFormat.format(bagelCostDouble) +

           '\n' + "Cream Cheese:" + formatDecimalFormat.format(creamCheeseCostDouble)

           + '\n' + "Total Cost:" + formatDecimalFormat.format(totalCostDouble));





       }



       public void summary()

       {

           DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");



           double totalBagelCostDouble, totalCreamCheeseCostDouble, averageBagelsBoughtDouble;

           Calculate myBagel = new Calculate();



           totalBagelCostDouble = myBagel.getTotalBagelCost();

           totalCreamCheeseCostDouble = myBagel.getTotalCheeseCost();

           averageBagelsBoughtDouble = myBagel.getAverageBagels();



           JOptionPane.showMessageDialog(null,

                       '\n' + " Total Cost of Bagels  " + formatDecimalFormat.format(totalBagelCostDouble)+

                       '\n' + " Total Cost of Cream Cheese  " + formatDecimalFormat.format(totalCreamCheeseCostDouble)

                       + '\n' + "Average Number of Bagels Bought" + averageBagelsBoughtDouble);



       }

       public void addVariety()

       {

           bagelJComboBox.addItem(addVarietyString);

       }



       public void deleteVariety()

       {

           bagelJComboBox.removeItemAt(bagelJComboBox.getSelectedIndex());

       }



       }

     


    calculation class:

    Code (Text):


    public class Calculate

    {

       private static int quantityBagelInteger, quantityCreamCheeseInteger, counterInteger, totalBagelsBoughtInteger;

       private double bagelCostDouble, creamCheeseCostDouble, totalCostDouble, totalBagelCostDouble,

       totalCreamCheeseCostDouble, averageBagelsBoughtDouble, grandTotalDouble;

       private final double BAGEL_PRICE = .99;

       private final double CREAM_CHEESE_PRICE = 2.25;

       public Calculate()

       {}



       public Calculate(int quantityBagelNewInteger, int quantityCheeseNewInteger,

       int counterNewInteger)

       {

           setBagelQuantity(quantityBagelNewInteger);

           setCheeseQuantity(quantityCheeseNewInteger);

           setCounter(counterNewInteger);

           calculate();

       }



       private void setBagelQuantity(int quantityBagelNewInteger)

       {

           //assign public variable to private

           quantityBagelInteger = quantityBagelNewInteger;

       }

       private void setCheeseQuantity(int quantityCheeseNewInteger)

       {

           //assign public variable to private

           quantityCreamCheeseInteger = quantityCheeseNewInteger;

       }

       private void setCounter(int counterNewInteger)

       {

           //assign public variable to private

           counterInteger = counterNewInteger;

       }





       public void calculate()

       {



           totalCostDouble = (quantityBagelInteger * BAGEL_PRICE) +

           (quantityCreamCheeseInteger * CREAM_CHEESE_PRICE);

           bagelCostDouble = quantityBagelInteger * BAGEL_PRICE;

           creamCheeseCostDouble = quantityCreamCheeseInteger * CREAM_CHEESE_PRICE;

           totalBagelCostDouble += bagelCostDouble;

           totalCreamCheeseCostDouble += creamCheeseCostDouble;

           grandTotalDouble += totalCostDouble;

           totalBagelsBoughtInteger += quantityBagelInteger;

           averageBagelsBoughtDouble = totalBagelsBoughtInteger / counterInteger;





       }

       public double getTotalCost()

       {

           //returning total cost

           return totalCostDouble;

       }



       public double getBagelCost()

       {

           //returning total bagel cost

           return bagelCostDouble;

       }



       public double getCheeseCost()

       {

           //return total number of employees processed

           return creamCheeseCostDouble;

       }

       public double getTotalBagelCost()

       {

           //return total number of employees processed

           return totalBagelCostDouble;

       }

       public double getTotalCheeseCost()

       {

           //return total number of employees processed

           return totalCreamCheeseCostDouble;

       }

       public double getGrandTotal()

       {

           //return total number of employees processed

           return grandTotalDouble;

       }

       public double getTotalBagels()

       {

           //return total number of employees processed

           return totalBagelsBoughtInteger;

       }

       public double getAverageBagels()

       {

           //return total number of employees processed

           return averageBagelsBoughtDouble;

       }





    }


    When I output the results everything just shows up as "$0.00"

    I can't seem to find the problem!
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you help with the solution or looking for help too?



Similar Discussions: Problem w/ java calculation
  1. JAVA indexOf() method (Replies: 0)

  2. Scanner Help? (java) (Replies: 0)

Loading...