Question

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

Was it helpful?

Solution 2

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-

  1. The user info (Build User)
  2. Ask if the user has a pet. If no pet, update the user profile to state no pets.
  3. If has a pet, ask if they have a dog. If they have, ask how many. Read the info on each dog. Build user's dog profile. If no dog, update user profile to indicate no dogs.
  4. If has a pet, ask if they have a cat. If they have, ask how many. Read the info on each cat. Build user's cat profile like you did for dog in step 3 so on..

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")); 

OTHER TIPS

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");

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 !");
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top