In: Computer Science
SOLUTION IN JAVA LANGUANGE NEEDED. JAVA LANGUAGE
QUESTION.
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:
Add dog
View all dogs
View all available dogs
View dog
Update dog home status
exit
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.
import java.util.*;
class Dog
{
int dogId;
String name;
double age;
String breed;
char sex;
boolean foundHome=false;
public Dog(int id,String n,double age,String breed,char s)
{
this.dogId=id;
this.name=n;
this.age=age;
this.breed=breed;
this.sex=s;
}
}
class DogManager
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("System Menu:\n1.Add dog\n2.View all
dogs\n3.View all available dogs\n4.View dog\n5.Update dog home
status\nother:exit");
System.out.println("Enter the operation number:");
int input =sc.nextInt();
int id_arr[]=new int[10];
int k=0;
Dog d[]=new
Dog[10];
int id;
String name;
String breed;
char sex;
double age;
switch(input)
{
case 1: System.out.println("Enter Dog Details");
id=sc.nextInt();
name=sc.next();
age=sc.nextDouble();
breed=sc.next();
sex=sc.next().charAt(0);
if(age<=0 && age>25)
{
System.out.println("age should between 1 to 25");
break;
}
int count=0;
if(id_arr.length>0){
for(int
i=0;i<id_arr.length;i++)
{
if(id_arr[i]==id)
{
count++;
}
}
}
System.out.println("count="+count);
if(count==0)
{
id_arr[k]=id;
k++;
d[k]=new Dog(id,name,age,breed,sex);
Dog p=d[k];
System.out.println(p.dogId+" "+p.name);
}
else
{
System.out.println("Dog Id already present");
}
break;
case 2:
System.out.println("DOGID NAME AGE BREED SEX HOME_STATUS");
System.out.println(k);
for(int i=0;i<=k;i++)
{
Dog e=d[i];
System.out.println(e.dogId+" "+e.name+" "+e.age+" "+e.breed+"
"+e.sex+" "+e.foundHome);
}
break;
case 3:
System.out.println("Dogs without home in the system \n DOGID NAME
AGE BREED SEX ");
for(int i=0;i<=k;i++)
{
Dog e=d[i];
if(e.foundHome==false)
{
System.out.println(e.dogId+" "+e.name+" "+e.age+" "+e.breed+"
"+e.sex);
}
}
break;
case 4:System.out.println("Enter a dog
id");
int d1=sc.nextInt();
for(int i=0;i<=k;i++)
{
Dog y=d[i];
if(y.dogId==d1)
{
System.out.println(y.dogId+"
"+y.name+" "+y.age+" "+y.breed+" "+y.sex);
break;
}
}
System.out.println("There is no dog with that id..");
break;
case 5:System.out.println("Enter a dog id");
int d_id=sc.nextInt();
for(int i=0;i<=k;i++)
{
Dog y=d[i];
if(y.dogId==d_id)
{
y.foundHome=true;
break;
}
}
System.out.println("There is no dog with that id..");
break;
default: System.out.println("Invalid Input...Please provide input
between 1 to 5");
return;
}
}
}
}