import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
public class ArquivoAnimais {
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, Tartaruga): ");
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("Digite a cor: ");
String cor = scanner.nextLine();
yield new Gato(nome, idade, cor);
}
case "passaro" -> {
System.out.print("Digite a espécie: ");
String especiePassaro = scanner.nextLine();
yield new Passaro(nome, idade, especiePassaro);
}
case "tartaruga" -> {
System.out.print("Digite o habitat: ");
String habitat = scanner.nextLine();
yield new Tartaruga(nome, idade, habitat);
}
default -> null;
};
if (animal != null) {
bw.write(animal.getInformacoes());
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());
}
}
private static void excluirDado(Scanner scanner) {
}
private static void pesquisar(Scanner scanner) {
System.out.print("Digite o nome do animal que você deseja pesquisar: ");
String nomePesquisar = scanner.nextLine();
try (BufferedReader br = new BufferedReader(new FileReader(ARQUIVO))) {
String linha;
boolean encontrado = false;
while ((linha = br.readLine()) != null) {
if (linha.toLowerCase().contains(nomePesquisar.toLowerCase())) {
System.out.println("Animal encontrado: " + linha);
encontrado = true;
break;
}
}
if (!encontrado) {
System.out.println("Desculpe, não encontramos nenhum animal com esse nome.");
}
} catch (IOException e) {
System.out.println("Ocorreu um erro ao tentar pesquisar: " + e.getMessage());
}
}
private static void exibirLista() {
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: