Question

In: Computer Science

Java Programming Question: A dog shelter would like a simple system to keep track of all...

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:

  1. Add dog
  2. View all dogs
  3. View all available dogs
  4. Update dog home status
  5. 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.

Solutions

Expert Solution

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.


Related Solutions

Using c++ Design a system to keep track of employee data. The system should keep track...
Using c++ Design a system to keep track of employee data. The system should keep track of an employee’s name, ID number and hourly pay rate in a class called Employee. You may also store any additional data you may need, (hint: you need something extra). This data is stored in a file (user selectable) with the id number, hourly pay rate, and the employee’s full name (example): 17 5.25 Daniel Katz 18 6.75 John F. Jones Start your main...
C# The Zookeepers need a system to keep track of all their animals. They need to...
C# The Zookeepers need a system to keep track of all their animals. They need to be able to enter all their animals into the system in a way that allows them to identify and locate them. This requires identifying them by species, age and one characteristic unique to their species. There are three cages and the user must input information about the animal in each one. After accepting input for all three cages, the program should output the contents...
C# The Zookeepers need a system to keep track of all their animals. They need to...
C# The Zookeepers need a system to keep track of all their animals. They need to be able to enter all their animals into the system in a way that allows them to identify and locate them. This requires identifying them by species, age and one characteristic unique to their species. There are three cages and the user must input information about the animal in each one. After accepting input for all three cages, the program should output the contents...
This is a simple list table of a company trying to keep track of parts that...
This is a simple list table of a company trying to keep track of parts that they sell and orders that came in purchasing those parts (in other words, not a database but a flat one table file). You will design a database for this company so that they won’t be relying on a simple 1 table list system to keep track of their data. Looking at the table below, produce the 3NF of the data. OrderNum OrderDate PartNum Description...
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The program will now be broken up into methods and store all the inventory in an ArrayList object. The program will be able to run a report for all inventory details as well as a report for items that are low in...
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The Food and Book items should inherit all the properties of the Product item in the previous assignment. Foods cannot be added to the inventory without an expiration date. Implement a toString method for Product, Food, and Book. Grading details: Correct usage...
Write a Java program that lets the user keep track of their homemade salsa sales. Use...
Write a Java program that lets the user keep track of their homemade salsa sales. Use 5-element arrays to track the following. The salsa names mild, medium, sweet, hot, and zesty. The number of each type sold. The price of each type of salsa. Show gross amount of money made (before tax). Calculate how much the user owes in sales tax (6% of the amount made). Then display the net profit (after subtracting the tax).
Java Programming Question The problem is to count all the possible paths from top left to...
Java Programming Question The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move to right or down.Input: The first line of input contains an integer T, denoting the number of test cases. The first line of each test case is M and N, M is number of rows and N is number of columns.Output: For each test case, print the...
For ONE day, keep track of all of the electrical devices that you make use of...
For ONE day, keep track of all of the electrical devices that you make use of and for how long. (eg. Microwave oven for 3 minutes; hair dryer for 5 minutes; TV for 90 minutes;...) Track only those devices over which you have direct control and don’t bother about things like home heating and refrigerators that are too challenging to track. Using either a published table of common power ratings (cite your source), or information read off of the device...
Programming language to be used: Java Exercises Part 1) The Dog Class In the first part...
Programming language to be used: Java Exercises Part 1) The Dog Class In the first part of the lab, we are writing a class to represent a Dog. It should not have a main method. Dog needs fields for price (to purchase the dog), breed, name, and age. Use appropriate data types The class should have the following two kinds of Constructors: Constructor 1: Write a constructor that accepts a value for each of the fields Constructor 2: Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT