A pet can either be a cat or a dog. Each pet needs to have a name, an owner's name, color, doctor's name and breed. All of the pets can cry, eat and sleep. Without using the switch method, I tried to use setter and getter function with scanner. but i dont know how to identify if the user inputs dog then the user will input all about the dog then else the cat.
is this possible?
package petexercise;
import java.util.Scanner;
public class PetCatDog {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PetCat cat = new PetCat();
PetDog dog = new PetDog();
System.out.print("Enter Dog or Cat Word:");
String pet = cat.nextLine();
}
}
the other subclasses are made for the setter and getters
I think if you don't want to use switch you can use map. First step identify what user inputs with simple string, second step create your pet.
Map<String, PetFactory> factories = new HashMap<>();
factories.put("dog", new DogFactory())
...
String petType = scanner.nextLine();
factories.get(petType).createPet("name", "color");
So you have -
User - a class to represent a user. Has name, pet info, etc. User can have many or no pets
Pet - A pet that represents some type of animals. Have some common attributes like name, age..some common actions like eat.
Dog - Extends pet has some attributes like name, age, color, breed, some actions specifially for dog like barks, fetches balls
Cat - Extends pet has some attributes like name, age, color, breed, some actions like being useless by sleeping twenty times a day
Ask the user for-
UPDATE:
For you to know if the user entered either a dog or a cat, you can have something like-
Scanner scan = new Scanner(System.in);
String response;
do{
System.out.print("Do you have a pet ? (Y/N): ");
response = scan.nextLine();
} while(!response.equalsIgnoreCase("Y") && !response.equalsIgnoreCase("N"));
if(response.equalsIgnoreCase("N")){
System.exit(0);
}
do{
System.out.print("Cat or a dog ? (C/D): ");
response = scan.nextLine();
} while(!response.equalsIgnoreCase("C") && !response.equalsIgnoreCase("D"));
The properties mentioned where not animal specific, so one could have a single Pet class with properties like name, color, breed and Animal type:
enum Animal {
CAT(4),
DOG(4);
public final int legs;
private Animal(int legs) {
this.legs = legs;
}
}
public static Animal what(String whatAnimal) {
return Animal.valueOf(whatAnimal.toUpperCase());
};
Animal animal = what("dog");
I got it ! thanks for the help everyone
package dogandcat;
import java.util.Scanner;
public class CatDogSystem {
public static void main(String[] args) {
String animal;
Scanner scan = new Scanner(System.in);
Cat cat = new Cat();
Dog dog = new Dog();
System.out.print("Enter Dog or Cat Only: ");
animal = scan.nextLine();
if(animal.equalsIgnoreCase("Cat")) {
System.out.print("Enter cat's name: ");
cat.setCatName(scan.nextLine());
System.out.print("Enter owner's name: ");
cat.setCatOwnersName(scan.nextLine());
System.out.print("Enter cat's color: ");
cat.setCatColor(scan.nextLine());
System.out.print("Enter doctor's name: ");
cat.setCatDoctorsName(scan.nextLine());
System.out.print("Enter cat's breed: ");
cat.setCatBreed(scan.nextLine());
System.out.println("");
System.out.println("Cat's Details");
System.out.println(cat.getCatName());
System.out.println(cat.getCatOwnersName());
System.out.println(cat.getCatColor());
System.out.println(cat.getCatDoctorsName());
System.out.println(cat.getCatBreed());
} else if(animal.equalsIgnoreCase("Dog")) {
System.out.print("Enter Dog's name: ");
dog.setDogName(scan.nextLine());
System.out.print("Enter owner's name: ");
dog.setDogOwnersName(scan.nextLine());
System.out.print("Enter Dog's color: ");
dog.setDogColor(scan.nextLine());
System.out.print("Enter doctor's name: ");
dog.setDogDoctorsName(scan.nextLine());
System.out.print("Enter Dog's breed: ");
dog.setDogBreed(scan.nextLine());
System.out.println("");
System.out.println("Dog's Details");
System.out.println(dog.getDogName());
System.out.println(dog.getDogOwnersName());
System.out.println(dog.getDogColor());
System.out.println(dog.getDogDoctorsName());
System.out.println(dog.getDogBreed());
} else System.out.println("Invalid Input !");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.