Nemexia Nemexia - 3 months ago 22
Java Question

class design for this game(java)

I'm a programming fan and want to make a Java program for automation of my City game (real game with small toy car that i play with my brother). It's 100% not homework or school problem.

I have made a

Citizen
class and a
Jobs
class. Each job has got a name, base income, income multiplier (for job ranks).

I want to make objects for each job, and connect each citizen to it but i have these problems:


  1. If two people have got the same job, two job object is created (i want to be just one);

  2. I don't know whether i should make classes for each job separately (sub class) or create one new job object if a player has a new job when i'm initializing a new citizen.


Answer

if two people have got the same job, two job object is created (i want to be just one)

Two main options:

  1. Make each job instance a Singleton (if you don't know what a singleton is, just google it or search here on SO).
  2. Make a singleton Job Manager class that holds a Set (or Map or List, but you probably want a Set), pairing job names to an instance of that job's class; which you can then simply get a reference to, instead of creating a new one.

i don't know whether i should make classes for each job separately (sub class) or create one new job object if a player has a new job when i'm initializing a new citizen.

You probably shouldn't. Instead, make the things common to all jobs an abstract parent class, and then implement each job class as extensions to that. If, however you just need the variables within the Job class to contain different values for each individual job, you want multiple instances of your single Job class, each with their own set of values. For example:

class Job{
  public String name;
  public float salary;
  public float multiplier;

  public Job(String name, float salary, float multiplier){
    this.name=name;
    this.salary=salary;
    this.multiplier=multiplier;
  }
}

//On Main
Job banker = new Job("Banker", 9000, 1);
Job seniorBanker = new Job("Senior Banker", 9000, 1.2f);
Job plumber = new Job("Plumber", 800, 1);

//...
//Assuming citizen class contains Job variable: public Job job;
Citizen jack = new Citizen(); 
jack.job = banker;
Citizen mario = new Citizen(); 
mario.job = plumber;
Citizen luigi = new Citizen(); 
luigi.job = plumber; //Same instance of 'plumber' as mario's...
//If you change the value of the plumber's job salary, and then do...
  //system.out.println(mario.job.salary);
  //system.out.println(luigi.job.salary);
//...both will be changed.

Note: As you can see above, your current design (from what can be inferred) doesn't reuse data from previous job tiers, making the multiplier redundant and kinda pointless. A better design would be to have the ranks built into the job class as a Map, pairing tiers to specific names and multipliers.

Note of acknowledgement: This is basically going to turn the Job class into the equivalent of a Profession class, which is what garnulf suggests in his answer.

Comments