VERSION THREE. PRETTY MUCH DONE /* * * OOP practice. Dogs and working dogs */ package ooppracticalexercise; public class OOPPracticeUI { public static void main(String[]args) { String allRecords = null; DogManager dm = new DogManager("dog.txt"); allRecords = dm.displayAll(); System.out.println(allRecords); System.out.println("OOP"); } } ================================================================= /* * OOP practice. Dogs and working dogs */ package ooppracticalexercise; import java.io.File; import java.io.FileNotFoundException; import java.time.LocalDate; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class DogManager { private String name = null; private String dobSt = null; private LocalDate dob = null; private String breed = null; private String pedigreeSt = null; private boolean pedigree = false; private int height = 0; private int experience = 0; private String area = null; private String trainer = null; private Dog[] dogArr = new Dog[20]; int counter = 0; public DogManager(String t) { try { String textfile = t; Scanner scFile = new Scanner(new File(textfile)); while(scFile.hasNext()) { String line = scFile.nextLine(); Scanner scLine = new Scanner(line).useDelimiter("#"); name = scLine.next(); dobSt = scLine.next(); dob = LocalDate.parse(dobSt); breed = scLine.next(); pedigreeSt = scLine.next(); if(pedigreeSt.equals("yes")) pedigree = true; else if(pedigreeSt.equals("no")) pedigree = false; else { System.out.println("Error with pedigree"); System.exit(0); } height = scLine.nextInt(); if(scLine.hasNext()){ experience = scLine.nextInt(); area = scLine.next(); trainer = scLine.next(); dogArr[counter] = new WorkingDog(name, dob, breed, pedigree, height, experience, area, trainer); } else { dogArr[counter] = new Dog(name, dob, breed, pedigree, height); } counter++; } // end while sortString(); // no parameters or return type necessary System.out.println("manager constructor"); } catch (FileNotFoundException ex) { Logger.getLogger(DogManager.class.getName()).log(Level.SEVERE, null, ex); System.out.println("File not found"); } } // end constructor public String displayAll() { String theString = ""; for(int i = 0; i < counter; i++) { theString = theString + dogArr[i]; } // end for return theString; } // end displayAll private void sortString() { int count = counter; Dog temp = null; for(int i = 0; i < count - 1; i++) { for(int j = i + 1; j < count; j++) { if(dogArr[i].getName().compareTo(dogArr[j].getName()) > 0) { // swap around temp = dogArr[i]; dogArr[i] = dogArr[j]; dogArr[j] = temp; } // end if } // end inner loop } // end outer loop } // end sortString } // end class =================================================================================== /* * * OOP practice. Dogs and working dogs */ package ooppracticalexercise; import java.time.LocalDate; import java.time.Period; public class Dog { private String name; private LocalDate dob; private String breed; private boolean pedigree; private int height; private int age; public Dog(String n, LocalDate db, String b, boolean p, int h) { name = n; dob = db; breed = b; pedigree = p; height = h; } public String getName() { return name; } // end getName public int getAge() { //Period is a class (and therefore a datatype) used for years, // and months and days. Age is a derived dynamic value calculated // on the day the program is run. Therefore the value is not stored LocalDate current = LocalDate.now(); Period period = Period.between(dob, current); int theYears = period.getYears(); return theYears; // the question requires a return type of int } public String toString() { String theString = null; // the private helper method getAge is called here theString = "\n\n" + "Name: " + name + "\t" + getAge() + "\t" + breed + "\n" + "Pedigree: " + pedigree + "\t" + height; return theString; } // end toString } // end class =============================================================================================== /* * * OOP practice. Dogs and working dogs */ package ooppracticalexercise; import java.time.LocalDate; public class WorkingDog extends Dog{ private int experience; private String area; private String trainer; public WorkingDog(String n, LocalDate db, String b, boolean p, int h, int e, String a, String t) { super(n,db,b,p, h); experience = e; area = a; trainer = t; } // end constructor public String toString(){ String theString = null; theString = super.toString() + "\t" + trainer; return theString; } // end toString } // end class