3

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

4

4 回答 4

4

我认为如果您不想使用开关,您可以使用地图。第一步用简单的字符串识别用户输入的内容,第二步创建你的宠物。

Map<String, PetFactory> factories = new HashMap<>();
factories.put("dog", new DogFactory())
...
String petType = scanner.nextLine();
factories.get(petType).createPet("name", "color");
于 2013-07-31T10:50:42.583 回答
2

所以你有了 -

User - 代表用户的类。有名字、宠物信息等。用户可以有很多宠物或没有宠物

宠物 - 代表某种动物的宠物。有一些共同的属性,例如姓名、年龄……一些共同的动作,例如吃。

狗 - 扩展宠物具有一些属性,如名称、年龄、颜色、品种、一些专门针对狗的动作,如吠叫、取球

猫 - 扩展宠物有一些属性,比如名字、年龄、颜色、品种,一些行为比如一天睡二十次没用

询问用户 -

  1. 用户信息(构建用户)
  2. 询问用户是否有宠物。如果没有宠物,请更新用户配置文件以声明没有宠物。
  3. 如果有宠物,询问他们是否有狗。如果有,请问有多少。阅读每只狗的信息。建立用户的狗档案。如果没有狗,则更新用户配置文件以指示没有狗。
  4. 如果有宠物,请询问他们是否有猫。如果有,请问有多少。阅读每只猫的信息。像您在第 3 步中为狗所做的那样建立用户的猫资料,依此类推..

更新:

为了让你知道用户输入的是狗还是猫,你可以有类似的东西 -

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")); 
于 2013-07-31T10:51:25.050 回答
1

我知道了 !谢谢大家的帮助

包dogandcat;

导入 java.util.Scanner;

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

}

于 2013-08-02T09:10:04.170 回答
1

提到的属性不是特定于动物的,所以可以有一个 Pet 类,其属性包括名称、颜色、品种和动物类型:

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");
于 2013-07-31T10:42:38.213 回答