dimanche 26 avril 2020

User's input inside an if statement

Hello i am begginer with java here's my code:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.*;

public class Main extends MyZoo {

    public static void main(String[] args) {

        MyZoo a1 = new MyZoo();

        a1.SetAnimal_Name("Tiger");
        a1.SetCohabitation("Mammal");
        a1.SetGender("Female");
        a1.SetWeight(170);
        a1.SetMaximum_Age(15);
        a1.SetAnimal_Code("A01");

        MyZoo a2 = new MyZoo();

        a2.SetAnimal_Name("Taurus");
        a2.SetCohabitation("Mammal");
        a2.SetGender("Male");
        a2.SetWeight(1100);
        a2.SetMaximum_Age(22);
        a2.SetAnimal_Code("A02");

        MyZoo a3 = new MyZoo();

        a3.SetAnimal_Name("Red Deer");
        a3.SetCohabitation("Mammal");
        a3.SetGender("Male");
        a3.SetWeight(200);
        a3.SetMaximum_Age(27);
        a3.SetAnimal_Code("A03");

        MyZoo a4 = new MyZoo();

        a4.SetAnimal_Name("Giraffe");
        a4.SetCohabitation("Mammal");
        a4.SetGender("Female");
        a4.SetWeight(800);
        a4.SetMaximum_Age(30);
        a4.SetAnimal_Code("A04");

        List<MyZoo> List = new ArrayList<>();
        List.add(a1);
        List.add(a2);
        List.add(a3);
        List.add(a4);

        for (MyZoo s: List) { }

        System.out.println("Welcome to MyZoo application.\nPlease select a number from the menu\n" +
                "-------------------------------------\n " +
                "1.View all available zoo animals\n" +
                " 2.Add a new animal\n " +
                "3.Search for an animal by name\n " +
                "4.Search for an animal by code\n " +
                "5.Animal processing based on code\n " +
                "6.Delete animal based on code\n" +
                " 7.Exit from the application" +
                "\n-------------------------------------\n");

        Scanner input = new Scanner(System.in);

        int UserInput;

        while (true) {

            try {
                System.out.print("\nWrite Here: ");
                UserInput = input.nextInt();

            } catch (InputMismatchException e) {
                input.next();
                System.out.print("That’s not an integer!Please Try again!");
                continue;

            }

            if (UserInput < 1 || UserInput > 7) {

                System.out.println("The number doesn't exist in the menu!Please try again!");
                continue;
            }
            break;
        }

        if (UserInput == 1) {
            System.out.println("You choose " + UserInput);

            for (int i=0;i<List.size();i++){

                System.out.print("\n\nAnimal name:"+List.get(i).GetAnimal_Name());
                System.out.print("\nCohibiatation:"+List.get(i).GetCohabitation());
                System.out.print("\nGender:"+List.get(i).GetGender());
                System.out.print("\nWeight:"+List.get(i).GetWeight());
                System.out.print("\nMaximum Age:"+List.get(i).GetMaximum_Age());
                System.out.print("\nAnimal Code:"+List.get(i).GetAnimal_Code());

                UserInput = input.nextInt();
                }
        } else if (UserInput == 2) {
            System.out.println("You choose " + UserInput);
        } else if (UserInput == 3) {
            System.out.println("You choose " + UserInput);
        } else if (UserInput == 4) {
            System.out.println("You choose " + UserInput);
        } else if (UserInput == 5) {
            System.out.println("You choose " + UserInput);
        } else if (UserInput == 6) {
            System.out.println("You choose " + UserInput);
        } else if (UserInput == 7) {
            System.out.println("You choose " + UserInput + "\n\nThank you for running the programm!" +
                    "\nI hope you enjoyed!\nTime to exit, bye!");
            System.exit(0);
        }

    }

}

When the User writes 1 , it shows my list but not all the objects and the result is :

Welcome to MyZoo application.
Please select a number from the menu
-------------------------------------
 1.View all available zoo animals
 2.Add a new animal
 3.Search for an animal by name
 4.Search for an animal by code
 5.Animal processing based on code
 6.Delete animal based on code
 7.Exit from the application
-------------------------------------


Write Here: 1
You choose 1


Animal name:Tiger
Cohibiatation:Mammal
Gender:Female
Weight:170
Maximum Age:15
Animal Code:A01

But i want the result to be like this:

Welcome to MyZoo application.
Please select a number from the menu
-------------------------------------
 1.View all available zoo animals
 2.Add a new animal
 3.Search for an animal by name
 4.Search for an animal by code
 5.Animal processing based on code
 6.Delete animal based on code
 7.Exit from the application
-------------------------------------


Write Here: 1
You choose 1


Animal name:Tiger
Cohibiatation:Mammal
Gender:Female
Weight:170
Maximum Age:15
Animal Code:A01

Animal name:Taurus
Cohibiatation:Mammal
Gender:Male
Weight:1100
Maximum Age:22
Animal Code:A02

Animal name:Red Deer
Cohibiatation:Mammal
Gender:Male
Weight:200
Maximum Age:27
Animal Code:A03

Animal name:Giraffe
Cohibiatation:Mammal
Gender:Female
Weight:800
Maximum Age:30
Animal Code:A04

Write here:

with the input here i want to jump off from the if , how can i make that?

Aucun commentaire:

Enregistrer un commentaire