Animal hospital

Assignment 1 

Instructions:

Design and implement a Java program to create a system that will keep track of pets treated and boarded in an animal hospital. The program will maintain a current list of owner and pet records within a given veterinary practice. Owner data for the program includes an owner’s first and last name, an ID number, an e-mail address (valid emails contain an @ and .), a telephone number and an array of pets owned by the owner. The array of pets should contain the following: type of pet (dog, cat, bird, lizard, etc), name of pet, reason for visit (illness, checkup, shots, boarding, etc) (the maximum number of pets an individual can own should be five). Error checking of all data is required. The program should allow entry of owner information via the keyboard with the maximum number of owners being 30, but the list can contain less. Once the data is entered the program should then allow the user to retrieve the entire list of owners along with their pet’s information or an individual owner’s information via their ID number.

This assignment should use classes and methods.

Solution 

Hospital.java

package main.java;

import java.util.Scanner;

public class Hospital {

private final int ADD_OWNER = 1;

private final int ADD_PET = 2;

private final int DISPLAY = 3;

private final int DISPLAY_ALL = 1;

private final int DISPLAY_OWNER = 2;

private final int EXIT = 4;

private final int MAX_OWNERS = 30;

private boolean exit;

private boolean exitDisplay;

private Scanner scanner;

private Owner[] owners;

private int currNumberOfOwners;

private Hospital() {

scanner = new Scanner(System.in);

owners = new Owner[MAX_OWNERS];

}

public static void main(String[] args) {

Hospital hospital = new Hospital();

hospital.run();

}

private void run() {

while (!exit) {

resolveOption();

}

}

private void printMenu() {

System.out.println(ADD_OWNER + ” – Add owner”);

System.out.println(ADD_PET + ” – Add pet to owner (by id)”);

System.out.println(DISPLAY + ” – Display data”);

System.out.println(EXIT + ” – Exit”);

}

private void printDisplayMenu() {

System.out.println(DISPLAY_ALL + ” – Display all”);

System.out.println(DISPLAY_OWNER + ” – Display the chosen owner (by id)”);

System.out.println(EXIT + ” – Exit”);

}

private void resolveOption() {

printMenu();

int option = Integer.parseInt(prompt(“Please enter the option: “));

switch (option) {

case ADD_OWNER: {

addOwner();

break;

}

case ADD_PET: {

addPet();

break;

}

case DISPLAY: {

exitDisplay = false;

while (!exitDisplay) {

resolveDisplayOption();

}

break;

}

case EXIT: {

exit = true;

break;

}

default: {

System.out.println(“Can not resolve the option ” + option);

}

}

}

private void addOwner() {

// first name validation (not null, non-empty String)

String firstName = prompt(“Please enter the owner fist name: “);

if (“”.equals(firstName) || firstName == null) {

System.out.println(“Invalid first name”);

return;

}

// last name validation (not null, non-empty String)

String lastName = prompt(“Please enter the owner last name: “);

if (“”.equals(lastName) || lastName == null) {

System.out.println(“Invalid last name”);

return;

}

// id validation (unique positive number)

int id;

try {

id = Integer.parseInt(prompt(“Please enter the owner id: “));

if (id <= 0) {

System.out.println(“Id should be a positive number”);

return;

}

} catch (NumberFormatException e) {

System.out.println(“Id should be a number”);

return;

}

if (getOwnerIndex(id) != -1) {

System.out.println(“Owner with id ” + id + ” already exists”);

return;

}

// email validation (not null, non-empty String, contains . and @)

String email = prompt(“Please enter the owner email: “);

if (“”.equals(email) || email == null || !(email.contains(“.”) && email.contains(“@”))) {

System.out.println(“Invalid email”);

return;

}

// telephone number validation (not null, non-empty String)

String telephoneNumber = prompt(“Please enter the owner telephone number: “);

if (“”.equals(telephoneNumber) || telephoneNumber == null) {

System.out.println(“Invalid telephone number”);

return;

}

if (currNumberOfOwners == MAX_OWNERS) {

System.out.println(“The hospital is full”);

} else {

owners[currNumberOfOwners++] = new Owner(firstName, lastName, id, email, telephoneNumber);

}

}

private void addPet() {

int currOwnerId;

try {

currOwnerId = Integer.parseInt(prompt(“Please enter the owner id: “));

if (currOwnerId < 1) {

System.out.println(“Owner id should be a positive number”);

return;

}

if (getOwnerIndex(currOwnerId) == -1) {

System.out.println(“There is no owner with id ” + currOwnerId);

return;

}

} catch (NumberFormatException e) {

System.out.println(“Owner id should be a number”);

return;

}

Owner currentOwner = owners[getOwnerIndex(currOwnerId)];

String type = prompt(“Please enter a pet type: “);

if (“”.equals(type) || type == null) {

System.out.println(“Invalid pet type”);

return;

}

String name = prompt(“Please enter a pet name: “);

if (“”.equals(name) || name == null) {

System.out.println(“Invalid pet name”);

return;

}

String reasonForVisit = prompt(“Please enter a reason for visit: “);

if (“”.equals(reasonForVisit) || reasonForVisit == null) {

System.out.println(“Invalid reason for visit”);

return;

}

currentOwner.addPet(new Pet(type, name, reasonForVisit));

}

private void resolveDisplayOption() {

printDisplayMenu();

int displayOption = Integer.parseInt(prompt(“Please enter the option: “));

switch (displayOption) {

case DISPLAY_ALL: {

displayAll();

break;

}

case DISPLAY_OWNER: {

displayOwner();

break;

}

case EXIT: {

exitDisplay = true;

break;

}

default: {

System.out.println(“Can not resolve the option ” + displayOption);

}

}

}

private void displayAll() {

for (int i = 0; i < currNumberOfOwners; i++) {

System.out.println(owners[i]);

}

}

private void displayOwner() {

int currOwnerId;

try {

currOwnerId = Integer.parseInt(prompt(“Please enter the owner id: “));

if (currOwnerId < 1) {

System.out.println(“Owner id should be a positive number”);

return;

}

if (getOwnerIndex(currOwnerId) == -1) {

System.out.println(“There is no owner with id ” + currOwnerId);

return;

}

} catch (NumberFormatException e) {

System.out.println(“Owner id should be a number”);

return;

}

System.out.println(owners[getOwnerIndex(currOwnerId)]);

}

private int getOwnerIndex(int id) {

for (int i = 0; i < currNumberOfOwners; i++) {

if (owners[i].getId() == id) {

return i;

}

}

return -1;

}

private String prompt(String message) {

System.out.print(message);

return scanner.nextLine();

}

} 

