// steve eilertsen // Reading from a text file // Creates an array of Dog objects // Working Dog inherits from Dog class // Age is determined using the date of birth package dogsdate; public class DogsUI { public static void main(String[] args) { DogManager dm = new DogManager (); System.out.println(dm.printAll() ); System.out.println("Max age dog " + Dogs.maxAge); System.out.println("Min age working " + WorkingDogs.minAge); System.out.println(dm.testAge()); dm.determineClass(); } } ================================================================== // steve eilertsen // Reading from a text file // Creates an array of Dog objects // Working Dog inherits from Dog class // Age is determined using the date of birth // Dogs in the array must be older than 2 and // younger that 10 years package dogsdate; import java.io.File; import java.io.FileNotFoundException; import java.time.LocalDate; import java.time.Period; import java.time.format.DateTimeFormatter; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; public class DogManager { // global variables private Dogs[] dogArray = new Dogs[10]; private int counter = 0; public DogManager(){ // local variables String[] tokenArray = null; String nameSt = null, dobSt = null, pedigreeSt = null; String heightSt = null,trainerSt = null; int height = 0, age = 0; boolean pedigree = false; try { Scanner scFile = new Scanner(new File("dogs.txt")); while(scFile.hasNextLine()) { String line = scFile.nextLine(); tokenArray = line.split("#"); nameSt = tokenArray[0]; dobSt = tokenArray[1]; heightSt = tokenArray[2]; pedigreeSt = tokenArray[3]; // parse String to final datatypes LocalDate dob = LocalDate.parse(dobSt, DateTimeFormatter.ofPattern("dd-MM-yyyy")); height = Integer.parseInt(heightSt); if(pedigreeSt.equals("true")) pedigree = true; else if (pedigreeSt.equals("false")) pedigree = false; else { System.out.println("Error with pedigree"); System.exit(0); } // calculate age from dob LocalDate today = LocalDate.now(); Period dobP = Period.between(dob, today); age = dobP.getYears(); // create Dog or Working Dog objects if(tokenArray.length == 5) { // test for valid ages of dogs if(age > WorkingDogs.minAge && age < Dogs.maxAge) { trainerSt = tokenArray[4]; WorkingDogs oneWorkingDog = new WorkingDogs (nameSt, age, height, pedigree, trainerSt); dogArray[counter] = oneWorkingDog; counter++; } } else if (tokenArray.length == 4) { // test for valid ages if(age < Dogs.maxAge) { Dogs oneDog = new Dogs(nameSt, age, height, pedigree); dogArray[counter] = oneDog; counter++; } } else { System.out.println("Error with text file"); System.exit(0); } } } catch (FileNotFoundException ex) { Logger.getLogger(DogManager.class.getName()).log(Level.SEVERE, null, ex); System.out.println("File not found"); } } // end constructor public String printAll() { String all = ""; for (int x = 0; x < counter;x++) { all = all + dogArray[x] + "\n"; } return all; } // test to see if all dogs fit into the age window public String testAge() { boolean flag1 = false, flag2 = false; String testResults = ""; for(int x = 0; x < counter; x++) { if(dogArray[x].getAge() < Dogs.maxAge) flag1 = true; if(dogArray[x].getAge() > WorkingDogs.minAge) flag2 = true; testResults = testResults + flag1 + " " + flag2 + "\n"; // reset the boolean flags flag1 = false; flag2 = false; } return testResults; } // end testAge // Test to see if a specific dog is a workind dog or not public void determineClass() { boolean flag = false; String theDogName = JOptionPane.showInputDialog(null, "Name the dog. Use uppercase only"); theDogName = theDogName.toUpperCase(); for(int x = 0; x < counter;x++) { String dogName = dogArray[x].getName(); dogName = dogName.toUpperCase(); System.out.println(""); if(dogName.equals(theDogName)) if(dogArray[x] instanceof Dogs) { flag = true; } } System.out.println(theDogName + " is a working dog: " + flag); } } ================================================================================ // steve eilertsen // The Dog class - the parent class package dogsdate; public class Dogs { private String name = null; private int age = 0; private int height = 0; private boolean pedigree = false; // Dogs may not be older than 10 years protected static int maxAge = 10; public Dogs(String n, int a, int h, boolean p){ name = n; age = a; height = h; pedigree = p; } // end constructor public String toString(){ return name + " " + age + " " + pedigree; } public int getAge(){ return age; } public String getName(){ return name; } } ================================================================================= // steve eilertsen // The Working Dog class - the child class package dogsdate; public class WorkingDogs extends Dogs{ private String trainer = null; // Working dogs may not be younger than 2 years protected static int minAge = 2; public WorkingDogs(String n, int a, int h,boolean p, String t){ super(n, a, h, p); trainer = t; } // end constructor public String toString() { return super.toString() + " " + trainer; } public void getMaxAge() { System.out.println("Max age " + Dogs.maxAge); } } ========================================================================= TEXT FILES TEXT FILES Shaka#1#true Max#3#false#Ivan Mervin#5#false Bingo#11#true Rusta#3#false#Omphile Ivan#4#true Shaka#04-12-2022#28#true Max#04-12-2025#35#false#Ivan#2005/02/17 Mervin#04-05-2022#34#false Bingo#24-01-2020#31#true Rusta#30-07-2019#42#false#Omphile#2002/10/15 Ivan#04-12-2015#40#true Shaka#04-12-2022#28#true Max#04-12-2024#35#false#Ivan Mervin#04-05-2022#34#false Bingo#24-01-2020#31#true Rusta#30-07-2019#42#false#Omphile Ivan#04-12-2015#40#true