dimanche 31 mai 2020

Generate different object with same attributes

I am building a project. One of the requirements is to generate the object with the same attributes but randomly different values. I tried to use deep-copy, but not sure if it is conceptually correct.

  1. So, for example, I have a Person class, inherited from the abstract class Character.
  2. And there is a ScenarioGenerator, which I'll put the getRandomPerson method to create the instances of the Person class.

Any help of advice is highly appreciated.

Here is part of my Person class:

public class Person extends Character {
private Random random;

private boolean pregnant;
private boolean isYou;

Person(int age, Profession profession ,Gender gender, BodyType bodyType, boolean isPregnant) {
    super(age, gender, bodyType);//pass the attributes to the super class called Character
}

Person (Person otherPerson) { //copy constructor

    this.age = otherPerson.getAge();
    this.gender = otherPerson.getGender();
    this.bodyType = otherPerson.getBodyType();
}
public Profession getProfession () { // One of the getters which generate random enum value
    //only adults have profession
    if (getAge()<=16 || getAge()>68) {
        return Profession.NONE;
    } else {
        return Profession.values()[new Random().nextInt(Profession.values().length)];
    }
}
// setters and getters
}

And the method of my ScernarioGenerator class:

public class ScenarioGenerator {
    public Person getRandomPerson() {
    //need age, gender, bodyType, profession, pregnancy
    Person people = new Person(person.getAge(), person.getProfession(), person.getGender(), person.getBodyType(), person.isPregnant());
    Person clone = new Person(people);
    return clone;
}



Aucun commentaire:

Enregistrer un commentaire