SlideShare a Scribd company logo
1 of 7
Download to read offline
JAVA:
Add to the code at the bottom to do the following two things
1. A "Monitoring.java" class that contains a “main(…)” method that will instantiate a
“animalsHabitat” object and call the methods in it.
2. Proper comments and formatting in this .java file and the one that you will create.
PROMPT:
Create a program for a zookeeper monitoring system using two classes. One to be used to
determine what the user wants to do, monitor animals or habitats. The other to read the file for
the chosen action (listed below as animal/habitat file). Create a monitoring system that does all
of the following:
1) Asks a user if they want to monitor an animal, monitor a habitat, or exit
2)Displays a list of animal/habitat options (based on the previous selection) as read from either
the animals or habitats file o Asks the user to enter one of the options
3) Displays the monitoring information by finding the appropriate section in the file
4) Separates sections by the category and selection (such as “Animal - Lion” or “Habitat -
Penguin”)
5) Uses a dialog box to alert the zookeeper if the monitor detects something out of the normal
range (These will be denoted in the files by a new line starting with *****. Do not display the
asterisks in the dialog.)
6) Allows a user to return to the original options
Animals file: (saved on local)
Details on lions
Details on tigers
Details on bears
Details on giraffes
Animal - Lion
Name: Leo
Age: 5
*****Health concerns: Cut on left front paw
Feeding schedule: Twice daily
Animal - Tiger
Name: Maj
Age: 15
Health concerns: None
Feeding schedule: 3x daily
Animal - Bear
Name: Baloo
Age: 1
Health concerns: None
*****Feeding schedule: None on record
Animal - Giraffe
Name: Spots
Age: 12
Health concerns: None
Feeding schedule: Grazing
Habitat file: (Saved on local)
Details on penguin habitat
Details on bird house
Details on aquarium
Habitat - Penguin
Temperature: Freezing
*****Food source: Fish in water running low
Cleanliness: Passed
Habitat - Bird
Temperature: Moderate
Food source: Natural from environment
Cleanliness: Passed
Habitat - Aquarium
Temperature: Varies with output temperature
Food source: Added daily
*****Cleanliness: Needs cleaning from algae
Code so far:
package monitoring;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class animalsHabitat {
private String filePath;
final private Scanner scnr;
public animalsHabitat() {
filePath = "C:mylocalfolder";
scnr = new Scanner(System.in);
}
public void askForWhichDetails(String fileName) throws IOException {
FileInputStream fileByteStream = null; // File input stream
Scanner inFS = null; // Scanner object
String textLine = null;
ArrayList aList1 = new ArrayList();
int i = 0;
int option = 0;
boolean bailOut = false;
// Try to open file
fileByteStream = new FileInputStream(filePath + fileName + ".txt");
inFS = new Scanner(fileByteStream);
while (inFS.hasNextLine() && bailOut == false) {
textLine = inFS.nextLine();
if (textLine.contains("Details")) {
i += 1;
System.out.println(i + ". " + textLine);
ArrayList aList2 = new ArrayList();
for (String retval : textLine.split(" ")) {
aList2.add(retval);
}
String str = aList2.remove(2).toString();
aList1.add(str);
} else {
System.out.print("Enter selection: ");
option = scnr.nextInt();
System.out.println("");
if (option <= i) {
String detailOption = aList1.remove(option - 1).toString();
showData(fileName, detailOption);
bailOut = true;
}
break;
}
}
// Done with file, so try to close it
fileByteStream.close(); // close() may throw IOException if fails
}
public void showData(String fileName, String detailOption) throws IOException {
FileInputStream fileByteStream = null; // File input stream
Scanner inFS = null; // Scanner object
String textLine = null;
String lcTextLine = null;
int lcStr1Len = fileName.length();
String lcStr1 = fileName.toLowerCase().substring(0, lcStr1Len - 1);
int lcStr2Len = detailOption.length();
String lcStr2 = detailOption.toLowerCase().substring(0, lcStr2Len - 1);
boolean bailOut = false;
// Try to open file
fileByteStream = new FileInputStream(filePath + fileName + ".txt");
inFS = new Scanner(fileByteStream);
while (inFS.hasNextLine() && bailOut == false) {
textLine = inFS.nextLine();
lcTextLine = textLine.toLowerCase();
if (lcTextLine.contains(lcStr1) && lcTextLine.contains(lcStr2)) {
do {
System.out.println(textLine);
textLine = inFS.nextLine();
if (textLine.isEmpty()) {
bailOut = true;
}
} while (inFS.hasNextLine() && bailOut == false);
}
}
// Done with file, so try to close it
fileByteStream.close(); // close() may throw IOException if fails
}
}
Solution
// main Method
public class Monitoring
{
public static void main(String a[])
{
try
{
animalsHabitat zoo = new animalsHabitat(); // Create object of anumalshabitat
zoo.askForWhichDetails("animals"); // Call askForWhichDetails() method for animals file
zoo.askForWhichDetails("habitat"); //Call askForWhichDetails() method for habitat file
zoo.showData("habitat", "bird"); // Call showData() method to display data in habitat file
}
catch(Exception e)
{
System.out.println(e);
}
}
}
package monitoring;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class animalsHabitat {
private String filePath;
final private Scanner scnr;
public animalsHabitat() {
filePath = "C:mylocalfolder";
scnr = new Scanner(System.in);
}
public void askForWhichDetails(String fileName) throws IOException {
FileInputStream fileByteStream = null; // File input stream
Scanner inFS = null; // Scanner object
String textLine = null;
ArrayList aList1 = new ArrayList(); // Arraylist to hold details of each animal or habitat
int i = 0;
int option = 0;
boolean bailOut = false;
// Try to open file
fileByteStream = new FileInputStream(filePath + fileName + ".txt");
inFS = new Scanner(fileByteStream);
while (inFS.hasNextLine() && bailOut == false) { // Read data from file line by line
textLine = inFS.nextLine();
if (textLine.contains("Details")) {
i += 1;
System.out.println(i + ". " + textLine);
ArrayList aList2 = new ArrayList();
for (String retval : textLine.split(" ")) { // add each line to array list
aList2.add(retval);
}
String str = aList2.remove(2).toString();
aList1.add(str);
} else {
System.out.print("Enter selection: ");
option = scnr.nextInt();
System.out.println("");
if (option <= i) {
String detailOption = aList1.remove(option - 1).toString();
showData(fileName, detailOption);
bailOut = true;
}
break;
}
}
// Done with file, so try to close it
fileByteStream.close(); // close() may throw IOException if fails
}
public void showData(String fileName, String detailOption) throws IOException {
FileInputStream fileByteStream = null; // File input stream
Scanner inFS = null; // Scanner object
String textLine = null;
String lcTextLine = null;
int lcStr1Len = fileName.length();
String lcStr1 = fileName.toLowerCase().substring(0, lcStr1Len - 1); // convert file name to
lower case
int lcStr2Len = detailOption.length();
String lcStr2 = detailOption.toLowerCase().substring(0, lcStr2Len - 1);
boolean bailOut = false;
// Try to open file
fileByteStream = new FileInputStream(filePath + fileName + ".txt"); // open file
inFS = new Scanner(fileByteStream);
while (inFS.hasNextLine() && bailOut == false) { // read data line by line from file
textLine = inFS.nextLine();
lcTextLine = textLine.toLowerCase();
if (lcTextLine.contains(lcStr1) && lcTextLine.contains(lcStr2)) { // check if this line contains
detailOption
do {
System.out.println(textLine);
textLine = inFS.nextLine();
if (textLine.isEmpty()) {
bailOut = true;
}
} while (inFS.hasNextLine() && bailOut == false);
}
}
// Done with file, so try to close it
fileByteStream.close(); // close() may throw IOException if fails
}
}

