1

this is a snippet of a bigger project. The project is designed to be a customer manager of customers + a job manager of money transfers. Before I can continue, I have a "small" problem I do not understand. I wonder, why

System.out.println(myJobOrganizer.jobNumber); // in the Main.java

outputs 0, allthough I have added two jobs to the jobList. When printing the jobList itself, it prints out

[[[customer1;adress1;accountBalance1];[customer2;adress2;accountBalance2];moneyToTransfer];[[customer3;adress3;accountBalance3];[customer4;address4;accountBalance4];moneyToTransfer2]]

jobOrganizer.java

import java.util.ArrayList;
import java.util.List;

public class jobOrganizer {

    private static final jobOrganizer myJobOrganizer = new jobOrganizer();

    public static jobOrganizer getMyJobOrganizer() {
        return myJobOrganizer;
    }

    private List<job> jobList = new ArrayList<job>();

    public int jobNumber = jobList.size();

    public void add_job(job newJob) {
        this.jobList.add(newJob);
    }

    public Iterable<job> all_jobs() {
        return this.jobList;
    }

    public static jobOrganizer getInstance() {
        return myJobOrganizer;
    }
}

job.java

    public class job implements Runnable {
    private customer kunde1;
    private customer kunde2;
    private Double transfer;

    public job(customer kunde1, customer kunde2, Double transfer) {
        this.kunde1 = kunde1;
        this.kunde2 = kunde2;
        this.transfer = transfer;
    }

    @Override
    public String toString() {
        return "[" + kunde1 + "; " + kunde2 + "; " + transfer + "]";
    }

    public void run() {
        System.out.println("Starting transfer");
        Double geber = this.kunde1.getAccountBalance();
        Double nehmer = this.kunde2.getAccountBalance();
        Double geberNeu = geber - this.transfer;
        this.kunde1.setAccountBalance(geberNeu);
        Double nehmerNeu = nehmer + this.transfer;
        this.kunde2.setAccountBalance(nehmerNeu);
        System.out.println("Transfer done");
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        customerOrganizer myCustomerOrganizer = new customerOrganizer();
        jobOrganizer myJobOrganizer = new jobOrganizer();

        customer mueller = new customer("Tim Mueller", "Strasse 1", 1077.00);
        customer john = new customer("John Doe", "Strasse 2", 503.00);
        customer meier = new customer("John Meier", "Strasse 3", 8500.50);
        customer wurst = new customer("Hans Wurst", "Strasse 4", 1000.00);

        myCustomerOrganizer.add_customer(mueller);
        myCustomerOrganizer.add_customer(john);
        myCustomerOrganizer.add_customer(meier);
        myCustomerOrganizer.add_customer(wurst);

        job transfer1 = new job(meier, wurst, 500.50);
        job transfer2 = new job(mueller, john, 77.00);

        myJobOrganizer.add_job(transfer1);
        myJobOrganizer.add_job(transfer2);

        System.out.println(myJobOrganizer.jobNumber);
        System.out.println(myJobOrganizer.all_jobs().toString());
    }
}
SamHoque
  • 2,978
  • 2
  • 13
  • 43
Shushiro
  • 577
  • 1
  • 9
  • 32
  • 1
    `public int jobNumber = jobList.size();` passes *current* size of `jobList` which is 0 at current moment. It will *not* get automatically updated. – Pshemo Oct 27 '18 at 09:13
  • 1
    When `public int jobNumber = jobList.size();` is initialized with the value of `jobList.size()` the size is `0`. The value of `jobNumber` don't change anymore. – LuCio Oct 27 '18 at 09:13
  • 1
    BTW you shouldn't make your fields `public` and let others access it. Instead you should use getters and setters (more info: [Why use getters and setters/accessors?](https://stackoverflow.com/q/1568091)). In this case your getter wouldn't even require field, it can simply be `int getJobNumber(){ return jobList.size();}` and use it like `myJobOrganizer.getJobNumber()` instead. (You can also think about better name for it since *number* usually represents some *identifier*, so maybe better word would be *amount*) – Pshemo Oct 27 '18 at 09:14
  • oh my, thank you all so much. what a stupid mistake -.-" thanks @Pshemo, I will definitely put intrest into reading about that! – Shushiro Oct 27 '18 at 09:21

2 Answers2

1

public int jobNumber = jobList.size(); isn't getting updated so do it like this, this will return the current jobList and not store an instance of it like your field did.

public int jobNumber(){
  return jobList.size();
}
SamHoque
  • 2,978
  • 2
  • 13
  • 43
0

it is because the below code executed while object creation but not after that and you are not updating the value later.

public int jobNumber = jobList.size();
greengreyblue
  • 389
  • 3
  • 12