1

I am implemeting a circular queue in java which will accept objects (Employee) as entries. Now I have a method to edit the surname of the specific object, but somehow I cannot access the surname setter method found in the Employee class from the CQueue class even though I am importing all the required packages. Here is the code:

//PACKAGES IMPORTED
package linearstructures;
import dataobjects.*;
import linearnodes.*;

public class CQueue{
    Node front, rear, temp;
    boolean full = false;
    String key;

public AnyClass searchKey(String key)
{
    temp = rear.next; // first node

    do
    {
        if (temp.obj.getKey().equals(key))
            return temp.obj;
        temp = temp.next;
    } while (temp != rear.next);

    return null;
}

public AnyClass editObject(String key){
    int choice, newSeqNo;
    double newPay;
    boolean exit = false;
    Employee etemp = new Employee(); //INCLUDED THIS AFTER EDIT
    searchKey(key);

    if(searchKey(key) != null){
        temp.obj.getData();
        System.out.println();
        System.out.println("------------------------");
        System.out.print("Enter new Salary: ");
        newPay = sc.nextDouble();
        System.out.println("------------------------");
        System.out.println();
        etemp.setPay(newPay); //INCLUDED THIS AFTER EDIT
    else
        System.out.println("NO OBJECT WAS FOUND!");
    return null;
}
}

Employee class:

package dataobjects;

public class Employee extends AnyClass
{
public String surname;
public double pay;

public Employee(){}

public Employee(int seqNo, String surname, double pay)
{
    super(seqNo);
    this.surname = surname;
    this.pay = pay;
}

public double getSalary()
{
    return pay;
}

public void setPay(double newPay)
{
    pay = newPay;
}

public String getData()
{
    return super.getData() + ", Surname: " + surname + ", Pay: " + pay;
}

public String getKey()
{
    return surname;
}
}

AnyClass class:

package dataobjects;

public class AnyClass
{
public int seqNo;

public AnyClass(){}

public AnyClass(int seqNo)
{
    this.seqNo = seqNo;
}

public int getseqNo()
{
    return seqNo;
}

public void setseqNo(int seqNo) {
    this.seqNo = seqNo;
}

public String getData()
{

    return "Sequential Number - " + seqNo;
}

public String getKey()
{
    return Integer.toString(seqNo);     
}

public void edit(){}
}

Node Class

package linearnodes;
import dataobjects.*;

public class Node{
    public AnyClass obj;
    public Node next;
    public Node(AnyClass obj){
        next = null;
        this.obj = obj;
    }
}
John Floyd
  • 13
  • 5
  • 1
    What's a CQueue? Where's the code for this? – dbrown93 Dec 17 '15 at 10:51
  • @dbrown93 The code for it is the first piece of code. I included only the relavant methods (search and edit) and some declarations. It is the class that implements the Circular Queue. – John Floyd Dec 17 '15 at 10:53
  • What do you mean by "problem", is there a compiler error or an exception thrown? – dbrown93 Dec 17 '15 at 10:56
  • @dbrown93 It is saying that the `setPay()` method does not exists. The thing is that it is looking in the `AnyClass` however the `setPay()` is located in the `Employee` class. My question is how to access that class. – John Floyd Dec 17 '15 at 10:57
  • You access the setPay method through an employee object, if the "obj" in your temp node is not an employee, the compiler won't know where that method is – dbrown93 Dec 17 '15 at 11:00
  • @dbrown93 So all I have to do is add an `Employee` object ? Can you check the edited post please? – John Floyd Dec 17 '15 at 11:09
  • @Sascha `temp` is of type `Node`. When using `temp.obj` it will access the `AnyClass`. I edited the post to include the `Node` class. – John Floyd Dec 17 '15 at 11:11

2 Answers2

0

for include pay "etemp.setPay(newPay);" you will change return object to Employee.

public Employee editObject(String key){

Employee etemp = new Employee(); //INCLUDED THIS AFTER EDIT
....
....
etemp.setPay(newPay); //INCLUDED THIS AFTER EDIT
return etemp;
}

because AnyClass hasn't "public double pay;"

Khurram
  • 1
  • 2
0

Your editobject method could be something like this:

public AnyClass editObject(String key){
    // ...

    // Store your search result to avoid performing searchKey twice
    AnyClass searchResult = searchKey(key);

    if( searchResult != null ){
        // Check if searchResult is an Employee object
        if( searchResult instanceof Employee )
            // Cast AnyClass to Employee
            Employee empl = (Employee) searchResult;

            // Your logic here...

            // Set properties of the Employee object
            empl.setPay(newPay);

            // ...

           return empl;
        }
        // Add 'else if' here, if you need to manage other Object types
        // otherwise you can join previous if conditions 
    }
    else
        System.out.println("NO OBJECT WAS FOUND!");
    return null;
}

Your code creates a new local Employee instance that dies when the method ends, its value will be lost because no object points to it.

VincenzoC
  • 30,117
  • 12
  • 90
  • 112