Question

The Program (Java) You will create a pet database program with the following operations incrementally as...

The Program (Java)
You will create a pet database program with the following operations incrementally as describe
in the Milestones section. You are not required to save the pet data into a file. You must use
appropriate design and make use of Object-Oriented Design. See milestones.
• Add pets
o Let the user add as many pets as they want. A pet is entered as a single line
consisting of a name and an integer which represents the age of the pet. The
user type “done” to end reading. See sample run.

• Show pets
o Prints a table of all pets in the database. See below or the sample run for the
formatting of the table.

• Update pets
o Shows the user a table of all the pets and reads the pet ID that the user wants to
update. ID is the array index of the object. Once the user has selected a pet by
typing and ID, it asks the user to enter the new name and new age. It then
updates the actual object.
• Search pets by name or age
o Prompts the user for a name and then displays a table showing all pets matching
the name. The name is case insensitive. For example, “boomer” will “Boomer”.
o Prompts the user for an age and shows a table consists of pets with that age.
• Remove a pet
o Prompts the user for the index of the Pet to delete.

Table Formatting:
Your program will display the results for view all pets, search pet by name and search pet by
age using a table as shown below. You will need to use System.out.printf() to format the table.
Below is a sample table with annotations as to which part is the header, rows, and footer. A
row represents a single record about a pet. The ID is the index in the array the object is
located. A table consists of zero more rows. You may use 3 characters for ID, 10 characters for
NAME, and 4 characters for AGE.
+----------------------+
| ID | NAME | AGE | header
+----------------------+
| 0 | Kitty | 8 |
| 1 | Bruno | 7 |
| 2 | Boomer | 8 | rows
| 3 | Boomer | 3 |
| 4 | Fiesty | 3 |
+----------------------+
5 rows in set. footer

Sample run


Pet Database Program.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 2
add pet (name, age): Kitty 8
add pet (name, age): Bruno 7
add pet (name, age): Boomer 8
add pet (name, age): Boomer 3
add pet (name, age): Fiesty 3
add pet (name, age): done
5 pets added.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 1 +----------------------
+
| ID | NAME | AGE | +----------------------+
| 0 | Kitty | 8 |
| 1 | Bruno | 7 |
| 2 | Boomer | 8 |
| 3 | Boomer | 3 |
| 4 | Fiesty | 3 | +----------------------+
5 rows in set.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 2

add pet (name, age): Krazy 11
add pet (name, age): done
1 pets added.
What would you like to do
?

1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 1 +----------------------
+
| ID | NAME | AGE | +----------------------+
| 0 | Kitty | 8 |
| 1 | Bruno | 7 |
| 2 | Boomer | 8 |
| 3 | Boomer | 3 |
| 4 | Fiesty | 3 |
| 5 | Krazy | 11 | +----------------------+
6 rows in set.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 3 +----------------------
+
| ID | NAME | AGE | +----------------------+
| 0 | Kitty | 8 |
| 1 | Bruno | 7 |
| 2 | Boomer | 8 |
| 3 | Boomer | 3 |
| 4 | Fiesty | 3 |
| 5 | Krazy | 11 | +----------------------+
6 rows in set.
Enter the pet ID you can to update:1
Enter new name and new age: Brunoozz 8

Bruno 7 changed to Brunoozz 8.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 1 +----------------------
+
| ID | NAME | AGE | +----------------------+
| 0 | Kitty | 8 |
| 1 | Brunoozz | 8 |
| 2 | Boomer | 8 |
| 3 | Boomer | 3 |
| 4 | Fiesty | 3 |
| 5 | Krazy | 11 | +----------------------+
6 rows in set.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 4 +----------------------
+
| ID | NAME | AGE | +----------------------+
| 0 | Kitty | 8 |
| 1 | Brunoozz | 8 |
| 2 | Boomer | 8 |
| 3 | Boomer | 3 |
| 4 | Fiesty | 3 |
| 5 | Krazy | 11 | +----------------------+
6 rows in set.
Enter the pet ID to remove: 1
Brunoozz 8 is removed.

What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 1 +----------------------
+
| ID | NAME | AGE | +----------------------+
| 0 | Kitty | 8 |
| 1 | Boomer | 8 |
| 2 | Boomer | 3 |
| 3 | Fiesty | 3 |
| 4 | Krazy | 11 | +----------------------+
5 rows in set.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 5
Enter a name to search:boomer +----------------------+
| ID | NAME | AGE | +----------------------+
| 1 | Boomer | 8 |
| 2 | Boomer | 3 | +----------------------+
2 rows in set.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 6

