The first class, Animal, stores basic data about an animal. The second class Pet, stores additional data about an animal which is a pet. The third class ZooAnimal, stores additional data about an animal which is kept in a zoo. The fourth class ProgThree has a main method which reads data about animals, pets and zoo animals and creates a report about animals, pets and zoo animals.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ProgThree {
public static void main(String[] args) {
File inputFile = new File("data/animal.txt");
if (!inputFile.exists()) {
System.err.println("Error: The file 'animal.txt' does not exist.");
return;
}
try {
Scanner scanner = new Scanner(inputFile);
PrintWriter writer = new PrintWriter("animalout.txt");
int animalsCount = 0, petsCount = 0, zooAnimalsCount = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] data = line.split(" ");
if (data.length == 3) { // Animal
int id = Integer.parseInt(data[0]);
String type = data[1];
double weight = Double.parseDouble(data[2]);
if (isValidAnimal(id, type, weight)) {
Animal animal = new Animal(id, type, weight);
writer.println(animal);
animalsCount++;
}
} else if (data.length == 5) { // Pet or ZooAnimal
int id = Integer.parseInt(data[0]);
String type = data[1];
double weight = Double.parseDouble(data[2]);
if (id >= 3000 && id <= 7999 && isValidPet(id, type, weight, data[3], data[4])) {
Pet pet = new Pet(id, type, weight, data[3], data[4]);
writer.println(pet);
petsCount++;
} else if (id >= 8000 && id <= 9999) {
if (isValidZooAnimal(id, type, weight, data[3], data[4])) {
ZooAnimal zooAnimal = new ZooAnimal(id, type, weight, Integer.parseInt(data[3]), data[4]);
writer.println(zooAnimal);
zooAnimalsCount++;
}
}
}
}
writer.println("Total Animals: " + animalsCount);
writer.println("Total Pets: " + petsCount);
writer.println("Total Zoo Animals: " + zooAnimalsCount);
scanner.close();
writer.close();
} catch (FileNotFoundException e) {
System.err.println("Error: The file 'animal.txt' was not found.");
e.printStackTrace();
}
}
private static boolean isValidAnimal(int id, String type, double weight) {
return id >= 1000 && id <= 2999 && type.length() >= 3 && weight > 0;
}
private static boolean isValidPet(int id, String type, double weight, String name, String owner) {
return id >= 3000 && id <= 7999 && type.length() >= 3 && weight > 0 && name.length() >= 3 && owner.length() >= 3;
}
private static boolean isValidZooAnimal(int id, String type, double weight, String cageNumber, String trainer) {
try {
int cageNum = Integer.parseInt(cageNumber);
return id >= 8000 && id <= 9999 && type.length() >= 3 && weight > 0 && cageNum > 0 && trainer.length() >= 3;
} catch (NumberFormatException e) {
return false;
}
}
}
class Animal {
private int id;
private String type;
private double weight;
public Animal(int id, String type, double weight) {
this.id = id;
this.type = type;
this.weight = weight;
}
public int getId() {
return id;
}
public String getType() {
return type;
}
public double getWeight() {
return weight;
}
@Override
public String toString() {
return "Animal{" +
"id=" + id +
", type='" + type + '\'' +
", weight=" + weight +
'}';
}
}
class Pet extends Animal {
private String name;
private String owner;
public Pet(int id, String type, double weight, String name, String owner) {
super(id, type, weight);
this.name = name;
this.owner = owner;
}
@Override
public String toString() {
return "Pet{" +
"id=" + getId() +
", type='" + getType() + '\'' +
", weight=" + getWeight() +
", name='" + name + '\'' +
", owner='" + owner + '\'' +
'}';
}
}
class ZooAnimal extends Animal {
private int cageNumber;
private String trainer;
public ZooAnimal(int id, String type, double weight, int cageNumber, String trainer) {
super(id, type, weight);
this.cageNumber = cageNumber;
this.trainer = trainer;
}
@Override
public String toString() {
return "ZooAnimal{" +
"id=" + getId() +
", type='" + getType() + '\'' +
", weight=" + getWeight() +
", cageNumber=" + cageNumber +
", trainer='" + trainer + '\'' +
'}';
}
}
So according to the one error I have in the code when testing it, it seems I am on the final step and it seems I need to debug the final error.
Error: The file 'animal.txt' does not exist.
I need insight on how to fix that error and make the code work. Please debug the error only and donât change the code entirely. Ensure the code works and test that it works after making your changes.