0

I am trying to store data that I'm receiving from a client to a LinkedLsit of Jobs, but everytime i add multiple jobs the list overwrites itself to display the latest value added into the list.

The classes below are the ones I'm calling to store the data from my main server in which I'm calling them using the code below in public class Server. Server itself is an additional class of my program that's why it doesn't contain "public static void main(String[] args)".

In this program I'm not allowed to use the static function.

public class Jobs {

    private String jobNumber = "";
    private String jobTime = "";

    public Jobs() {

    }

    public void setJobNumber(String number) {
        jobNumber = number;
    }

    public String getJobNumber() {
        return jobNumber;
    }

    public void setJobTime(String time) {
        jobTime = time;
    }

    public String getJobTime() {
        return jobTime;
    }
}

public class JobsManager {

    private LinkedList<Jobs> jobsList = new LinkedList<Jobs>();

    public JobsManager() {

    }

    public void addJob(Jobs job) {
        jobsList.add(job);
    }
    public void showAll() {
        for (Jobs j : jobsList) {
            System.out.println("Job Number: " + j.getJobNumber() + ", Processing Time: " + j.getJobTime());
        }
    }
}

public class ServerSystem extends Thread {

    Jobs jobStore = new Jobs();
    JobsManager jobManager = new JobsManager();

    @Override
    public void run() {

        Nodes nodeClass = new Nodes();
        NodesManager nodeManager = new NodesManager();
        
        try {

            boolean repeat = true;

            while (repeat) {
                byte[] packetData = new byte[1024];
                DatagramPacket packet = new DatagramPacket(packetData, packetData.length);
                socket.receive(packet);
                String message = new String(packetData);

                String[] elements = message.trim().split(",");
                switch (elements[0]) {
                    case "JOB":
                        // this is a job
                        System.out.println("---> got a JOB instruction");
                        String jobTime = elements[1];
                        String jobNumber = elements[2];
                        if (Integer.parseInt(jobNumber) >= 11) {
                            System.out.println("-----> Maximum job numbers reached");
                            break;
                        }
                        System.out.println("-----> Job Time = " + jobTime + ", Job Number = " + jobNumber);

                        jobStore.setJobNumber(jobNumber);
                        jobStore.setJobTime(jobTime);
                        jobManager.addJob(jobStore);
                        break;
            }
            System.out.println("Blocking incoming traffic...");
        } catch (Exception error) {

        }
    }
}

MSinghD
  • 3
  • 3
  • Can you share the part of your code that adds a job to the list? – Mureinik Mar 24 '23 at 19:37
  • Code works as expected, pretty sure the error in the code is in the code you omitted. I am guessing you are only using a single `Jobs jobStore = new Jobs();` and changing the values and adding again instead of making another object to add to the list -- `Jobs jobStore2 = new Jobs();` – Nexevis Mar 24 '23 at 19:42

1 Answers1

0

You are only ever creating a single Jobs object with Jobs jobStore = new Jobs();.

When you change the value of this jobStore object and add it to the list, you are modifying the same object you already added to the list and adding it again. This means that only values from the last add will be in the list.

To fix this you simply need to create a new Jobs object on every iteration AKA move Jobs jobStore = new Jobs(); into the loop. Here is what the case will look like with the added line:

case "JOB":
    // this is a job
    System.out.println("---> got a JOB instruction");
    String jobTime = elements[1];
    String jobNumber = elements[2];
    if (Integer.parseInt(jobNumber) >= 11) {
        System.out.println("-----> Maximum job numbers reached");
        break;
    }
    System.out.println("-----> Job Time = " + jobTime + ", Job Number = " + jobNumber);

    Jobs jobStore = new Jobs(); //Add this line here, and remove it from before the loop

    jobStore.setJobNumber(jobNumber);
    jobStore.setJobTime(jobTime);
    jobManager.addJob(jobStore);
    break;
Nexevis
  • 4,647
  • 3
  • 13
  • 22
  • 1
    That worked thank you very much for the explanation, it worked! Been on it for more than one hour, knew if was going to be something simple. – MSinghD Mar 24 '23 at 20:24