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

  • Having trouble with a Zoo Monitoring system. Using NetBeans. i have worked out most of the...

    Having trouble with a Zoo Monitoring system. Using NetBeans. i have worked out most of the bugs but now its only printing the first few lines of the text file (it prints the lines that begin with "Details" and not continuing to the rest to find the lines with ***** in them to pop up the Joption pane with the alert for that line. below is the code for both java files, as well as the .txt files its reading...

  • 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,...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • 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;...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

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

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