Java Person and Pet Management Code
Java Person and Pet Management
This Java code provides functionality to manage a list of people and their pets. It includes methods to create, read, update, and delete person records.
Create Person
The createPerson()
method allows you to add a new person to the list. It prompts for the person’s name, age, and gender, and allows adding pets to the person’s record.
public static void createPerson() {
String name = WordNotEmpty("Enter name:");
if (getPersonByName(name) != null) {
System.out.println("A person with that name already exists, please try again");
} else {
int age = intFromTo(0, 150, "Enter age between 0 - 120: ");
char genre = letterStringToChar('F', 'M', "Enter genre (F - Female, M - Male): ");
ArrayList<Pet> pets = new ArrayList<>();
System.out.println("Do you want to add a Pet? (Y/N)");
Scanner teclado = new Scanner(System.in);
while (teclado.nextLine().equalsIgnoreCase("y")) {
String petName = WordNotEmpty("Enter name for your Pet: ");
int petAge = intFromTo(0, 100, "Enter age between 0 - 100: ");
pets.add(new Pet(petName, petAge));
System.out.println("Do you want to add a Pet? (Y/N)");
}
people.add(new Person(name, age, genre, pets));
FileController.writePeople();
}
}
Read People
The readPeople()
method displays the information of all people in the list, including their pets.
public static void readPeople() {
if (!people.isEmpty()) {
for (Person person : people) {
System.out.println(person);
for (Pet pet : person.getPets()) {
System.out.println(pet);
}
}
} else {
System.out.println("ArrayList People is empty");
}
}
Update Person
The update()
method allows you to modify the age, gender, or add a pet to an existing person’s record.
public static void update() {
if (!people.isEmpty()) {
readPeople();
String nameToUpdate = WordNotEmpty("Enter name: ");
if (getPersonByName(nameToUpdate) == null) {
System.out.println("There is no person with that name, please try again");
} else {
Person personToUpdate = getPersonByName(nameToUpdate);
boolean exitUpdate = false;
do {
Scanner sc = new Scanner(System.in);
System.out.println("1 - Age");
System.out.println("2 - Genre");
System.out.println("3 - Add one Pet");
System.out.println("0 - Exit menu UPDATE");
switch (sc.nextInt()) {
case 1:
int ageUpdate = intFromTo(0, 150, "Enter age between 0-150: ");
personToUpdate.setAge(ageUpdate);
FileController.writePeople();
break;
case 2:
sc.nextLine();
char genreUpdate = letterStringToChar('F', 'M', "Enter genre: ");
personToUpdate.setGenre(genreUpdate);
FileController.writePeople();
break;
case 3:
sc.nextLine();
String petName = WordNotEmpty("Enter name for your Pet: ");
int petAge = intFromTo(0, 100, "Enter age between 0-100: ");
Pet animal = new Pet(petName, petAge);
personToUpdate.getPets().add(animal);
FileController.writePeople();
case 0:
exitUpdate = true;
break;
default:
System.out.println("Wrong option");
}
} while (!exitUpdate);
}
} else {
System.out.println("This person doesn't exist");
}
}
Delete Person
The deletePerson()
method removes a person from the list based on their name.
public static void deletePerson() {
if (!people.isEmpty()) {
readPeople();
String nameToDelete = WordNotEmpty("Enter name: ");
if (getPersonByName(nameToDelete) == null) {
System.out.println("No existe una persona con ese nombre, vuelva a intentarlo");
} else {
Person personToDelete = getPersonByName(nameToDelete);
if (personToDelete != null) {
people.remove(personToDelete);
System.out.println(nameToDelete + " has been removed");
FileController.writePeople();
} else {
System.out.println("This person doesn't exist");
}
}
} else {
System.out.println("People array is empty");
}
}
Get Person by Name
The getPersonByName()
method retrieves a person from the list by their name.
public static Person getPersonByName(String name) {
Person ToReturn = null;
for (int i = 0; i < people.size() && ToReturn == null; i++) {
if (people.get(i).getName().equalsIgnoreCase(name)) {
ToReturn = people.get(i);
}
}
return ToReturn;
}