In: Computer Science
THE QUESTION IS OF JAVA LANGUAGE.
ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE
DIFFERENTIATE FILES SO I CAN UNDERSTAND BETTER.
NOTE - Submission in parts. Parts required - Dog Class Code, Dog
Manager Class Code and the main code. Please differentiate all
three in the answer.
This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects.
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.
Solution of the above problem is provided below.
three separate .java files are provided
Dog.java
public class Dog {
private int dogId;
private String name;
private double age;
private String breed;
private char sex;
private boolean foundHome;
/**
* @param dogId
* @param name
* @param age
* @param breed
* @param sex
* @param foundHome
*/
public Dog(int dogId, String name, double age, String breed, char sex) {
super();
this.dogId = dogId;
this.name = name;
this.age = age;
this.breed = breed;
this.sex = sex;
this.foundHome = false;
}
/**
* @return the dogId
*/
public int getDogId() {
return dogId;
}
/**
* @param dogId the dogId to set
*/
public void setDogId(int dogId) {
this.dogId = dogId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public double getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(double age) {
this.age = age;
}
/**
* @return the breed
*/
public String getBreed() {
return breed;
}
/**
* @param breed the breed to set
*/
public void setBreed(String breed) {
this.breed = breed;
}
/**
* @return the sex
*/
public char getSex() {
return sex;
}
/**
* @param sex the sex to set
*/
public void setSex(char sex) {
this.sex = sex;
}
/**
* @return the foundHome
*/
public boolean isFoundHome() {
return foundHome;
}
/**
* @param foundHome the foundHome to set
*/
public void setFoundHome(boolean foundHome) {
this.foundHome = foundHome;
}
@Override
public String toString() {
return "Dog [dogId=" + dogId + ", name=" + name + ", age=" + age + ", breed=" + breed + ", sex=" + sex
+ ", foundHome=" + foundHome + "]";
}
}
DogManager.java
import java.util.*;
public class DogManager {
ArrayList<Dog> dogList = new ArrayList<>();
/* method to add dog to the list*/
public void addDog(Dog dog) {
if(!dogList.contains(dog)) {
dogList.add(dog);
}
}
/* Method to view all dogs */
public void viewAllDogs() {
for(Dog d : dogList) {
System.out.println("\t"+d.toString());
}
}
public void viewAllAvailableDogs() {
for(Dog d : dogList) {
if(!d.isFoundHome()) {
System.out.println("\t"+d.toString());
}
}
}
/* print dog information if the dogId matches */
public void viewDog(int dogId) {
for(Dog d : dogList) {
if(d.getDogId() == dogId) {
System.out.println("\t"+d.toString());
}
}
}
public void updateDogHomeStatus(int dogID) {
for(Dog d : dogList) {
if(d.getDogId() == dogID) {
d.setFoundHome(true);
System.out.println("\t"+d.toString());
}
System.out.println("There is no dog with that id..");
}
}
}
Main.java <Driver class of code>
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// dog class objects
Dog d1 = new Dog(1,"tony",5,"Jerman Shepard", 'm');
Dog d2 = new Dog(2,"jessy",8,"Pamerian", 'm');
// creating objects of dog manager class
DogManager dm = new DogManager();
/* manually adding dog objects to list to test the view dogs method */
dm.addDog(d2);
int choice;
do {
menu();
choice = sc.nextInt();
switch (choice) {
case 1: {
System.out.println("Enter dogid");
int dogid = sc.nextInt();
System.out.println("Enter dog name");
String dogName = sc.next();
System.out.println("Enter age");
double age = sc.nextDouble();
System.out.println("Enter breed");
String breed = sc.next();
System.out.println("Enter sex of dog m/f");
char sex = sc.next().charAt(0);
dm.addDog(new Dog(dogid, dogName, age, breed, sex));
break;
}
case 2: {
dm.viewAllDogs();
break;
}
case 3: {
dm.viewAllAvailableDogs();
break;
}
case 4: {
System.out.println("Enter Dog id");
int dogID = sc.nextInt();
dm.viewDog(dogID);
break;
}
case 5: {
System.out.println("Enter Dog id");
int dogID = sc.nextInt();
dm.updateDogHomeStatus(dogID);
break;
}
case 6: {
System.exit(0);
break;
}
default:
System.out.println("\nInvalid choice!\n");
}
} while (choice != 0);
}
public static void menu() {
System.out.print("1. Add dog\n"
+ "2. View all dogs\n"
+ "3. View all available dogs \n"
+ "4. View dog \n"
+ "5. Update dog home status\n"
+ "6. exit\n");
}
}
Output of the program:
End of Solution