Owner.java

package main.java;

public class Owner {

private static final int MAX_PETS = 5;

private String firstName;

private String lastName;

private int id;

private String email;

private String telephoneNumber;

private Pet[] pets;

private int currNumOfPets;

public Owner(String firstName, String lastName, int id, String email, String telephoneNumber) {

this.firstName = firstName;

this.lastName = lastName;

this.id = id;

this.email = email;

this.telephoneNumber = telephoneNumber;

pets = new Pet[MAX_PETS];

}

public void addPet(Pet pet) {

if (currNumOfPets == MAX_PETS) {

System.out.println(“The owner ” + firstName + ” ” + lastName +

” already owns a maximum number of pets”);

} else {

pets[currNumOfPets++] = pet;

}

}

public int getId() {

return id;

}

@Override

public String toString() {

String result = “[“;

result += “\n  First name: ” + firstName;

result += “\n  Last name: ” + lastName;

result += “\n  Id: ” + id;

result += “\n  Email: ” + email;

result += “\n  Telephone number: ” + telephoneNumber;

for (int i = 0; i < currNumOfPets; i++) {

result += “\n” + pets[i].toString();

}

result += “\n]”;

return result;

}

} 

Pet.java

package main.java;

public class Pet {

private String type;

private String name;

private String reasonForVisit;

public Pet(String type, String name, String reasonForVisit) {

this.type = type;

this.name = name;

this.reasonForVisit = reasonForVisit;

}

@Override

public String toString() {

return ”  [\n    Type: ” + type + “\n    Name: ”

+ name + “\n    Reason for visit: ” + reasonForVisit + “\n  ]”;

}

}