4 Replies - 935 Views - Last Post: 01 May 2008 - 06:00 PM Rate Topic: -----

#1 qianasykes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-October 06

Java problem in coding calculations...

Posted 01 May 2008 - 12:40 PM

I am still having problems with the program. Can someone tell me where I am going wrong? The code that I am trying to calcualte should be the total of an amount inputted by the user and he delivery charge from a checkbox component. Here is my code>

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class CandleApplet extends Applet implements ItemListener
{

//Declares variables
double dollars, answer,sales, amount;
int empCode;

//Create components for applet
Label candleLabel = new Label("CandleLine -- Candles Online");

Label orderLabel = new Label("Please enter the total dollar amount of your order:");
TextField amountField = new TextField(20);

Label shippingLabel = new Label("Please choose your method of shipping:");

CheckboxGroup deliveryGroup = new CheckboxGroup();
Checkbox priorityBox = new Checkbox("Priority (Overnight)",false,deliveryGroup);
Checkbox expressBox = new Checkbox("Express (2 business days)",false,deliveryGroup);
Checkbox standardBox = new Checkbox("Standard (3 to 7 business days)",false,deliveryGroup);
Checkbox hiddenBox = new Checkbox("", true,deliveryGroup);

Label outputLabel = new Label("We guarantee on time delivery, or your money back.");

public void init()
{

setBackground(Color.cyan);
setForeground(Color.black);
add(candleLabel);
add(orderLabel);
add(amountField);
amountField.requestFocus();
amountField.setForeground(Color.black);
add(shippingLabel);
add(priorityBox);
priorityBox.addItemListener(this);
add(expressBox);
expressBox.addItemListener(this);
add(standardBox);
standardBox.addItemListener(this);
add(outputLabel);
}
//This method is triggered by the user clicking an option button.
public void itemStateChanged(ItemEvent choice)
{
try
{
dollars = getShipping();
empCode = getCode();
answer = getAmount(dollars,empCode);
output(answer, dollars);
}

catch (NumberFormatException e)
{
outputLabel.setText("If your order is more than $100.00 we will ship it for free.");
hiddenBox.setState(true);
amountField.setText("");
amountField.requestFocus();
}
}

public double getShipping()
{
double dollars = Double.parseDouble(amountField.getText());

if (dollars >=100.01) throw new NumberFormatException();

return dollars;
}

public int getCode()
{
int code = 0;
if (priorityBox.getState()) code = 1;
else
if (expressBox.getState()) code = 2;
else
if (standardBox.getState()) code = 3;
return code;
}

public double getAmount(double dollars, int code)
{
double [] shippingFees = {16.95, 13.95, 7.95};
return dollars + shippingFees[code-1];
}

public void output(double amount, double sales)
{
DecimalFormat twoDigits = new DecimalFormat("$#,00.00");
outputLabel.setText("The total of your order is" + twoDigits.format(sales));
}
}


*edit: Forgot the forward slash in the closing tag. Thanks for using the code tags. :)

This post has been edited by Martyr2: 01 May 2008 - 12:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java problem in coding calculations...

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Java problem in coding calculations...

Posted 01 May 2008 - 12:55 PM

View Postqianasykes, on 1 May, 2008 - 12:40 PM, said:

I am still having problems with the program. Can someone tell me where I am going wrong? The code that I am trying to calcualte should be the total of an amount inputted by the user and he delivery charge from a checkbox component. Here is my code>


I have already answered that question


public void output(double amount, double sales)
{
   DecimalFormat twoDigits = new DecimalFormat("$#,00.00");
   outputLabel.setText("The total of your order is" + twoDigits.format(sales));
}



You are displaying sales
You should display amount
Actually this method does not need 2 parameters only amount is required
Was This Post Helpful? 0
  • +
  • -

#3 qianasykes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-October 06

Re: Java problem in coding calculations...

Posted 01 May 2008 - 04:40 PM

PBL,
I have changed what you told me to change and I am still having a problem. The problem I am having is when I put in numbers and I try to select one of the checkboxes in order to get a total nothing comes up and my output text for the total amount of the order comes up with 0.00 dollars.

here is the code with the changes
[code]import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class CandleApplet extends Applet implements ItemListener
{

//Declares variables
double dollars,answer,amount;
int empCode;

//Create components for applet
Label candleLabel = new Label("CandleLine -- Candles Online");

Label orderLabel = new Label("Please enter the total dollar amount of your order:");
TextField amountField = new TextField(20);

Label shippingLabel = new Label("Please choose your method of shipping:");

CheckboxGroup deliveryGroup = new CheckboxGroup();
Checkbox priorityBox = new Checkbox("Priority (Overnight)",false,deliveryGroup);
Checkbox expressBox = new Checkbox("Express (2 business days)",false,deliveryGroup);
Checkbox standardBox = new Checkbox("Standard (3 to 7 business days)",false,deliveryGroup);
Checkbox hiddenBox = new Checkbox("", true,deliveryGroup);

Label outputLabel = new Label("We guarantee on time delivery, or your money back.");

public void init()
{

setBackground(Color.cyan);
setForeground(Color.black);
add(candleLabel);
add(orderLabel);
add(amountField);
amountField.requestFocus();
amountField.setForeground(Color.black);
add(shippingLabel);
add(priorityBox);
priorityBox.addItemListener(this);
add(expressBox);
expressBox.addItemListener(this);
add(standardBox);
standardBox.addItemListener(this);
add(outputLabel);
}
//This method is triggered by the user clicking an option button.
public void itemStateChanged(ItemEvent choice)
{
try
{
dollars = getShipping();
empCode = getCode();
answer = getAmount(dollars,empCode);
output(amount);
}

catch (NumberFormatException e)
{
outputLabel.setText("If your order is more than $100.00 we will ship it for free.");
hiddenBox.setState(true);
amountField.setText("");
amountField.requestFocus();
}
}

public double getShipping()
{
double dollars = Double.parseDouble(amountField.getText());

if (dollars >=100.01) throw new NumberFormatException();

return dollars;
}

public int getCode()
{
int code = 0;
if (priorityBox.getState()) code = 1;
else
if (expressBox.getState()) code = 2;
else
if (standardBox.getState()) code = 3;
return code;
}

public double getAmount(double dollars, int code)
{
double [] shippingFees = {16.95, 13.95, 7.95};
return dollars + shippingFees[code-1];
}

public void output(double amount)
{
DecimalFormat twoDigits = new DecimalFormat("$#,00.00");
outputLabel.setText("The total of your order is" + twoDigits.format(amount));
}
}[code]
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Java problem in coding calculations...

Posted 01 May 2008 - 05:23 PM

You took the wrong amount... don't mixed up the name of the arguments in the method call and in the method itself

you want the value returned by getAmount(...)
so

   // value of payment is in variable answer
   answer = getAmount(dollars,empCode);
   output(answer);



not

   output(amount);


Was This Post Helpful? 0
  • +
  • -

#5 qianasykes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-October 06

Re: Java problem in coding calculations...

Posted 01 May 2008 - 06:00 PM

Thank you Pbl,

My code is running perfectly now. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1