// steve eilertsen // Reading from a text file // Creates an array of Dog objects // Age is determined using the date of birth // Dogs in the array must be older than 2 and // younger that 10 years // Dog class HAS-A relationship with trainer. Composite . . . // – this is when one class HAS a property that references an object // made from another class package dogsdate; public class DogsUI { public static void main(String[] args) { DogManager dm = new DogManager (); System.out.println("Dogs that qualify for the age constraints"); System.out.println("=========================================="); String heading = "Name age height pedigree trainer tr dob" + "\n"; System.out.println(heading + dm.printAll() ); System.out.println("Max age dog " + Dogs.maxAge); System.out.println("Min age working " + Dogs.minAge); System.out.println(dm.testAge()); } } ============================================================================ // steve eilertsen // Reading from a text file // Creates an array of Dog objects // Age is determined using the date of birth // Dogs in the array must be older than 2 and // younger that 10 years // Dog class HAS-A relationship with trainer. Composite . . . // – this is when one class HAS a property that references an object // made from another class 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; // dog fields String nameSt = null, dobDogSt = null, pedigreeSt = null, heightSt = null; int height = 0, age = 0; boolean pedigree = false; boolean valid = false; // trainer fields String trainerSt = null, dobTrainerSt = null; Trainer tr = null; try { Scanner scFile = new Scanner(new File("dogs.txt")); while(scFile.hasNextLine()) { String line = scFile.nextLine(); tokenArray = line.split("#"); nameSt = tokenArray[0]; dobDogSt = tokenArray[1]; heightSt = tokenArray[2]; pedigreeSt = tokenArray[3]; // parse String to final datatypes LocalDate dob = LocalDate.parse(dobDogSt, 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(); // test for valid ages for inclusion into array if(age < Dogs.maxAge && age > Dogs.minAge) valid = true; if(tokenArray.length == 6 && valid == true) { trainerSt = tokenArray[4]; dobTrainerSt = tokenArray[5]; LocalDate dobTr = LocalDate.parse(dobTrainerSt, DateTimeFormatter.ofPattern("yyyy/MM/dd")); tr = new Trainer(trainerSt, dobTr ); Dogs oneDog = new Dogs(nameSt, age, height, pedigree, tr); dogArray[counter] = oneDog; counter++; } if (tokenArray.length == 4 && valid == true) { Dogs oneDog = new Dogs(nameSt, age, height, pedigree); dogArray[counter] = oneDog; counter++; } // reset age valid flag back to false for the next record valid = false; } } 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() > Dogs.minAge) flag2 = true; testResults = testResults + flag1 + " " + flag2 + "\n"; // reset the boolean flags flag1 = false; flag2 = false; } return testResults; } // end testAge } ============================================================================ // steve eilertsen // The Dog class - with composite Trainer class package dogsdate; import java.time.LocalDate; public class Dogs { private String name = null; private int age = 0; private int height = 0; private boolean pedigree = false; private Trainer trainer; // Dogs may not be older than 10 years or younger than 2 years protected static int maxAge = 10; protected static int minAge = 2; // Constructor one - dogs with no trainer - 4 fields public Dogs(String n, int a, int h, boolean p) { name = n; age = a; height = h; pedigree = p; } // end constructor one // Constructor two - dogs with a trainer - 5 fields // The trainer class stores 2 fields ie trainer name and dob public Dogs(String n, int a, int h, boolean p, Trainer tr) { name = n; age = a; height = h; pedigree = p; trainer = tr; } // end constructor two public String toString() { return name + "\t" + age + "\t" + height + "\t" + pedigree + "\t" + trainer; } public int getAge(){ return age; } public String getName(){ return name; } } =============================================================================== // trainer class for composite app package dogsdate; import java.time.LocalDate; public class Trainer { private String name = "n/a"; private LocalDate dob = null; public Trainer(String n, LocalDate dob) { name = n; this.dob = dob; } public String toString() { return name + "\t" + dob; } } =========================================================================== 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