In: Computer Science
Use JAVA BASICS classes, aggregation and manipulating arrays of objects.
I DO NOT WANT THE SAME SOLUTION WHICH ALREADY EXISTS ON CHEG.
DO NO USE ARRAY LIST
Scenario:
A dog shelter would like a simple system to keep track of all the dogs that pass through the facility.
The system must record for each dog:
dogId (int) - must be unique
name (string)
age (double) - cannot be less than 0 or more than 25
breed (string)
sex (char) – m for male, f for female
foundHome (bool) - true means the dogs has been places in a home false otherwise.
You must create an object oriented solution with a text based menu as summarized on the next page. The system must check that valid data is entered. For example, the menu has four items so only 1 to 5 must be allowed as input from the menu.
Summary of Operations
System Menu:
Overview:
Add Dog:
When a dog is added to the system, you must check that the dogId is not already used in the system. All new dogs have no home as yet (foundHome = false).
View all Dogs:
This menu option shows all dogs in the system. This includes dogs that have a home and those that do not.
View all available dogs:
Shows all dogs in the system, that have no homes as yet.
View dog:
Asks the user for a dogId and then displays the dog information if found and “There is no dog with that id..” if it is not found.
Update dog home status:
Asks the user for a dogId. If a dog with that id is found, the “foundHome” status is changed to true and the dog information is to be displayed. If the dog is not found the message “There is no dog with that id..” should be displayed.
Dog.java
public class Dog {
//instance variables
private int dogId;
private String name;
private double age;
private String breed;
private char sex;
private boolean foundHome;
//default constructor
public Dog()
{
this.dogId = 0;
this.name = "";
this.age = 0;
this.breed = "";
this.sex = ' ';
this.foundHome = false;
}
//constructor with parameters
public Dog(int dogId, String name, double age, String
breed, char sex, boolean foundHome)
{
this.dogId = dogId;
this.name = name;
this.age = age;
this.breed = breed;
this.sex = sex;
this.foundHome = foundHome;
}
//getters
//method that gets the dogId
public int getDogId()
{
return this.dogId;
}
//method that gets the dog name
public String getName()
{
return this.name;
}
//method that gets the dog age
public double getAge()
{
return this.age;
}
//method that gets the dog breed
public String getBreed()
{
return this.breed;
}
//method that gets the dog sex
public char getSex()
{
return this.sex;
}
//method that gets the home of the dog
public boolean getFoundHome()
{
return this.foundHome;
}
//setters
//method that sets the dogId
public void setDogId(int dogId)
{
this.dogId = dogId;
}
//method that sets the dog name
public void setName(String name)
{
this.name = name;
}
//method that sets the dog age
public void setAge(double age)
{
this.age = age;
}
//method that sets the dog breed
public void setBreed(String breed)
{
this.breed = breed;
}
//method that sets the dog sex
public void setSex(char sex)
{
this.sex = sex;
}
//method that sets the home of the dog
public void setFoundHome(boolean foundHome)
{
this.foundHome = foundHome;
}
//display the dog
public String toString()
{
if(getFoundHome())
return "ID: " +
getDogId() + " Name: " + getName() + " Age: " + getAge() + " Breed:
" + getBreed()
+ " Sex: " +
getSex() + " Found Home: Yes";
else
return "ID: " +
getDogId() + " Name: " + getName() + " Age: " + getAge() + " Breed:
" + getBreed()
+ " Sex: " +
getSex() + " Found Home: No";
}
}
DogShelter.java
public class DogShelter extends Dog {
//instance variable
private Dog[] dogs = new Dog[100];
private static int numberOfDogs = 0;
//method that adds a dog to the system
public void addDog(int dogId, String name, double age,
String breed, char sex)
{
boolean dogExists = false;
//check if the dog id exists
for(int i = 0; i < numberOfDogs;
i++)
{
if(dogs[i].getDogId() == dogId)
dogExists = true;
}
//add the dog if id does not
exist
if(!dogExists)
{
dogs[numberOfDogs] = new Dog(dogId, name, age, breed, sex,
false);
System.out.println("Dog added successfully");
numberOfDogs++;
}
//else display error message
else
System.out.println("DogId already exists");
}
//method that displays all the dogs
public void viewAllDogs()
{
for(int i = 0; i < numberOfDogs;
i++)
{
System.out.println(dogs[i].toString());
}
}
//method that displays the dogs that don't have home
yet
public void viewAllAvailableDogs()
{
//run a loop and display all the
dogs
for(int i = 0; i < numberOfDogs;
i++)
{
if(dogs[i].getFoundHome() == false)
System.out.println(dogs[i].toString());
}
}
//method that shows a particular dog
public void viewDog(int dogId)
{
int index = -1;
//check if dog exists with given
id
for(int i = 0; i < numberOfDogs;
i++)
{
//if exists
store the index
if(dogs[i].getDogId() == dogId)
index = i;
}
//if index is -1 display error
message
if(index == -1)
System.out.println("There is no dog with that id..");
//else display the dog
else
System.out.println(dogs[index].toString());
}
//method that update the dog home status
public void updateDogHomeStatus(int dogId)
{
int index = -1;
//check if dog exists with given
id
for(int i = 0; i < numberOfDogs;
i++)
{
//if exists
store the index
if(dogs[i].getDogId() == dogId)
index = i;
}
//if index is -1 display error
message
if(index == -1)
{
System.out.println("There is no dog with that id..");
}
//else update and display the
dog
else
{
dogs[index].setFoundHome(true);
System.out.println("\nUpdated dog info:");
viewDog(dogs[index].getDogId());
}
}
}
DogShelterDemo.java
import java.util.Scanner;
public class DogShelterDemo {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
DogShelter dogs = new
DogShelter();
//variables
int dogId;
String name;
double age;
String breed;
char sex;
int userChoice;
//run a loop until the user wishes
to exit
do
{
//display the
menu
System.out.println("1. Add dog");
System.out.println("2. View all dogs");
System.out.println("3. View all available dogs");
System.out.println("4. Update dog home status");
System.out.println("5. Exit");
//prompt and
read the user choice
System.out.print("Enter your choice: ");
userChoice =
input.nextInt();
//run a switch
case based on user choice
switch(userChoice)
{
case 1:
//read the dog details from the user
System.out.print("Enter the dog id: ");
dogId = input.nextInt();
System.out.print("Enter the name: ");
input.nextLine();
name = input.nextLine();
System.out.print("Enter the age: ");
age = input.nextInt();
while(age < 1 || age > 25)
{
System.out.println("Age
cannot be less than 1 and greater than 25. Try again: ");
age = input.nextInt();
}
System.out.print("Enter the breed: ");
input.nextLine();
breed = input.nextLine();
System.out.print("Enter the sex: ");
sex = input.nextLine().charAt(0);
dogs.addDog(dogId, name, age, breed, sex); //add
the dog
break;
case 2:
//display all the dogs
System.out.println("\nInfo of all dogs:");
dogs.viewAllDogs();
break;
case 3:
//display all available dogs
System.out.println("\nInfo of all dogs
available:");
dogs.viewAllAvailableDogs();
break;
case 4:
//prompt and read the dogId
System.out.print("Enter the dog id: ");
dogId = input.nextInt();
//update the home status
dogs.updateDogHomeStatus(dogId);
break;
case 5:
System.out.println("Thank you for using our
system.");
break;
default:
System.out.println("Invalid input.");
}
System.out.println();
}while(userChoice != 5);
input.close();
}
}
Output:
Code Screenshots:
Let me know if you have any concerns with the above solution