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.
-
1Copy the code you used so that we could help you in where the changes could be done.Mohan Raj– Mohan Raj2015-05-04 04:19:07 +00:00Commented May 4, 2015 at 4:19
-
You might be using inheritance, polymorphism etcHJK– HJK2015-05-04 04:19:22 +00:00Commented May 4, 2015 at 4:19
-
post what you have done so far.Anirudh Sharma– Anirudh Sharma2015-05-04 04:23:42 +00:00Commented May 4, 2015 at 4:23
3 Answers
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);
}
}
Comments
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.
Comments
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;
}