Enter age to search:3
+----------------------+
| ID | NAME | AGE |
+----------------------+
| 2 | Boomer | 3 |
| 3 | Fiesty | 3 |
+----------------------+
2 rows in set.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 1
+----------------------+
| ID | NAME | AGE |
+----------------------+
| 0 | Kitty | 8 |
| 1 | Boomer | 8 |
| 2 | Boomer | 3 |
| 3 | Fiesty | 3 |
| 4 | Krazy | 11 |
+----------------------+
5 rows in set.
What would you like to do?
1) View all pets
2) Add more pets
3) Update an existing pet
4) Remove an existing pet
5) Search pets by name
6) Search pets by age
7) Exit program
Your choice: 7
Goodbye!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pet.java

public class Pet {
private static int count = 0;
private int id;
private String name;
private int age;

public Pet(String name, int age) {
this.id = count;
this.name = name;
this.age = age;
count++;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

PetDatabase.java

import java.util.ArrayList;
import java.util.Scanner;

public class PetDatabase {
private ArrayList<Pet> pets;

public PetDatabase() {
this.pets = new ArrayList<>();
}

public ArrayList<Pet> getPets() {
return pets;
}
  
public void viewAllPets()
{
if(this.pets.isEmpty())
{
System.out.println("\nSorry, no pets to show!\n");
return;
}
  
System.out.printf("\n%3s %10s %4s\n", "ID", "Name", "Age");
for (Pet pet : this.pets) {
System.out.printf("%3d %10s %4d\n", pet.getId(), pet.getName(), pet.getAge());
}
System.out.println();
}
  
public void addMorePets()
{
Scanner sc = new Scanner(System.in);
String line;
String name;
int age;
  
int count = 0;
  
System.out.println();
do
{
System.out.print("Add pet (name, age): ");
line = sc.nextLine().trim();
if(line.equalsIgnoreCase("done"))
break;
else
{
String[] data = line.split(" ");
name = data[0];
age = Integer.parseInt(data[1]);
  
this.pets.add(new Pet(name, age));
count++;
}
}while(!line.equalsIgnoreCase("done"));
System.out.println(count + " pets added.\n");
}
  
public void updatePet()
{
viewAllPets();
  
Scanner sc = new Scanner(System.in);
System.out.print("Enter the pet ID you can update: ");
int petId = Integer.parseInt(sc.nextLine().trim());
  
while(petId < 0 || petId > this.pets.size())
{
System.out.println("Please enter a valid et Id: ");
petId = Integer.parseInt(sc.nextLine().trim());
}
String oldName = this.pets.get(petId).getName();
int oldAge = this.pets.get(petId).getAge();
  
System.out.print("\nEnter new name and new age: ");
String line = sc.nextLine().trim();
String[] data = line.split(" ");
String name = data[0];
int age = Integer.parseInt(data[1]);
  
this.pets.get(petId).setName(name);
this.pets.get(petId).setAge(age);
  
System.out.println("\n" + oldName + " " + oldAge + " changed to "
+ this.pets.get(petId).getName() + " " + this.pets.get(petId).getAge() + "\n");
}
  
public void removePet()
{
viewAllPets();
  
Scanner sc = new Scanner(System.in);
System.out.print("Enter the pet ID to remove: ");
int petId = Integer.parseInt(sc.nextLine().trim());
  
while(petId < 0 || petId > this.pets.size())
{
System.out.println("Please enter a valid et Id: ");
petId = Integer.parseInt(sc.nextLine().trim());
}
String oldName = this.pets.get(petId).getName();
int oldAge = this.pets.get(petId).getAge();
  
this.pets.remove(petId);
System.out.println("\n" + oldName + " " + oldAge + " is removed.\n");
}
  
public void searchPetsByName()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter a name to search: ");
String nameSearch = sc.nextLine().trim();
ArrayList<Pet> tempPets = new ArrayList<>();
  
for(int i = 0; i < this.pets.size(); i++)
{
if(this.pets.get(i).getName().equalsIgnoreCase(nameSearch))
tempPets.add(this.pets.get(i));
}
if(tempPets.isEmpty())
System.out.println("\nNo pets with name " + nameSearch + " was found!\n");
else
{
System.out.printf("\n%3s %10s %4s\n", "ID", "Name", "Age");
for (Pet pet : tempPets) {
System.out.printf("%3d %10s %4d\n", pet.getId(), pet.getName(), pet.getAge());
}
System.out.println();
}
}
  
public void searchPetsByAge()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter age to search: ");
int ageSearch = Integer.parseInt(sc.nextLine().trim());
ArrayList<Pet> tempPets = new ArrayList<>();
  
for(int i = 0; i < this.pets.size(); i++)
{
if(this.pets.get(i).getAge() == ageSearch)
tempPets.add(this.pets.get(i));
}
if(tempPets.isEmpty())
System.out.println("\nNo pets with age " + ageSearch + " was found!\n");
else
{
System.out.printf("\n%3s %10s %4s\n", "ID", "Name", "Age");
for (Pet pet : tempPets) {
System.out.printf("%3d %10s %4d\n", pet.getId(), pet.getName(), pet.getAge());
}
System.out.println();
}
}
}

