Results 1 to 3 of 3
  1. #1
    JacobRocks is offline Member
    Join Date
    Oct 2014
    Posts
    1
    Rep Power
    0

    Default Really need help with java applet project

    Hi! I really need help. This is due tomorrow and I can't figure it out. I've spent several hours on it. Anyway, it will come up but when I input something it always just equals up to one dollar. Also, I'm supposed to add some function where the applet will keep adding up all the stuff put in, but I don't know how.




    /*
    Chapter 4: Overdue Books
    Programmer: Jacob Lindsay
    Date: October 9, 2014
    Filename: OverdueBooksApplet.java
    Purpose: The purpose of this applet is to calculate various overdue library items, add up the sum
    total of one or multiple items, and display a total amount to be charged to the customer.
    */

    import java.awt.*; //Imports the 'Abstract Window Toolkit' which is the first step in converting an application into an applet. This step enables applets to access specific elements likes colors and methods for drawing.
    import java.applet.*; //Another piece in converting an application into an applet, this step allows applets to take on certain attributes and the ability to manipulate classes.
    import java.awt.event.*; //Imports the all the classes used for creating interfaces, graphics, and images.
    import java.text.DecimalFormat; //

    public class OverdueBooksApplet extends Applet implements ItemListener
    {
    //declares variables that will be used in the applet and constructs a color to be used
    double dollars, answer;
    int empCode;
    Color darkRed = new Color(160, 50, 0);

    Label welcomeLabel = new Label("Welcome to Anderson Public Library system");
    Label promptLabel = new Label("Input items to calculate total overdue amount");
    TextField totaloverdueField = new TextField(10);

    CheckboxGroup codeGroup = new CheckboxGroup(); //Implements a group of check boxes into the applets
    Checkbox booksBox= new Checkbox("Books",false,codeGroup); //Labels one of the newly created check boxes "Books" and sets it so that it will appear unchecked until the user checks it
    Checkbox magazineBox = new Checkbox("Magazines",false,codeGroup); //Labels one of the newly created check boxes "Magazines" and sets it so that it will appear unchecked until the user checks it
    Checkbox dvdBox = new Checkbox("DVDs",false,codeGroup); //Labels one of the newly created check boxes "DVDs" and sets it so that it will appear unchecked until the user checks it
    Checkbox cdBox= new Checkbox("CDs",false,codeGroup); //Labels one of the newly created check boxes "CDs" and sets it so that it will appear unchecked until the user checks it
    Checkbox audiobooksBox = new Checkbox("Audio Books",false,codeGroup); //Labels one of the newly created check boxes "Audio Books" and sets it so that it will appear unchecked until the user checks it
    Checkbox vinylsBox = new Checkbox("Vinyls",false,codeGroup); //Labels one of the newly created check boxes "Vinyls" and sets it so that it will appear unchecked until the user checks it
    Checkbox hiddenBox = new Checkbox("",true,codeGroup); //Sets the last of the newly created check boxes to "hidden" so that it won't be displayed in the interface. It is also set to "checked" until one of the other boxes is check by the user, simply because one must remain checked at all times

    Label outputLabel = new Label("Click an option button to calculate the sales commission."); //Tells the applet to display an label with the text "Click an option button to calculate the sales commission" after the user inputs information to be calculated

    public void init()
    {
    setBackground(darkRed);
    setForeground(Color.black);
    add(welcomeLabel);
    add(promptLabel);
    add(totaloverdueField);
    totaloverdueField.requestFocus();
    totaloverdueField.setForeground(Color.black);
    add(booksBox);
    booksBox.addItemListener(this);
    add(magazineBox);
    magazineBox.addItemListener(this);
    add(dvdBox);
    dvdBox.addItemListener(this);
    add(cdBox);
    cdBox.addItemListener(this);
    add(audiobooksBox);
    audiobooksBox.addItemListener(this);
    add(vinylsBox);
    vinylsBox.addItemListener(this);
    add(outputLabel);
    }

    public void itemStateChanged(ItemEvent choice) //This method is triggered by the user clicking an option button
    {
    try //This action tests out the information that is inputted by the user to makes sure it is a number that will function correctly with the specified equation
    {
    dollars = getTotal(); //Retrieves the information from the getSales field
    empCode = getCode(); //Tells the applet to withhold an answer because the information inputted is invalid
    answer = getTotal(dollars, empCode); //Retrieves that computed answer
    output(answer, dollars); //Displays the answer
    }

    catch (NumberFormatException e) //Causes an exception to run in the case that an invalid number is inputted
    {
    outputLabel.setText("You must enter a dollar amount greater than zero."); //Creates a label to be shown if an invalid number is inputted
    hiddenBox.setState(true); //Causes the hidden box to be reset to true if an invalid number is inputted
    totaloverdueField.setText(""); //Resets the information in the textfield in the case of an invalid input
    totaloverdueField.requestFocus(); //Tells applet to focus the text in the input sales field
    }
    }
    public double getTotal() //Retrieves the info from the "getSales" field
    {
    double totaloverdue = Double.parseDouble(totaloverdueField.getText()); //Parses the inputted information into the "salesfield"

    if (totaloverdue <= 0) throw new NumberFormatException(); //Says that if the sales input is equal to or less than zero, than the input is invalid

    return totaloverdue; //Says if the input is valid then the input should be computed
    }

    public int getCode() //initiates the getCode method
    {
    int code = 0; //decalres a variable named code and assigns it a variable of zero
    if (booksBox.getState()) code = 1; //if booksBox is checked, use code 1
    else //or
    if (magazineBox.getState()) code = 2; //if magazinesBox is checked, use code 2
    else //or
    if (dvdBox.getState()) code =3; //if dvdBox is checked, use code 3
    else //or
    if (cdBox.getState()) code =4; //if cdBox is checked, use code 4
    else //or
    if (audiobooksBox.getState()) code = 5; //if audiobooksBox is checked, use code 5
    else //or
    if (vinylsBox.getState()) code = 6; //if vinylsBox is checked, use code 6
    return code; //the appropriate variable code is returned to the calling init() method
    }

    public double getTotal(double totaloverdue, int code) //initiates the getTotal method
    {
    double fee = 0.0; //creating a variable and assigning it to be a real number, in order for it to have decimal points
    switch(code) //uses a switch statement evaluates the value of the code and applies it to the correct case
    {
    case 1: //if the first checkbox is checked, a .50 value is multiplied by the overdue amount entered
    fee = .50 * totaloverdue;
    break;

    case 2: //if the second checkbox is checked, a .30 value is multiplied by the overdue amount entered
    fee = .25 * totaloverdue;
    break;

    case 3: //if the third checkbox is checked, a .55 value is multiplied by the overdue amount entered
    fee = .75 * totaloverdue;
    break;

    case 4: //if the fourth checkbox is checked, a .15 value is multiplied by the overdue amount entered
    fee = .75 * totaloverdue;
    break;

    case 5: //if the fifth checkbox is checked, a .25 value is multiplied by the overdue amount entered
    fee = .75 * totaloverdue;
    break;

    case 6: //if the sixth checkbox is checked, a .17 value is multiplied by the overdue amount entered
    fee = 1.00 * totaloverdue;
    break;
    }
    return fee; //the appropriate variable code is returned to the calling init() method
    }


    public void output(double fee, double totaloverdue) //Specifies how the output should be put once computed
    {
    DecimalFormat twoDigits = new DecimalFormat("$#,0.00"); //Sets the format for the answer
    outputLabel.setText("Your overdue total is " + twoDigits.format(totaloverdue)); //Displays the numerical answer output with the specified text
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Really need help with java applet project

    Forum Rules
    BB Code List - Java Programming Forum - Learn Java Programming

    Is this a free course? I wouldn't pay to learn obsolete technology like Applets and AWT.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Really need help with java applet project

    Hi Jacob,

    Only the value in totaloverdueField is currently displayed. Although answer is passed in to output(), it is never used.
    If other errors exist then you can use print statements to check the values of each variable.

    As for the counting, all you would need is another variable which increments by the total due as each fee is calculated.

    Following Darryl's second link, please edit your initial post and wrap your code in [code][/code] tags.



    Regards.

Similar Threads

  1. Replies: 1
    Last Post: 07-19-2014, 09:55 PM
  2. Replies: 2
    Last Post: 06-26-2014, 07:43 PM
  3. Replies: 2
    Last Post: 12-23-2012, 08:22 AM
  4. Replies: 0
    Last Post: 10-21-2012, 06:56 PM
  5. Replies: 5
    Last Post: 08-27-2011, 02:34 PM

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
  •