More Related Content

Similar to JAVAAdd to the code at the bottom to do the following two things.pdf

C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT III
Minu Rajasekaran
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
theodorelove43763
 

Similar to JAVAAdd to the code at the bottom to do the following two things.pdf (20)

CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Application-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageApplication-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta Language
 
I need some help creating Psuedocode for a project using Java. Basic.pdf
I need some help creating Psuedocode for a project using Java. Basic.pdfI need some help creating Psuedocode for a project using Java. Basic.pdf
I need some help creating Psuedocode for a project using Java. Basic.pdf
 
Biopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and OutlookBiopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and Outlook
 
Inheritance
InheritanceInheritance
Inheritance
 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testability
 
Files in c
Files in cFiles in c
Files in c
 
Monitoring System As a zookeeper, is important to it know the activi.pdf
Monitoring System As a zookeeper, is important to it know the activi.pdfMonitoring System As a zookeeper, is important to it know the activi.pdf
Monitoring System As a zookeeper, is important to it know the activi.pdf
 
C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT III
 
ExtraFileIO.pptx
ExtraFileIO.pptxExtraFileIO.pptx
ExtraFileIO.pptx
 
File handling
File handlingFile handling
File handling
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
 
source code which create file and write into it
source code which create file and write into itsource code which create file and write into it
source code which create file and write into it
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4
 

More from fasttracksunglass

I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdfI have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
fasttracksunglass
 
40.Classification of a network as a LAN or a WAN is not relevant to .pdf
40.Classification of a network as a LAN or a WAN is not relevant to .pdf40.Classification of a network as a LAN or a WAN is not relevant to .pdf
40.Classification of a network as a LAN or a WAN is not relevant to .pdf
fasttracksunglass
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
fasttracksunglass
 
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdfPlease answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
fasttracksunglass
 
Low platelet count it a recessively inherited trait. Reevaluation of .pdf
Low platelet count it a recessively inherited trait. Reevaluation of .pdfLow platelet count it a recessively inherited trait. Reevaluation of .pdf
Low platelet count it a recessively inherited trait. Reevaluation of .pdf
fasttracksunglass
 
Match the modified stem to its correct definition.StolonRhizome.pdf
Match the modified stem to its correct definition.StolonRhizome.pdfMatch the modified stem to its correct definition.StolonRhizome.pdf
Match the modified stem to its correct definition.StolonRhizome.pdf
fasttracksunglass
 
Choose the best possible answer for each of the following multiple ch.pdf
Choose the best possible answer for each of the following multiple ch.pdfChoose the best possible answer for each of the following multiple ch.pdf
Choose the best possible answer for each of the following multiple ch.pdf
fasttracksunglass
 

More from fasttracksunglass (14)

I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdfI have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
I have been working on this ROCK, PAPER, SCISSORS project for the pa.pdf
 
Describe tolerance as a immunologic function. What is the consequenc.pdf
Describe tolerance as a immunologic function. What is the consequenc.pdfDescribe tolerance as a immunologic function. What is the consequenc.pdf
Describe tolerance as a immunologic function. What is the consequenc.pdf
 
Answer the following question about human evolution as inferred from.pdf
Answer the following question about human evolution as inferred from.pdfAnswer the following question about human evolution as inferred from.pdf
Answer the following question about human evolution as inferred from.pdf
 
40.Classification of a network as a LAN or a WAN is not relevant to .pdf
40.Classification of a network as a LAN or a WAN is not relevant to .pdf40.Classification of a network as a LAN or a WAN is not relevant to .pdf
40.Classification of a network as a LAN or a WAN is not relevant to .pdf
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
 
A sterile item is free ofmicrobes.endospores.viruses.prions..pdf
A sterile item is free ofmicrobes.endospores.viruses.prions..pdfA sterile item is free ofmicrobes.endospores.viruses.prions..pdf
A sterile item is free ofmicrobes.endospores.viruses.prions..pdf
 
Both mitochondria and chloroplasts... A. obtain electrons from water .pdf
Both mitochondria and chloroplasts... A. obtain electrons from water .pdfBoth mitochondria and chloroplasts... A. obtain electrons from water .pdf
Both mitochondria and chloroplasts... A. obtain electrons from water .pdf
 
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdfPlease answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
Please answer all parts thank you!Parents PrPr YY rr x   PrPr.pdf
 
Low platelet count it a recessively inherited trait. Reevaluation of .pdf
Low platelet count it a recessively inherited trait. Reevaluation of .pdfLow platelet count it a recessively inherited trait. Reevaluation of .pdf
Low platelet count it a recessively inherited trait. Reevaluation of .pdf
 
Lists can contain other lists as elements. For example the list HAIR.pdf
Lists can contain other lists as elements. For example the list HAIR.pdfLists can contain other lists as elements. For example the list HAIR.pdf
Lists can contain other lists as elements. For example the list HAIR.pdf
 
Match the modified stem to its correct definition.StolonRhizome.pdf
Match the modified stem to its correct definition.StolonRhizome.pdfMatch the modified stem to its correct definition.StolonRhizome.pdf
Match the modified stem to its correct definition.StolonRhizome.pdf
 
