Question: Modify the solution below so that your program will reads from pets.dat using DataInputStream and write to pets.dat upon exit using DataOutputStream. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Solution code:

Modify the solution below so that your program will reads from pets.dat using DataInputStream and write to pets.dat upon exit using DataOutputStream.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Solution code:

package petClinic; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner;

public class PetClinicApp {

/** * * Method:main() * * This method is the starting point of the program. It contains the initial * reading of items from the text file Pet.txt and a menu for allowing user * to choose various tasks. * * * @param args */ public static void main(String[] args) { ClientManager myClientManager = new ClientManager();

/* TODO 1: * open text file "pets.txt" and create a Scanner from this text file. */ File petFile = new File("pets.txt"); // open a Scanner to read data from File Scanner petReader = null; try { petReader = new Scanner(petFile); } catch (FileNotFoundException e) {

System.out.println("No ClientList Pet found- ClientList is empty");

}

/* TODO 2: * Read one pet at a time. Read common attributes first. Then based on * pet type, read remaining attributes. Create the proper pet objects. * * Give each pet object to the myClientManager. */ while (petReader != null && petReader.hasNext()) {

String category = petReader.nextLine(); String name = petReader.nextLine(); String ownerName = petReader.nextLine(); String ageString = petReader.nextLine(); int age = Integer.parseInt(ageString);

if (category.equalsIgnoreCase("Other")) { String temp = petReader.nextLine(); boolean handling = Boolean.parseBoolean(temp); String type = petReader.nextLine(); Other tp = new Other(category, name, ownerName,age, handling, type); myClientManager.addItem(tp);

} else if (category.equalsIgnoreCase("Cat")) { String temp = petReader.nextLine(); boolean declawed = Boolean.parseBoolean(temp); temp = petReader.nextLine(); boolean longHair = Boolean.parseBoolean(temp); String color = petReader.nextLine();

Cat tp = new Cat(category, name, ownerName,age, longHair, declawed, color); myClientManager.addItem(tp);

} else if (category.equalsIgnoreCase("Dog")) { String breed = petReader.nextLine(); String color = petReader.nextLine(); Dog tp = new Dog(category, name, ownerName,age,breed, color); myClientManager.addItem(tp); } else { System.out.println("Unknown Pet type " + category); }

}

//Create a Scanner to read user inputs Scanner keyboard = new Scanner(System.in);

System.out.println("Welcome to the Client List"); boolean done = false; while (!done) { //Print the menu System.out.println("Would you like to :"); System.out.println(" 1. View contents of Client List"); System.out.println(" 2. Search for a pet"); System.out.println(" 3. Remove a pet"); System.out.println(" 4. Exit");

/* TODO 3: * Read user input and based on the input perform proper actions. * If user enters 4, break the loop. */ String tp = keyboard.nextLine(); int userInput = Integer.parseInt(tp); if (userInput == 1) { myClientManager.printClientListItems(); } else if (userInput == 2) { System.out.println("Please enter pet name"); String id = keyboard.nextLine(); Pet item = myClientManager.searchByName(id); if (item != null) { System.out.println(item); } else { System.out.println("Sorry- item not found"); } } else if (userInput == 3) { System.out.println("Please enter pet name"); String id2 = keyboard.nextLine(); myClientManager.removeItem(id2); } else { done = true; } } //close the scanner for user inputs keyboard.close();

/* TODO 4: * Create a PrintWriter for the original file "pets.txt". * We will write out contents of ClientManager back to original file */ PrintWriter out = null; // open file for writing try { out = new PrintWriter(new File("pets.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }

/* TODO 5: * Write contents of each pet item in ClientManager to file */ for (Pet g : myClientManager.getClientListItems()) { // first write the attributes common to all out.println(g.getType()); out.println(g.getName()); out.println(g.getOwnerName() ); out.println(g.getAge() );

if (g.getType().equalsIgnoreCase("Other")) { // cast to appropriate subclass Other p = (Other) g; out.println(p.isSpecialHandling() ); out.println(p.getPetType() );

} else if (g.getType().equalsIgnoreCase("Cat")) { // cast to appropriate subclass Cat c = (Cat) g; // write attributes specific to Cat out.println(c.isClawed()); out.println(c.isLongHair()); out.println(c.getHairColor());

} else if (g.getType().equalsIgnoreCase("Dog")) { Dog m = (Dog) g; out.println(m.getBreed()); out.println(m.getHairColor()); } else { System.out.println("Unknown Pet type " + g.getType()); } } //close the print writer out.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!