Instructions
Requirements and Specifications
Source Code
VET CLINIC
public class VetClinic {
private static final int COLLECTION_SIZE = 5;
private Vet[] vets = new Vet[COLLECTION_SIZE];
/* ---------------------------------------------------
--- Create a new VetClinic collection populated ---
--- with blank Vet entries ---
---------------------------------------------------
*/
public VetClinic() {
for (int i = 0; i < COLLECTION_SIZE; i++) {
this.vets[i] = new Vet();
}
}
/* ----------------------------------------------------
--- Display the entire VetClinic collection with ---
--- one vet per line. Empty entries should not ---
--- be printed. ---
----------------------------------------------------
*/
public void showVetClinic() {
System.out.println("\nMy VetClinic\n-----------------");
for (int i = 0; i < this.vets.length; i++) {
if (!this.vets[i].getName().equals(""))
System.out.println(this.vets[i]);
}
System.out.println("-----------------\n");
}
/* -----------------------------------------------
--- Add the newVet to the first element in ---
--- vets with name = "". Return true if ---
--- successful, false otherwise ---
-----------------------------------------------
*/
public boolean addVet(Vet newVet) {
// TO DO: Add code to this method to prevent the same name
// being used twice.
// If the name already exists in the collection,
// false is returned and the collection is not updated
int i = 0;
while (i < COLLECTION_SIZE && !this.vets[i].getName().equals("")) {
if (this.vets[i].getName().equals(newVet.getName())) {
return false;
}
i++;
}
if (i < COLLECTION_SIZE) {
this.vets[i] = newVet;
return true;
} else return false;
}
/* -----------------------------------------------------------
--- Update the specialism of the VetClinic item that ---
--- matches the name and specialism values of the Vet ---
--- passed as a parameter. Return true if successful or ---
--- false if a matching vet is not found. ---
-----------------------------------------------------------
*/
public boolean updateSpecialism(Vet aVet, String newSpecialism) {
// TO DO: Provide this method body
for (int i = 0; i < this.vets.length; i++) {
String name = this.vets[i].getName();
if (name.isEmpty()) {
continue;
}
if (name.equals(aVet.getName())) {
this.vets[i].setSpecialism(newSpecialism);
return true;
}
}
return false;
}
/* ----------------------------------------------------------
--- Return the vet names where the vet specialism ---
--- matches that passed as a parameter, one title per ---
--- or "" if the artist name is not found ---
----------------------------------------------------------
*/
public String getVetsBySpecialism(String aSpecialism) {
// TO DO: Provide this method body
StringBuilder builder = new StringBuilder("");
for (int i = 0; i < this.vets.length; i++) {
String name = this.vets[i].getName();
if (name.isEmpty()) {
continue;
}
if (this.vets[i].getSpecialism().equals(aSpecialism)) {
if (!builder.toString().isEmpty()) {
builder.append(System.lineSeparator());
}
builder.append(name);
}
}
return builder.toString();
}
/* ---------------------------------------------------------
--- Delete the Vet matching the name and specialism ---
--- of that passed as a parameter by setting it to an ---
--- empty Vet. Return true if successful or false if ---
--- the details in the parameter do not match a Vet ---
--- in the collection ---
---------------------------------------------------------
*/
public boolean delete(Vet aVet) {
// TO DO: Provide this method body
for (int i = 0; i < this.vets.length; i++) {
String name = this.vets[i].getName();
if (name.isEmpty()) {
continue;
}
if (this.vets[i].getName().equals(aVet.getName())) {
this.vets[i] = new Vet();
return true;
}
}
return false;
}
/* ------------------------------------------------------------
--- Return true if the VetClinic contains at least three ---
--- non-empty Vet objects or false otherwise ---
------------------------------------------------------------
*/
public boolean isViable() {
// TO DO: Provide this method body
int counter = 0;
for (int i = 0; i < this.vets.length; i++) {
String name = this.vets[i].getName();
if (name.isEmpty()) {
continue;
}
counter++;
}
return counter >= 3;
}
}
VET
public class Vet {
private String name;
private String specialism;
public Vet() {
this.name = "";
this.specialism = "";
}
public Vet(String newName, String newSpecialism) {
this.name = newName;
this.specialism = newSpecialism;
}
public String getName() {
return this.name;
}
public String getSpecialism() {
return this.specialism;
}
public void setName(String newName) {
this.name = newName;
}
public void setSpecialism(String newSpecialism) {
this.specialism = newSpecialism;
}
public String toString() {
return this.getName() + " specialises in " + this.getSpecialism();
}
}
VET CLINIC TEST
public class VetClinicTest {
public static void main(String[] args) {
VetClinic vc = new VetClinic();
if (vc.addVet(new Vet("Amanda", "cats")))
System.out.println("Amanda added to clinic");
else System.out.println("Amanda NOT ADDED");
if (vc.addVet(new Vet("Bob", "dogs")))
System.out.println("Bob added to clinic");
else System.out.println("Bob NOT ADDED");
if (vc.addVet(new Vet("Christine", "horses")))
System.out.println("Christine added to clinic");
else System.out.println("Christine NOT ADDED");
if (vc.addVet(new Vet("Bob", "rabbits")))
System.out.println("Bob added to clinic");
else System.out.println("Bob NOT ADDED");
if (vc.addVet(new Vet("David", "dogs")))
System.out.println("David added to clinic");
else System.out.println("David NOT ADDED");
if (vc.addVet(new Vet("Erica", "horses")))
System.out.println("Erica added to clinic");
else System.out.println("Erica NOT ADDED");
if (vc.addVet(new Vet("Fred", "goldfish")))
System.out.println("Fred added to clinic");
else System.out.println("Fred NOT ADDED");
vc.showVetClinic();
if (vc.updateSpecialism(new Vet("Christine", "horses"), "snakes"))
System.out.println("Christine's specialism changed");
else System.out.println("Christine's specialism NOT CHANGED");
System.out.println("\nDog specialists: \n" + vc.getVetsBySpecialism("dogs"));
if (vc.delete(new Vet("Bob", "dogs")))
System.out.println("Bob deleted");
else System.out.println("Bob NOT DELETED");
vc.showVetClinic();
if (vc.isViable()) System.out.println("The clinic is viable");
else System.out.println("The clinic is not viable");
if (vc.delete(new Vet("Amanda", "cats")))
System.out.println("Amanda deleted");
else System.out.println("Amanda NOT DELETED");
if (vc.delete(new Vet("Christine", "snakes")))
System.out.println("Christine deleted");
else System.out.println("Christine NOT DELETED");
vc.showVetClinic();
if (vc.isViable()) System.out.println("The clinic is viable");
else System.out.println("The clinic is not viable");
}
}