Results 1 to 1 of 1
  1. #1
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Problem with variable values

    Hi guys,

    I have a problem with my code which I am supposed to submit in half an hour.

    I have a Test class and a User class.

    in my method 1 of Test class I am incrementing one of the instance attributes of the User class. But when I proceed to method 2 of Test class, the instance attributes of User class reset to zero.

    Kindly help.

    Thank you.

    Here is a piece of my code:
    Java Code:
    public class Test
    {
    ....method1()
    {
     // add xchange points
            userBidWinner.setXchangePoints(1); 
            itemSell.getSeller().setXchangePoints(2); 
    }
    
    ...method 2()
    {
    
            UserManager newUserManager = new UserManager();
            ArrayList<User> newUserList = newUserManager.getAllUsers();
            ArrayList<User> editedUserList = new ArrayList<User>();
            int compare = 0;
            
            for(int i = 0; i < newUserList.size(); i++)
            {   
                if(newUserList.get(i).getXchangePoints() > compare)
                {
                    compare = newUserList.get(i).getXchangePoints();
                }
            }
            
            
            for(int i = 0; i < newUserList.size(); i++)
            {
                int points = newUserList.get(i).getXchangePoints();
                if(points == compare)
                {
                    editedUserList.add(newUserList.get(i));
                }
            }
            
            
            if(compare == 0)
            {
                System.out.println("There is no user having maximum xchange points");
            }
            else
            {
                System.out.println("User(s) with the highest number of Xchange points:\nUsername            Xchange Points\n====================================\n");
                
                for(int i = 0; i < editedUserList.size(); i++)
                {
                    System.out.println(editedUserList.get(i).getName()+"                 "+compare);
                }
            }
    }
    }
    
    
    
    public class User()
    {
    private String userName;
        private double balance;
        private int xchangePoints;
        
    
        public User(String userName, double balance) 
        {
            this.userName = userName;
            this.balance = balance;
        }
        
        public String getName()
        {
            return userName;
        }
        
        public double getBalance()
        {
            return balance;
        }
        
        public void debitBalance(double debit)
        {
            balance -= debit;
        }
        
        public void creditBalance(double credit)
        {
            balance += credit;
        }
        
        public void setXchangePoints(int i)
        {
            if( i == 1)
            {
                xchangePoints ++ ;
            }
            else
            {
                xchangePoints += 2;
            }
        }
        
        public int getXchangePoints()
        {
            return xchangePoints;
        }
    }
    Last edited by JosAH; 11-16-2011 at 02:47 PM. Reason: added [code] ... [/code] tags

Similar Threads

  1. Passing a variable problem.
    By kaizen9001 in forum New To Java
    Replies: 12
    Last Post: 04-30-2011, 05:02 PM
  2. Replies: 3
    Last Post: 05-12-2010, 01:10 PM
  3. sorting variable values in ascending order?
    By jeffrey in forum New To Java
    Replies: 0
    Last Post: 07-14-2009, 08:59 AM
  4. Get all values of variable that it can take
    By umarsharief in forum New To Java
    Replies: 9
    Last Post: 01-20-2009, 03:35 AM
  5. How to get variable values at runtime in another process
    By murali_java in forum Advanced Java
    Replies: 0
    Last Post: 11-02-2007, 09:03 AM

Posting Permissions

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