public class GerenciadorAnimais {
    private static final String ARQUIVO = "animais.txt";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int opcao;

        do {
            System.out.println("\nMenu:");
            System.out.println("1. Criar arquivo");
            System.out.println("2. Inserir dados");
            System.out.println("3. Excluir dado");
            System.out.println("4. Pesquisar");
            System.out.println("5. Exibir lista");
            System.out.println("6. Sair");
            System.out.print("Escolha uma opção: ");
            opcao = scanner.nextInt();
            scanner.nextLine();

            switch (opcao) {
                case 1 -> criarArquivo();
                case 2 -> inserirDados(scanner);
                case 3 -> excluirDado(scanner);
                case 4 -> pesquisar(scanner);
                case 5 -> exibirLista();
                case 6 -> System.out.println("Saindo...");
                default -> System.out.println("Opção inválida!");
            }
        } while (opcao != 6);

        scanner.close();
    }

    private static void criarArquivo() {
        try {
            File file = new File(ARQUIVO);
            if (file.createNewFile()) {
                System.out.println("Arquivo criado com sucesso!");
            } else {
                System.out.println("O arquivo já existe.");
            }
        } catch (IOException e) {
            System.out.println("Erro ao criar arquivo: " + e.getMessage());
        }
    }

    private static void inserirDados(Scanner scanner) {
        try (FileWriter fw = new FileWriter(ARQUIVO, true);
             BufferedWriter bw = new BufferedWriter(fw)) {
            System.out.print("Digite o nome do animal: ");
            String nome = scanner.nextLine();
            System.out.print("Digite a idade do animal: ");
            int idade = scanner.nextInt();
            scanner.nextLine();
            System.out.print("Digite a espécie (Cachorro, Gato, Passaro, Peixe): ");
            String especie = scanner.nextLine();

            Animal animal = switch (especie.toLowerCase()) {
                case "cachorro" -> {
                    System.out.print("Digite a raça: ");
                    String raca = scanner.nextLine();
                    yield new Cachorro(nome, idade, raca);
                }
                case "gato" -> {
                    System.out.print("É doméstico? (true/false): ");
                    boolean domestico = scanner.nextBoolean();
                    yield new Gato(nome, idade, domestico);
                }
                case "passaro" -> {
                    System.out.print("Digite a envergadura (em metros): ");
                    double envergadura = scanner.nextDouble();
                    yield new Passaro(nome, idade, envergadura);
                }
                case "peixe" -> {
                    System.out.print("Digite o tipo de água (Doce/Salgada): ");
                    String tipoAgua = scanner.nextLine();
                    yield new Peixe(nome, idade, tipoAgua);
                }
                default -> null;
            };

            if (animal != null) {
                bw.write(animal.getDescricao());
                bw.newLine();
                System.out.println("Animal cadastrado com sucesso!");
            } else {
                System.out.println("Espécie inválida.");
            }
        } catch (IOException e) {
            System.out.println("Erro ao escrever no arquivo: " + e.getMessage());
        }
    }
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: