简体   繁体   中英

How to access an object of another class

So I got this in a main program. As visible below I am first trying to edit the pay "45.0" by a raise of 10% of a single Employee object. Here I am making sure that my math calculation are correct. In another class I need to write a code to add the 10% to all the Employees.

temp.obj.getData() shows all the Employee data entered in the main class while the new object e is empty. Does someone know how I can get the data from the main program to object e or without creating object e. My problem is that when I am doing the same operations on object e the calculation of the pay are all 0. I think this is due to pay = 0 . I seem to have a problem of getting the data (pay values ) from the objects in main class. Can someone help? Thanks if there is the need of more code ask me.

MAIN PROG:

            etemp[15] = new Employee(134, "Nicole", 45.0); 
            queue.put(etemp[15]);
            if(queue!=null){
                //Attempt 1  : Editing pay of a single employee
                System.out.println("not empty!");
                double x = etemp[15].getSalary();
                double calc = (x*0.10)+x;
                etemp[15].setPay(calc);
                System.out.println("Raise: "+etemp[15].getData());
                //Attempt 2 : Using a method to edit pay of ALL employee
                queue.pay(10);
            } else {
                System.out.println("empty");
            }

METHOD IN OTHER CLASS:

public void pay(int p){   
    int id = 0;
    String k = "";
    double pay = 0;        
    Employee e = new Employee(id, k, pay);
    CircQueue q = new CircQueue(20);
    if(rear!=null){
        Node temp=front;
        do{
            for (int i= 0; i<currNodes; i++)
            {
                System.out.println(temp.obj.getData()); 
                q.put(e);
                q.listAll();
                double x = e.getSalary();
                double calc = (x*0.10)+x;
                System.out.println("Raise: "+calc);
                temp = temp.next;
            }
        }while(!(temp==rear.next));
    } else System.out.println("Empty queue!");
} 

You have to use this method to pass your own instance as an argument. If I understood correctly, you need to use the etemp array in the other class, the one having the pay method. If that is right, you don't have to create more objects.

etemp[15] = new Employee(134, "Nicole", 45.0); 
            queue.put(etemp[15]);
            if(queue!=null){
                //Attempt 1  : Editing pay of a single employee
                System.out.println("not empty!");
                double x = etemp[15].getSalary();
                double calc = (x*0.10)+x;
                etemp[15].setPay(calc);
                System.out.println("Raise: "+etemp[15].getData());
                //Attempt 2 : Using a method to edit pay of ALL employee
                queue.pay(this,10); //Adding our own instance
            } else {
                System.out.println("empty");
            }

METHOD IN OTHER CLASS:

public void pay(MainClass m, int p){ 

    // Here you can use m as the instance, it will have the etemp. Normally, atributes are private, so you have to implement some kind of getters to reach to that array
    int id = 0;
    String k = "";
    double pay = m.getOriginalPay(id); // For example     
    Employee e = new Employee(id, k, pay);
    CircQueue q = new CircQueue(20);
    if(rear!=null){
        Node temp=front;
        do{
            for (int i= 0; i<currNodes; i++)
            {
                System.out.println(temp.obj.getData()); 
                q.put(e);
                q.listAll();
                double x = e.getSalary();
                double calc = (x*0.10)+x;
                System.out.println("Raise: "+calc);
                temp = temp.next;
            }
        }while(!(temp==rear.next));
    } else System.out.println("Empty queue!");
} 

Tell me if I helped you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM