Results 1 to 6 of 6
  1. #1
    s34nn4 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default [SOLVED] reseting a value

    Hi everyone its a pleasure to be here again. Im a novice in Java and my instructor send us a home work on regards to Radio button. Basically, my problem with my applet was... everytime I hit reset button, the value still remain. Supposedly the whole data must reset and get back from where it should started once I hit reset, but apparently it doesn't seem to do anything... it was just hidding the value.


    here's my code is there something I miss?

    Thank in advance.

    Java Code:
    
    
    
    /**
     *
     * @author Sean
     */
    import java.awt.*;
    import java.applet.*; 
    import java.awt.event.*; 
    
    
    public class overdueItems extends Applet implements ActionListener,ItemListener{
          
     int bookspenalty=5, recordspenalty=7, tapespenalty=3, cdpenalty=4, videotapespenalty=6;
     int books=0, records=0, tapes=0, cd=0, videotapes=0;
    
     int bookitem,recorditem,itemprice;
     
     boolean done=false;
     int price=0;
     int total_overdueitems=0;
     int total_overduecharge=0;
     
        Label hiddenagainLabel = new Label("");
        Label companyName = new Label("      Hill Brookes Library       ");
        Label hiddenLabel = new Label("");
        Label heading = new Label("    Overdue Item              Price     ");
        
        Label bookstabulate = new Label("Books : ");
        Label recordstabulate = new Label("Records : ");
        Label tapestabulate = new Label("Tapes : ");
        Label cdtabulate = new Label("CD's : ");
        Label videotapetabulate = new Label("Videotapes : ");
        Label outputLabel = new Label("Total Overdue Charge : ");
        Label outputLabel2 = new Label("Total Overdue Items :");
        
    CheckboxGroup itemGroup = new CheckboxGroup();
     
        Checkbox bookcheckBox = new Checkbox("Books                $5.00",false,itemGroup);
        Checkbox recordcheckBox = new Checkbox("Records            $7.00",false,itemGroup);
        Checkbox tapescheckBox = new Checkbox (" Tapes               $3.00",false, itemGroup);
        Checkbox cdcheckBox = new Checkbox ("CD's                  $4.00",false,itemGroup);
        Checkbox videotapescheckBox = new Checkbox ("  Videotapes     $6.00",false,itemGroup);
        Checkbox hiddencheckBox = new Checkbox ("",true);
        Button calcButton = new Button ("Calculate");
        Button addButton = new Button ("Add");
        Button clearButton = new Button ("Clear");
        
        
        public void init() {
            
            setSize(170,450);
            setBackground (Color.yellow);
            
             add(hiddenagainLabel);
             add(companyName);
             add(hiddenLabel);
             add(heading);        
             add(bookcheckBox);
                bookcheckBox.addItemListener(this);  
             add(recordcheckBox);
                recordcheckBox.addItemListener(this);  
             add(tapescheckBox);
                tapescheckBox.addItemListener(this);  
             add(cdcheckBox);
                cdcheckBox.addItemListener(this);
             add(videotapescheckBox);
                videotapescheckBox.addItemListener(this);  
              add(addButton);
                addButton.addActionListener(this);   
             add(calcButton);
                calcButton.addActionListener(this);        
             add(clearButton);
                 clearButton.addActionListener(this);
             
             add(bookstabulate);
             
             add(recordstabulate);
             
             add(tapestabulate);
             
             add(cdtabulate);
             
             add(videotapetabulate);
             
             add(outputLabel2);
             
             add(outputLabel);
             
        }
    
        
        public void itemStateChanged(ItemEvent choice) 
        {
            
            
                
            if (bookcheckBox.getState())
            {
             bookstabulate.setText("Books : "+(++books)); 
           
            }
            
            if(recordcheckBox.getState())
              
            {
             recordstabulate.setText("Records : "+(++records));
            }
            
            
            if (tapescheckBox.getState())
                
            {
             tapestabulate.setText("Tapes : "+(++tapes)); 
            }
              
              
            if (cdcheckBox.getState())
              
            {
            cdtabulate.setText("CD's : "+(++cd)); 
            }
            
        
            if (videotapescheckBox.getState())
              
                    {
                    videotapetabulate.setText("Videotapes : "+(++videotapes)); 
            }
            
            else
                
            itemprice = (books+records+tapes+cd+videotapes);
            outputLabel2.setText("Total Overdue Items : "+(itemprice));
                
            
            
            
        }
        
        
        public void actionPerformed(ActionEvent n)
        {
                 
            {
          
          try
          {
                           
            String arg=n.getActionCommand();
            
            while (arg == "Clear") throw new NumberFormatException();
            
            
            
         
        
            {
            if (bookcheckBox.getState())
            total_overduecharge+=bookspenalty*books;
          
         
            if (recordcheckBox.getState())
            total_overduecharge+=recordspenalty*records;
            
            if (tapescheckBox.getState())
            total_overduecharge+=tapespenalty*tapes;
      
            if (cdcheckBox.getState())
            total_overduecharge+=cdpenalty*cd;
            
            if (videotapescheckBox.getState())
            total_overduecharge+=videotapespenalty*videotapes;
         
                
            total_overduecharge = (books*(bookspenalty)+records*(recordspenalty)
                                    +tapes*(tapespenalty)+cd*(cdpenalty)+videotapes*(videotapespenalty));
            
           
            outputLabel.setText("Your Overdue Charge $"+total_overduecharge); 
           }
         }   
        catch (NumberFormatException m )
        {
    
          
        
                    
       bookstabulate.setText("Books : ");
       recordstabulate.setText("Records :");
       tapestabulate.setText("Tapes :"); 
       cdtabulate.setText("CD's :"); 
       videotapetabulate.setText("Videotapes : "); 
       outputLabel2.setText("Total Overdue Items : ");
       outputLabel.setText("Your Overdue Charge : ");
       {
       bookcheckBox.setState(false);
       recordcheckBox.setState(false);
       tapescheckBox.setState(false);
       cdcheckBox.setState(false);
       videotapescheckBox.setState(false);
       hiddencheckBox.setState(true);
           
       
    
                }
             }
          }
       }
    }

  2. #2
    s34nn4 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    Ok! I answer my question again. There is one thing I miss. In this part you need to declare a looping in order to reset the value I use a while loop and increment it on the actionperformed. this is know solved after few cups of coffee.

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

    Default

    Be careful when comparing Strings. For instance, you don't compare strings with == but instead with equals:

    Not
    Java Code:
    if (arg == "Clear")
    but instead
    Java Code:
    if (arg.equals("Clear"))

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    Be careful when comparing Strings. For instance, you don't compare strings with == but instead with equals:
    I think it must. Not a good practice to get familiar with the use of == to compare strings.

  5. #5
    s34nn4 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    Ok thanks folks. Ill keep that on my mind.

    yellow line is gone now.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by s34nn4 View Post
    Ok thanks folks. Ill keep that on my mind.

    yellow line is gone now.
    If you can remove all such warnings too, much better. Nice to familiar with in future too.

Posting Permissions

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