Question

In JAVA, In two classes: As a zookeeper, it is important to know the activities of...

In JAVA, In two classes:

As a zookeeper, it is important to know the activities of the animals in your care and to monitor their living habitats. Create a monitoring system that does all of the following:

-Asks a user if they want to monitor an animal, monitor a habitat, or exit

-Displays a list of animal/habitat options (based on the previous selection) as read from either the animals or habitats file and asks the user to enter one of the options

-Displays the monitoring information by finding the appropriate section in the file

-Separates sections by the category and selection (such as “Animal - Lion” or “Habitat - Penguin”)

-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.)

-Allows a user to return to the original options

The class that I need assistance in creating is the "Monitoring" class that is menu driven and will create an "AnimalsHabitat" object and call the methods in it.

The AnimalsHabitat file has already been created. The txt files are below.

AnimalsHabitat:

import java.io.FileInputStream;

import java.io.IOException;

import java.util.*;

import javax.swing.JOptionPane;

public class AnimalsHabitat {

private String filePath;

final private Scanner scnr;

public AnimalsHabitat() {

//filePath = "C:\\Users\\mine\\Documents\\monitoring\\";

filePath = "";

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;

// 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;

}

}

// close file

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;

String alertMessage = "*****";

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;

// 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;

}

if (textLine.contains(alertMessage)) {

JOptionPane.showMessageDialog(null, textLine.substring(5));

}

} while (inFS.hasNextLine() && bailOut == false);

}

}

// close file file

ByteStream.close(); // close() may throw IOException if fails

}

}

Animals txt file:

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

Habitats txt file:

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

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

// Please note, your AnimalsHabitat.java file needed few correction, changed that code too.

// Monitoring.java

import java.util.*;

import java.io.*;

public class Monitoring {

public static void main(String args[]) {

// create menu string

String menuStr = "Zoo Monitoring System Menu\n" + "1. Animals\n" + "2. Habitats\n" + "3. Exit\n\n" + "Enter Selection : ";

Scanner in = new Scanner(System.in);

int choice;

String filename = "";

// create an object of AnimalsHabitat

AnimalsHabitat ah = new AnimalsHabitat();

// loop until user selects exit

do {

  System.out.println(menuStr);

  choice = in.nextInt();

  if (choice == 1) {

   filename = "Animals";

   try {

    // call the method to display animal menu and dialog

    ah.askForWhichDetails(filename);

   } catch (IOException e) {

    System.out.println(e);

   }

  } else if (choice == 2) {

   filename = "Habitats";

   try {

    // call the method to display habitat menu and dialog

    ah.askForWhichDetails(filename);

   } catch (IOException e) {

    System.out.println(e);

   }

  }

} while (choice != 3);

}

}

// AnimalHabitat.java

import java.io.FileInputStream;

import java.io.IOException;

import java.util.*;

import javax.swing.JOptionPane;

public class AnimalsHabitat {

private String filePath;

final private Scanner scnr;

public AnimalsHabitat() {

//filePath = "C:\\Users\\mine\\Documents\\monitoring\\";

/* change the filepath according to the file locations */

filePath = "C:\\Users\\dipatil\\workspace\\TestProj\\src\\";

// filePath = "";

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;

// 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;

   }     

  }

}

// close file

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;

String alertMessage = "*****";

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;

// open file

fileByteStream = new FileInputStream(filePath + fileName + ".txt");

inFS = new Scanner(fileByteStream);

while (inFS.hasNextLine() && bailOut == false) {

  textLine = inFS.nextLine();

  lcTextLine = textLine.toLowerCase();

  // Check if this line contains animal/habitat and selected name

  // if yes, then display the content of next line and dialog box if needed

  if (lcTextLine.contains(lcStr1) && lcTextLine.contains(lcStr2) && lcTextLine.contains("-")) {

   System.out.println(textLine);

   textLine = inFS.nextLine();

   if (textLine.isEmpty()) {

    bailOut = true;

   }

   System.out.println(textLine);

   if (textLine.contains(alertMessage)) {

    JOptionPane.showMessageDialog(null, textLine.substring(5));

   }

  }

}

// close file

fileByteStream.close();

// close() may throw IOException if fails

}

}

// Sample output

Zoo Monitoring System Menu

1. Animals

2. Habitats

3. Exit

Enter Selection :

1

1. Details on lions

2. Details on tigers

3. Details on bears

4. Details on giraffes

Enter selection: 1

Animal - Lion

Name: Leo Age: 5 *****Health concerns: Cut on left front paw Feeding schedule: Twice dailyworkspace Java-TestProj/src/AnimalsHabitatjava Eclipse Eile Edit Source Refactor Navigate Search Project Bun Window Help k Ac

Zoo Monitoring System Menu

1. Animals

2. Habitats

3. Exit

Enter Selection :

3


Add a comment
Know the answer?
Add Answer to:
In JAVA, In two classes: As a zookeeper, it is important to know the activities of...
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
  • As a zookeeper, it is important to know the activities of the animals in your care...

    As a zookeeper, it is important to know the activities of the animals in your care and to monitor their living habitats. Crea te a monitoring system that does all of the following:  Asks a user if they want to monitor an animal, monitor a habitat, or exit 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 ...

  • LAB: Zip code and population (generic types)

    13.5 LAB: Zip code and population (generic types)Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main():ArrayList<StatePair> zipCodeState: Contains ZIP code/state abbreviation pairsArrayList<StatePair> abbrevState: Contains state abbreviation/state name pairsArrayList<StatePair> statePopulation: Contains state name/population pairsComplete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to retrieve the state name from the ArrayList abbrevState. Lastly,...

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • Java Netbeans code Option 1: Authentication System For security-minded professionals, it is important that only the...

    Java Netbeans code Option 1: Authentication System For security-minded professionals, it is important that only the appropriate people gain access to data in a computer system. This is called authentication. Once users gain entry, it is also important that they only see data related to their role in a computer system. This is called authorization. For the zoo, you will develop an authentication system that manages both authentication and authorization. You have been given a credentials file that contains credential...

  • I need help ASAP on this, this is due at midnight PST. This is the current...

    I need help ASAP on this, this is due at midnight PST. This is the current code I have. How can I allow the user to quit. My counting while loop works fine, but I would like it to not keep outputting username if a file was successfully opened. This is what is required. Prompt You have assumed the role of managing the technology infrastructure at a zoo. You will develop a working program (either an authentication system or a...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac...

    Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...

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