| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import java.util.Scanner;
- import java.util.InputMismatchException;
- import java.util.Arrays;
- public class MainApplication {
- private static Pet pet;
- private static Pet[] petList;
- public MainApplication(){
-
- }
-
- public static void main(String[] args){
- process();
- }
-
- public static void process(){
- int numPets;
- System.out.println("How many pets do you have?");
- numPets = getNumberPets();
- System.out.println("You have " + numPets + " pet(s)!");
- petList = new Pet[numPets];
- Scanner input = new Scanner(System.in);
-
- if (numPets > 0){
- for (int i = 0; i < numPets; i++){
- System.out.println("What type is pet " + (i+1) + "?");
- String typeCheck = input.nextLine();
- System.out.println("What is it's name?");
- String name = input.nextLine();
- if (typeCheck.toLowerCase().equals("dog")){
- petList[i] = new Dog(name);
- } else if (typeCheck.toLowerCase().equals("cat")){
- petList[i] = new Cat(name);
- } else {
- petList[i] = new Chicken(name);
- }
- }
- }
-
- if (numPets > 0){
- StringBuilder list = new StringBuilder();
- for (Pet element: petList){
- list.append(element.getName() + " ");
- }
- System.out.println("Your pets are:\n " + list.toString());
- for (Pet element: petList){
- System.out.println(element.getName() + " says " + element.speak());
- }
- }
- }
-
- public static int getNumberPets(){
- int numberPets = 0;
- Scanner input = new Scanner(System.in);
- boolean error = true;
- do{
- try{
- numberPets = input.nextInt();
- error = false;
- } catch (InputMismatchException e){
- input.next();
- System.out.println("Error! Please type a number");
- }
- } while (error);
- if (numberPets == 0){
- return 0;
- }
- return numberPets;
- }
-
- public String setPetName(){
- Scanner input = new Scanner(System.in);
- String petName = input.nextLine();
- return petName;
- }
-
- }
|