Let (X,T) be a topological space,.A subset X. Suppose that for all x .pdf
Let (X,T) be a topological space,.A subset X. Suppose that for all x .pdfLet (X,T) be a topological space,.A subset X. Suppose that for all x .pdf
Let (X,T) be a topological space,.A subset X. Suppose that for all x .pdf
 
Choose the best possible answer for each of the following multiple ch.pdf
Choose the best possible answer for each of the following multiple ch.pdfChoose the best possible answer for each of the following multiple ch.pdf
Choose the best possible answer for each of the following multiple ch.pdf
 
If T is a bounded and self-adjoint operator on a Hilbert space and T_.pdf
If T is a bounded and self-adjoint operator on a Hilbert space and T_.pdfIf T is a bounded and self-adjoint operator on a Hilbert space and T_.pdf
If T is a bounded and self-adjoint operator on a Hilbert space and T_.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 

JAVAAdd to the code at the bottom to do the following two things.pdf

  • 1. JAVA: Add to the code at the bottom to do the following two things 1. A "Monitoring.java" class that contains a “main(…)” method that will instantiate a “animalsHabitat” object and call the methods in it. 2. Proper comments and formatting in this .java file and the one that you will create. PROMPT: Create a program for a zookeeper monitoring system using two classes. One to be used to determine what the user wants to do, monitor animals or habitats. The other to read the file for the chosen action (listed below as animal/habitat file). Create a monitoring system that does all of the following: 1) Asks a user if they want to monitor an animal, monitor a habitat, or exit 2)Displays a list of animal/habitat options (based on the previous selection) as read from either the animals or habitats file o Asks the user to enter one of the options 3) Displays the monitoring information by finding the appropriate section in the file 4) Separates sections by the category and selection (such as “Animal - Lion” or “Habitat - Penguin”) 5) Uses a dialog box to alert the zookeeper if the monitor detects something out of the normal range (These will be denoted in the files by a new line starting with *****. Do not display the asterisks in the dialog.) 6) Allows a user to return to the original options Animals file: (saved on local) Details on lions Details on tigers Details on bears Details on giraffes Animal - Lion Name: Leo Age: 5 *****Health concerns: Cut on left front paw Feeding schedule: Twice daily Animal - Tiger Name: Maj Age: 15 Health concerns: None Feeding schedule: 3x daily
  • 2. Animal - Bear Name: Baloo Age: 1 Health concerns: None *****Feeding schedule: None on record Animal - Giraffe Name: Spots Age: 12 Health concerns: None Feeding schedule: Grazing Habitat file: (Saved on local) Details on penguin habitat Details on bird house Details on aquarium Habitat - Penguin Temperature: Freezing *****Food source: Fish in water running low Cleanliness: Passed Habitat - Bird Temperature: Moderate Food source: Natural from environment Cleanliness: Passed Habitat - Aquarium Temperature: Varies with output temperature Food source: Added daily *****Cleanliness: Needs cleaning from algae Code so far: package monitoring; import java.io.FileInputStream; import java.io.IOException; import java.util.*; public class animalsHabitat { private String filePath; final private Scanner scnr; public animalsHabitat() { filePath = "C:mylocalfolder";
  • 3. scnr = new Scanner(System.in); } public void askForWhichDetails(String fileName) throws IOException { FileInputStream fileByteStream = null; // File input stream Scanner inFS = null; // Scanner object String textLine = null; ArrayList aList1 = new ArrayList(); int i = 0; int option = 0; boolean bailOut = false; // Try to open file fileByteStream = new FileInputStream(filePath + fileName + ".txt"); inFS = new Scanner(fileByteStream); while (inFS.hasNextLine() && bailOut == false) { textLine = inFS.nextLine(); if (textLine.contains("Details")) { i += 1; System.out.println(i + ". " + textLine); ArrayList aList2 = new ArrayList(); for (String retval : textLine.split(" ")) { aList2.add(retval); } String str = aList2.remove(2).toString(); aList1.add(str); } else { System.out.print("Enter selection: "); option = scnr.nextInt(); System.out.println(""); if (option <= i) { String detailOption = aList1.remove(option - 1).toString(); showData(fileName, detailOption); bailOut = true; } break; } }
  • 4. // Done with file, so try to close it fileByteStream.close(); // close() may throw IOException if fails } public void showData(String fileName, String detailOption) throws IOException { FileInputStream fileByteStream = null; // File input stream Scanner inFS = null; // Scanner object String textLine = null; String lcTextLine = null; int lcStr1Len = fileName.length(); String lcStr1 = fileName.toLowerCase().substring(0, lcStr1Len - 1); int lcStr2Len = detailOption.length(); String lcStr2 = detailOption.toLowerCase().substring(0, lcStr2Len - 1); boolean bailOut = false; // Try to open file fileByteStream = new FileInputStream(filePath + fileName + ".txt"); inFS = new Scanner(fileByteStream); while (inFS.hasNextLine() && bailOut == false) { textLine = inFS.nextLine(); lcTextLine = textLine.toLowerCase(); if (lcTextLine.contains(lcStr1) && lcTextLine.contains(lcStr2)) { do { System.out.println(textLine); textLine = inFS.nextLine(); if (textLine.isEmpty()) { bailOut = true; } } while (inFS.hasNextLine() && bailOut == false); } } // Done with file, so try to close it fileByteStream.close(); // close() may throw IOException if fails } } Solution
  • 5. // main Method public class Monitoring { public static void main(String a[]) { try { animalsHabitat zoo = new animalsHabitat(); // Create object of anumalshabitat zoo.askForWhichDetails("animals"); // Call askForWhichDetails() method for animals file zoo.askForWhichDetails("habitat"); //Call askForWhichDetails() method for habitat file zoo.showData("habitat", "bird"); // Call showData() method to display data in habitat file } catch(Exception e) { System.out.println(e); } } } package monitoring; import java.io.FileInputStream; import java.io.IOException; import java.util.*; public class animalsHabitat { private String filePath; final private Scanner scnr; public animalsHabitat() { filePath = "C:mylocalfolder"; scnr = new Scanner(System.in); } public void askForWhichDetails(String fileName) throws IOException { FileInputStream fileByteStream = null; // File input stream Scanner inFS = null; // Scanner object String textLine = null; ArrayList aList1 = new ArrayList(); // Arraylist to hold details of each animal or habitat
  • 6. int i = 0; int option = 0; boolean bailOut = false; // Try to open file fileByteStream = new FileInputStream(filePath + fileName + ".txt"); inFS = new Scanner(fileByteStream); while (inFS.hasNextLine() && bailOut == false) { // Read data from file line by line textLine = inFS.nextLine(); if (textLine.contains("Details")) { i += 1; System.out.println(i + ". " + textLine); ArrayList aList2 = new ArrayList(); for (String retval : textLine.split(" ")) { // add each line to array list aList2.add(retval); } String str = aList2.remove(2).toString(); aList1.add(str); } else { System.out.print("Enter selection: "); option = scnr.nextInt(); System.out.println(""); if (option <= i) { String detailOption = aList1.remove(option - 1).toString(); showData(fileName, detailOption); bailOut = true; } break; } } // Done with file, so try to close it fileByteStream.close(); // close() may throw IOException if fails } public void showData(String fileName, String detailOption) throws IOException { FileInputStream fileByteStream = null; // File input stream Scanner inFS = null; // Scanner object String textLine = null;
  • 7. String lcTextLine = null; int lcStr1Len = fileName.length(); String lcStr1 = fileName.toLowerCase().substring(0, lcStr1Len - 1); // convert file name to lower case int lcStr2Len = detailOption.length(); String lcStr2 = detailOption.toLowerCase().substring(0, lcStr2Len - 1); boolean bailOut = false; // Try to open file fileByteStream = new FileInputStream(filePath + fileName + ".txt"); // open file inFS = new Scanner(fileByteStream); while (inFS.hasNextLine() && bailOut == false) { // read data line by line from file textLine = inFS.nextLine(); lcTextLine = textLine.toLowerCase(); if (lcTextLine.contains(lcStr1) && lcTextLine.contains(lcStr2)) { // check if this line contains detailOption do { System.out.println(textLine); textLine = inFS.nextLine(); if (textLine.isEmpty()) { bailOut = true; } } while (inFS.hasNextLine() && bailOut == false); } } // Done with file, so try to close it fileByteStream.close(); // close() may throw IOException if fails } }