Rick
Rick

Reputation: 21

Creating specific objects based on user input

So I have 3 subclasses (Cat, Dog and Fish) and I have to ask the user how many pets they have (n), the type of pet and all the information for that pet (name and age are part of the super class but each subclass has two unique fields). My question is how would be the best way to separate each "pet" and create a class object for it. I am obviously a little stuck and any help is appreciated.

Upvotes: 1

Views: 953

Answers (3)

user2418306
user2418306

Reputation: 2382

Take a look at decorator pattern.

abstract class Pet {}
abstract class PetDecorator extends Pet {
    void initialize() {
        // read name and age from console
    }
}
class CatDecorator extends PetDecorator {
    @Override
    void initialize() {
        super.initialize();
        // read cat specific fields
    }
}
// other classes omited for brevity
// inside Main
List<Pet> readInfoAboutPets(int numberOfPets) {
    List<Pet> pets = new ArrayList<>(numberOfPets);
    for (int i = 0; i < numberOfPets; i++) {
        // read pet type from console
        switch (petType) {
            case "cat":
                PetDecorator petDecorator = new CatDecorator();
                petDecorator.initialize();
                pets.add(petDecorator);
                break;
            // case rest
        }
    }
    return pets;
}

Upvotes: 0

user2575725
user2575725

Reputation:

You may go for polymorphism with factory pattern:

public abstract class Pet {
  private String name;
  private int age;

  protected Pet(String name, int age) {
    setName(name);
    setAge(age);
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getAge() {
    return age;
  }
}

public class Dog extends Pet {
  public Dog(String name, int age, param3, param4) {
    super(name, age);
  }
}

public class Cat extends Pet {
  public Cat(String name, int age, param3, param4) {
    super(name, age);
  }
}

public class Fish extends Pet {
  public Fish(String name, int age, param3, param4) {
    super(name, age);
  }
}

public class PetFactory {
  public Pet newPet(String petType, String name, int age, param3, param4) throws IllegalArgumentException {
    if ("Dog".equalsIgnoreCase(petType))
      return new Dog(name, age, param3, param4);
    if ("Cat".equalsIgnoreCase(petType))
      return new Cat(name, age, param3, param4);
    if ("Fish".equalsIgnoreCase(petType))
      return new Fish(name, age, param3, param4);
    throw new IllegalArgumentException("Unknown pet: " + petType);
  }
}

Upvotes: 1

Luo
Luo

Reputation: 445

If you have a frame UI, you can create a radiobutton group to choose the type of pet and text boxes for pet parameters (of course, you need to check user input and give feedback). It would be better to provide your data restrictions on labels. If you use console, for pet choosing provide user an opportunity to choose between "cat", "dog", "fish", etc. Also provide your restrictions about other parameters. Control his input and give feedback. There plenty ways of realizing it, specify your vision.

Upvotes: 0

Related Questions