Results 1 to 8 of 8
  1. #1
    CJeremy is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Exclamation Need help with applet calculating a price!! URGENT!

    I am working on my final for JAVA, and can't seem to see why it wont give me a price for packageCost. :confused: See code below: (may be hard to follow with cut and paste)

    public class ComputerPackagesApplet extends Applet implements ItemListener
    {
    //declare class variables
    double packageCost, computer;
    int empCode;
    Image dollarSign;
    Color darkRed = new Color(160, 50, 0);

    //Create components for applet

    Label codeLabel = new Label(" Select the requested computer peripheral:");

    CheckboxGroup codeGroup = new CheckboxGroup();
    Checkbox printerBox = new Checkbox("Printer",false,codeGroup);
    Checkbox scannerBox = new Checkbox("Scanner",false,codeGroup);
    Checkbox webcamBox = new Checkbox("Webcam",false,codeGroup);
    Checkbox externalHDBox = new Checkbox("External HD",false,codeGroup);
    Checkbox gameControllerBox = new Checkbox("Game Controller",false,codeGroup);
    Checkbox printerScannerCopierBox = new Checkbox("Printer/Scanner/Copier device",false,codeGroup);
    Checkbox hiddenBox = new Checkbox("",true,codeGroup);

    Label outputLabel = new Label("Click an option button to calculate your package price with the peripheral of your choice.");

    public void init()
    {
    setBackground(Color.blue);
    setForeground(Color.white);
    add(codeLabel);
    add(printerBox);
    printerBox.addItemListener(this);
    add(scannerBox);
    scannerBox.addItemListener(this);
    add(webcamBox);
    webcamBox.addItemListener(this);
    add(externalHDBox);
    externalHDBox.addItemListener(this);
    add(gameControllerBox);
    gameControllerBox.addItemListener(this);
    add(printerScannerCopierBox);
    printerScannerCopierBox.addItemListener(this);
    add(outputLabel);
    }

    //This method is triggered by the user clicking an option button
    public void itemStateChanged(ItemEvent choice)
    {
    try
    {
    empCode = getCode();
    output(packageCost);
    }

    catch (NumberFormatException e)
    {
    outputLabel.setText("You must enter a dollar amount greater than zero.");
    hiddenBox.setState(true);

    }
    }

    public int getCode()
    {
    int code = 0;
    if (printerBox.getState()) code = 1;
    else
    if (scannerBox.getState()) code = 2;
    else
    if (webcamBox.getState()) code = 3;
    else
    if (externalHDBox.getState()) code = 4;
    else
    if (gameControllerBox.getState()) code = 5;
    else
    if (printerScannerCopierBox.getState()) code = 6;
    return code;
    }

    public double getPrice(double packageCost, int code, double computer)
    {
    double printer,scanner, webcam, externalHD, gameController, printScanCop;

    packageCost = 0;
    computer = 500;
    printer = 30;
    scanner = 60;
    webcam = 70;
    externalHD = 90;
    gameController = 50;
    printScanCop = 80;

    switch(code)
    {
    case 1:
    packageCost = computer + printer;
    break;

    case 2:
    packageCost = computer + scanner;
    break;

    case 3:
    packageCost = computer + webcam;
    break;

    case 4:
    packageCost = computer + externalHD;
    break;

    case 5:
    packageCost = computer + gameController;
    break;

    case 6:
    packageCost = computer + printScanCop;
    break;
    }
    return packageCost;
    }

    public void output(double packageCost)
    {
    DecimalFormat twoDigits = new DecimalFormat("$##0.00");
    outputLabel.setText("Your package cost is " + twoDigits.format(packageCost) + ".");
    }

    public void paint(Graphics g)
    {
    dollarSign = getImage(getDocumentBase(), "dollarSign.gif");
    g.drawImage(dollarSign,12,28,this);
    }
    }

  2. #2
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    look at the variable packageprice. Is it what it should be? Where do you call getPrice?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Marking your thread URGENT tends to get you less attention. That being said, please use code tags to help us read the code easier and supply a solid question, with the necessary details for us to help you.

    use code tags like this
    [code ]
    your code here
    [/code]

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sunde887 View Post
    Marking your thread URGENT tends to get you less attention. That being said, please use code tags to help us read the code easier and supply a solid question, with the necessary details for us to help you.
    Agree. *Urgent* implies that the poster thinks that his post is more important than any other post here, and for us, it isn't. This is being passed to you so that you won't run into this problem in the future and hurt your chances of getting help.

  5. #5
    CJeremy is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you! I was able to work it out after your help.

  6. #6
    CJeremy is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Sorry, first post here, and didn't know the details for everything. Thanks for the help people.

  7. #7
    CJeremy is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Next question is how do I end this post?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I was just about to answer this for you. When you are finished you can mark a thread solved with the thread tools at the top. I am glad you solved it, and if you run into problems don't hesitate to come here for help. Don't forget code tags though, and good title names with clear questions inside.

Similar Threads

  1. Update a price in a database
    By sandraW in forum New To Java
    Replies: 14
    Last Post: 08-29-2010, 05:41 AM
  2. ERROR DEPLOYING WEB APPLET......help needed urgent
    By bonjovi4u in forum Java Applets
    Replies: 1
    Last Post: 03-16-2010, 04:16 PM
  3. Replies: 1
    Last Post: 12-23-2009, 01:24 AM
  4. Replies: 23
    Last Post: 02-19-2009, 06:24 AM
  5. Displaying a price with a checkbox
    By F4iChick02 in forum New To Java
    Replies: 4
    Last Post: 12-09-2008, 06:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •