In: Computer Science
Java Programming Question:
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 java 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.
Answer :
Explanation: I have written all the code as per the requirements.I have made a Dog class and a DogMenuSystem class that is used to display the menus and perform various operations depending on the menu selected by the user.Please find both the codes in the answer.I have also shown the output of the program ,please find the images attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation
//code starts
//Dog class
public class Dog {
private int dogId;
private String name;
private double age;
private String breed;
private char sex;
private boolean foundHome;
public Dog(int dogId, String name, double age, String breed,
char sex,
boolean foundHome) {
super();
this.dogId = dogId;
this.name = name;
this.age = age;
this.breed = breed;
this.sex = sex;
this.foundHome = foundHome;
}
public int getDogId() {
return dogId;
}
public String getName() {
return name;
}
public double getAge() {
return age;
}
public String getBreed() {
return breed;
}
public char getSex() {
return sex;
}
public boolean isFoundHome() {
return foundHome;
}
public void setFoundHome(boolean foundHome) {
this.foundHome = foundHome;
}
}
//DogMenuSystem class
import java.util.ArrayList;
import java.util.Scanner;
public class DogMenuSystem {
private static boolean checkDogIdExist(ArrayList<Dog>
dogs, int dogId) {
for (Dog dog : dogs) {
if (dog.getDogId() == dogId)
return false;
}
return true;
}
private static Dog getDogFromId(ArrayList<Dog> dogs, int
dogId,
boolean updateFlag) {
for (Dog dog : dogs) {
if (dog.getDogId() == dogId) {
if (updateFlag) {
dog.setFoundHome(true);
}
return dog;
}
}
return null;
}
public static void main(String[] args) {
ArrayList<Dog> dogs = new ArrayList<>();
Scanner input = new Scanner(System.in);
int choice = -1;
while (choice != 6) {
System.out.println("1)Add dog" + "\n" + "2)View all dogs" +
"\n"
+ "3)View all available dogs" + "\n" + "4)View dogs" + "\n"
+ "5)Update dog home status" + "\n" + "6)exit");
choice = input.nextInt();
switch (choice) {
case 1 :
System.out.print(
"Enter the dogId,name,age,breed and sex(m for male and f for
female) of dog separated by space:");
int dogId = input.nextInt();
String name = input.next();
double age = input.nextDouble();
String breed = input.next();
char sex = input.next().charAt(0);
if (!checkDogIdExist(dogs, dogId)) {
System.out.println("DogId already exists");
}
Dog dog = new Dog(dogId, name, age, breed, sex, false);
dogs.add(dog);
break;
case 2 :
for (Dog dogIterator : dogs) {
System.out.println("dogId :" + dogIterator.getDogId()
+ ", dog name:" + dogIterator.getName()
+ ", dog breed:" + dogIterator.getBreed()
+ ", sex:" + dogIterator.getSex()
+ ", home status:" + dogIterator.isFoundHome());
}
break;
case 3 :
for (Dog dogIterator : dogs) {
if (!dogIterator.isFoundHome()) {
System.out.println("dogId :"
+ dogIterator.getDogId() + ", dog name:"
+ dogIterator.getName() + ", dog breed:"
+ dogIterator.getBreed() + ", sex:"
+ dogIterator.getSex());
}
}
break;
case 4 :
System.out.print("Enter the dog id:");
dogId = input.nextInt();
dog = getDogFromId(dogs, dogId, false);
if (dog != null) {
System.out.println("dogId :" + dog.getDogId()
+ ", dog name:" + dog.getName() + ", dog breed:"
+ dog.getBreed() + ", sex:" + dog.getSex()
+ ", home status:" + dog.isFoundHome());
} else {
System.out.println("There is no dog with that id");
}
break;
case 5 :
System.out.print("Enter the dog id:");
dogId = input.nextInt();
dog = getDogFromId(dogs, dogId, true);
if (dog != null) {
System.out.println("dogId :" + dog.getDogId()
+ ", dog name:" + dog.getName() + ", dog breed:"
+ dog.getBreed() + ", sex:" + dog.getSex()
+ ", home status:" + dog.isFoundHome());
} else {
System.out.println("There is no dog with that id");
}
break;
case 6 :
choice = 6;
break;
default :
System.out.println("Invalid option");
break;
}
}
input.close();
}
}
Output :
I hope this answer is helpful to you. Please Upvote(Thums Up) my answer, Thank you.