# Homework Help: Problem w/ java calculation

1. May 7, 2008

### clook

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;

//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(new JLabel("       Type of Bagel            "));

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

//add the panel to the frame

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)

{

}

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();

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 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

}

public double getBagelCost()

{

//returning total bagel cost

return bagelCostDouble;

}

public double getCheeseCost()

{

return creamCheeseCostDouble;

}

public double getTotalBagelCost()

{

}

public double getTotalCheeseCost()

{

}

public double getGrandTotal()

{

return grandTotalDouble;

}

public double getTotalBagels()

{

}

public double getAverageBagels()

{

return averageBagelsBoughtDouble;

}

}

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

I can't seem to find the problem!