Fix Java Broken Code
Copy and paste code below to a single file called PetSystem.java. Fix all compilation and logic errors.
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class PetSystem { private Map<String, Pet> petMap = new HashMap<>(); private Scanner scanner = new Scanner(); private String menu = """ \nMenu 1. Add pet 2. List all pets 3. Exit Please make your selection (1-3): """; public void run() { while(true) { System.out.print(menu); String selection = scanner.nextLine(); if(selection == "3"){ break; } else if(selection == "1") { addPet(); } else if(selection == "2") { listAllPets(); } } private void listAllPets() { for(Pet pet: petMap) { System.out.println("%s - %s - %s".formatted(pet.getSpecies, pet.getInfo, pet.getAge)); } } private void addPet { System.out.println("\nENTER NEW PET"); System.out.print("Pet Species: "); String species = scanner.nextLine(); System.out.print("Pet Name: "); String name = scanner.nextLine(); System.out.print("Pet age: "); String sAge = scanner.nextLine(); int age = Integer.parseInt(sAge); Pet pet = new Pet(age, name, species); petMap.add(pet.getName(),pet); } public static void main(String[] args) { PetSystem petSystem = new PetSystem(); petSystem.run(); } } class Pet { private String name; private int age; private String species; public Pet(String name, int age, String species) { this.name = name; this.age = age; this.species = species; } public String getName() { return species; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSpecies() { return name; } public void setSpecies(String species) { this.species = species; } }
Expected Result
Menu 1. Add pet 2. List all pets 3. Exit Please make your selection (1-3):1 ENTER NEW PET Pet Species: dog Pet Name: Stitch Pet age: 5 Menu 1. Add pet 2. List all pets 3. Exit Please make your selection (1-3):2 dog - Stitch - 5 Menu 1. Add pet 2. List all pets 3. Exit Please make your selection (1-3):1 ENTER NEW PET Pet Species: dog Pet Name: Maple Pet age: 7 Menu 1. Add pet 2. List all pets 3. Exit Please make your selection (1-3):2 dog - Stitch - 5 dog - Maple - 7 Menu 1. Add pet 2. List all pets 3. Exit Please make your selection (1-3):1 ENTER NEW PET Pet Species: tiger Pet Name: Tigger Pet age: 2 Menu 1. Add pet 2. List all pets 3. Exit Please make your selection (1-3):2 dog - Stitch - 5 tiger - Tigger - 2 dog - Maple - 7 Menu 1. Add pet 2. List all pets 3. Exit Please make your selection (1-3):3