PetDBApp.java (Driver class)

import java.util.Scanner;

public class PetDBApp {
  
public static void main(String[] args)
{
System.out.println("Pet Database Program.");
  
Scanner sc = new Scanner(System.in);
PetDatabase petDB = new PetDatabase();
  
int choice;
do
{
printMenu();;
choice = Integer.parseInt(sc.nextLine().trim());
switch(choice)
{
case 1:
{
petDB.viewAllPets();
break;
}
case 2:
{
petDB.addMorePets();
break;
}
case 3:
{
petDB.updatePet();
break;
}
case 4:
{
petDB.removePet();
break;
}
case 5:
{
petDB.searchPetsByName();
break;
}
case 6:
{
petDB.searchPetsByAge();
break;
}
case 7:
{
System.out.println("Goodbye!");
System.exit(0);
}
default:
System.out.println("Invalid choice!");
}
}while(choice != 7);
}
  
private static void printMenu()
{
System.out.print("What would you like to do?\n"
+ "1. View all pets\n"
+ "2. Add more pets\n"
+ "3. Update an existing pet\n"
+ "4. Remove an existing pet\n"
+ "5. Search pets by name\n"
+ "6. Search pet by age\n"
+ "7. Exit\n"
+ "Your choice: ");
}
}

***************************************************************** SCREENSHOT **********************************************************


Add a comment
Know the answer?
Add Answer to:
The Program (Java) You will create a pet database program with the following operations incrementally as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This assignment shpuld be code in java. Thanks Overview In this assignment you will write a...

    This assignment shpuld be code in java. Thanks Overview In this assignment you will write a program that will model a pet store. The program will have a Pet class to model individual pets and the Assignment5 class will contain the main and act as the pet store. Users will be able to view the pets, make them age a year at a time, add a new pet, and adopt any of the pets. Note: From this point on, your...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. Example Program Session: (First Run)Enter request 1 - Input new tool 2 - Delete...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • For Programming Assignment 3 you will be creating a program to manage cars in a dealership....

    For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu: 1. Display Inventory 2. Add a vehicle 3. Update a vehicle 4. Delete a vehicle 5. Sort inventory by VIN 6. Search inventory by Model 7. Read inventory from file 8. Write inventory to file and exit our program will be class based with the following UML representing the classes: Dealer...

  • Program in Java: Use​ ​the​ ​Factory​ ​pattern​ ​to​ ​address​ ​the​ ​following​ ​challenge... Your job is to...

    Program in Java: Use​ ​the​ ​Factory​ ​pattern​ ​to​ ​address​ ​the​ ​following​ ​challenge... Your job is to make it easy to obtain a Pet from a factory-like class: ● Pet is an abstract class that has 2 attributes ○ Name ○ Sound ● You should create 3 concrete types of pets ○ Parakeet (makes the sound "Tweet tweet") ○ Dog (makes the sound "Woof woof") ○ Lion (makes the sound "Roar roar") ● You should create a factory class that allows...

  • You are asked to build and test the following system using Java and the object-oriented concepts ...

    You are asked to build and test the following system using Java and the object-oriented concepts you learned in this course: Project (20 marks) CIT College of Information technology has students, faculty, courses and departments. You are asked to create a program to manage all these information's. Create a class CIT to represents the following: Array of Students, each student is represented by class Student. Array of Faculty, each faculty is represented by class Faculty. Array of Course, each course...

  • Write a C++ program to keep records and perform statistical analysis for a class of 20...

    Write a C++ program to keep records and perform statistical analysis for a class of 20 students. The information of each student contains ID, Name, Sex, quizzes Scores (2 quizzes per semester), mid-term score, final score, and total score. The program will prompt the user to choose the operation of records from a menu as shown below: ==============================================                                            MENU =============================================== 1. Add student records 2. Delete student records 3. Update student records 4. View all student records 5. Calculate...

  • in JAVA please please include a main() and show output!! Create a GUI program shown below....

    in JAVA please please include a main() and show output!! Create a GUI program shown below. User enters information through the first window. When “Close” button is clicked, your program stops; when “Show Info” button is clicked, the input information is displayed on the TextArea; while when “Modify” button is clicked, the second window pops up and the information user enters to the first window automatically fills in the second window. User can change the information on the second window....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT