Question: package ExampleHW1_Starter.petClinic; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.attribute.AclFileAttributeView; import java.util.Scanner; /** * Class: PetClinicApp * * @author Dr. Johnson * @version
package ExampleHW1_Starter.petClinic; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.attribute.AclFileAttributeView; import java.util.Scanner; /** * Class: PetClinicApp * * @author Dr. Johnson * @version 1.0 Course : ITEC 3150, Fall, 2015 Written: January 18, 2012 * @author Dr. Jin * @version 1.2 Course : ITEC 3150, Spring, 2021 Written: January 26, 2021 * * This class contains the main method which starts the application that * maintains the pet client list of a pet clinic. * * Purpose: Read pets' info from a file, perform operations according to a menu, * update the file with updated pets' info. */ 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("src/pets.txt"); Scanner petReader = null; try { petReader = new Scanner(petFile); } catch (IOException ex) { System.out.println("file not found"); System.exit(0); } /* 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.hasNextLine()) { String type = petReader.nextLine(); String name = petReader.nextLine(); String ownerName = petReader.nextLine(); String ageStr = petReader.nextLine(); int age = Integer.parseInt(ageStr); // add attributes based on the pet type if(type.equalsIgnoreCase("cat") ) { String longHairStr = petReader.nextLine(); boolean longHair = Boolean.parseBoolean(longHairStr); String clawedStr = petReader.nextLine(); boolean clawed = Boolean.parseBoolean(clawedStr); String hairColor = petReader.nextLine(); Cat cat = new Cat(type, name, ownerName,age ,longHair, clawed,hairColor ); } else if(type.equalsIgnoreCase("dog")) { String breed = petReader.nextLine(); String hairColor = petReader.nextLine(); Dog dog = new Dog(type, name, ownerName, age, breed, hairColor); } else if(type.equalsIgnoreCase("other")) { String isSpecialHandlingStr = petReader.nextLine(); boolean isSpecialHandling = Boolean.parseBoolean(isSpecialHandlingStr); String getPetType = petReader.nextLine(); Other other = new Other(type, name, ownerName, age, isSpecialHandling, getPetType); } else { System.out.println("wrong type"); System.exit(0); } } //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. */ int choice = Integer.parseInt(keyboard.nextLine()); if (choice == 4) { keyboard.close(); } else if (choice == 1) { myClientManager.printClientListItems(); } else if (choice == 2) { System.out.println("enter pet name"); String sNAme = keyboard.nextLine(); myClientManager.searchByName(sNAme); } else if (choice == 3) { System.out.println("enter pet name to remove"); String bNAme = keyboard.nextLine(); myClientManager.removeItem(bNAme); } else { System.out.println("invaild entry"); } } //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; try { out = new PrintWriter("pet.txt"); } catch (IOException ex) { System.out.println("unable to open file to write program"); System.exit(0); } /* 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() ); String type = g.getType(); if (type.equalsIgnoreCase("Other")) { // cast to appropriate subclass Other p = (Other) g; out.println(p.isSpecialHandling() ); out.println(p.getPetType() ); } else if (type.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 (type.equalsIgnoreCase("Dog")) { Dog m = (Dog) g; out.println(m.getBreed()); out.println(m.getHairColor()); } else { System.out.println("Unknown Pet type " + type); } } //close the print writer out.close(); } }
in java code
(1) Using DataOutputStream, write a Java program CreatePets1.java that will generate a binary file pets.dat that contains the same information as attached pets.txt but in binary format. (2) Modify your Example HW1 solution so that your program will reads from pets.dat using DataInputStream and write to pets.dat upon exit using DataOutputStream.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
