Why Does My Java Bagel Shop Application Show All Costs as $0.00?

In summary, it is recommended to carefully review the code and check for any potential errors, use debugging tools to track values, and double check for typos that may be causing the incorrect calculation.
  • #1
clook
35
0
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:
import javax.swing.*;
Code:
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:
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!
 
Physics news on Phys.org
  • #2
Please help.


I would suggest taking a step back and reviewing the code carefully to identify any potential errors that may be causing the incorrect calculation. Here are a few suggestions:

1. Check the input values: Make sure that the values entered by the user for the quantity of bagels and cream cheese are being correctly converted to integers in the getInput() method. Also, make sure that the values are not accidentally being reset or changed in any other methods.

2. Verify the calculation: Double check the calculation in the calculate() method to make sure that the correct variables and values are being used. You can also use a debugger or add print statements to track the values of different variables as the code runs.

3. Debugging tools: If you are using an IDE, try using the debugging tools to step through the code and see where the incorrect calculation is occurring. This can help pinpoint the issue more easily.

4. Check for typos: Sometimes, a simple typo in a variable name or method call can cause unexpected results. Make sure that all variable names and method calls are correct and consistent throughout the code.

I hope these suggestions help you identify and fix the issue with your calculation. Good luck!
 
  • #3




I would suggest taking a closer look at the calculation class. There may be an issue with the way the variables are being assigned and used in the calculations. It is also important to check for any errors or warnings in the code, as they can often point to the source of the problem. Additionally, I would recommend using debugging tools to step through the code and see where the values are going wrong. It may also be helpful to print out the values of the variables at different stages in the calculation to see where the issue is occurring. With careful examination and testing, the problem with the calculation should be able to be identified and resolved.
 

1. Why is my Java calculation not giving the correct result?

There are a few possible reasons for this. First, check your code to make sure you are using the correct mathematical operators and have the correct order of operations. Additionally, make sure you are using the correct data types for your variables. It's also possible that there is a bug or error in your code that is causing the incorrect result. Debugging tools and techniques can help you identify and fix any issues.

2. How can I improve the efficiency of my Java calculation?

One way to improve efficiency is to use appropriate data types for your variables. For example, using a double instead of an int for decimal values can prevent rounding errors. Another approach is to use built-in Java functions or libraries instead of writing your own code, as these are often optimized for efficiency. Additionally, consider using loops or other control structures to avoid repeating the same calculations multiple times.

3. Why is my Java calculation returning an error or exception?

This could be due to a variety of reasons. Check your code for syntax errors, missing semicolons, or incorrect variable names. It's also possible that you are trying to perform a calculation with incompatible data types, such as adding a string to an integer. Make sure you are handling any potential errors or exceptions in your code to prevent unexpected results.

4. Can I use Java to perform complex calculations?

Yes, Java is capable of handling complex mathematical calculations. However, it is important to carefully plan and structure your code to avoid errors and ensure accuracy. Additionally, using external libraries or packages can provide additional functionality for more complicated calculations.

5. How can I test my Java calculations to ensure they are accurate?

One way to test your calculations is to use predefined test cases and compare the results to the expected outcome. This can help identify any errors or inconsistencies in your code. Another approach is to use a debugger or step through your code line by line to see how each calculation is being performed. Additionally, you can use mathematical principles or equations to verify the accuracy of your results.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Back
Top