THE THREE TEXT FILES ARE AS FOLLOWS Hekpoort Prison,Roodepoort Prison,Heidelberg Prison // =============================================================== Text file 48917C,Thomas Corke,30,B 37284G,Peter Jack,Corporal 354821C,Daniel Sneddon,13,A 78664C,Bruce Bing,45,C 76359G,Lance Hackett,Sargeant 87204C,Rori Ranckan,12,A // =============================================================== Text file Toyota Hilux 16 Toyota People Mover 25 Toyota Mini Bus 45 Toyota LuxLiner 65 // ================================================================= Text file THE UI CLASS /* * Not all text files have the same number of fields ie they do not have * the same structure or their records could be different. * If this is the case you have to develop a strategy to organize the records * within each text files into their respective arrays. * * EXAMPLE: * 48917C,Thamas Corke,30,B * 37284G,Peter Jack,Corporal * EXPLANATION * The first record is a prisoner. See "C" for convict - has 4 fields * The second record is a prison guard. See "G" for guard - has 3 fields * WHAT TO DO * We need an array of prisoners, an array of guards. * */ package irregulartextfiles; import javax.swing.JOptionPane; public class IrregularTextFilesUI { public static void main(String[] args) { String theHRFile = JOptionPane.showInputDialog(null, "Enter the name of the HR file"); String theFFile = JOptionPane.showInputDialog(null, "Enter the name of the facilities file"); String theVFile = JOptionPane.showInputDialog(null, "Enter the name of the vehicles file"); PrisonManager pm = new PrisonManager(theFFile, theHRFile,theVFile); String allF = pm.allPrisonF(); System.out.println(allF); String allG = pm.allGuards(); System.out.println(allG); String allC = pm.allConvicts(); System.out.println(allC); String allV = pm.allVehicles(); System.out.println(allV); } } // ================================================================= end class THE MANAGER CLASS /* * Manager class with methods */ package irregulartextfiles; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class PrisonManager { private Convict[] convictArr = new Convict[50]; private Guards[] guardsArr = new Guards[50]; private Prisons[] prisonArr = new Prisons[50]; private Vehicles[] vehiclesArr = new Vehicles[50]; private int cCounter = 0; // Convict counter private int gCounter = 0; // Guard counter private int pCounter = 3; // Fixed prison counter private int vCounter = 0; // vehicle counter // Convicts prisoners uses "C" private String tIDC, tNameC; private int tMonthsC = 0; private char tBlockC; // Guards uses "G" private String tIDG, tNameG; private String tRankG; // Vehiclces uses "V" private String tBusNameV; private int tBusCapacityV; // ++++++++++++++++++++++++++++++++++++++++ public PrisonManager(String f, String hr, String v) { String theFFile = f; // The facilities file String theHRFile = hr; // The Human Resources file String theVFile = v; // The vehicle File // The prison facility file try { Scanner scFileF = new Scanner(new File(theFFile)); // The name of the prisons are all on one line in the text file while (scFileF.hasNext()){ String line = scFileF.nextLine(); Scanner scLine = new Scanner(line).useDelimiter(","); String tPrison1 = scLine.next(); String tPrison2 = scLine.next(); String tPrison3 = scLine.next(); prisonArr[0] = new Prisons(tPrison1); prisonArr[1] = new Prisons(tPrison2); prisonArr[2] = new Prisons(tPrison3); } scFileF.close(); // close inside try before the catch } catch (FileNotFoundException ex) { Logger.getLogger(PrisonManager.class.getName()).log(Level.SEVERE, null, ex); } // end prison facility // +++++++++++++++++++++++++++++++++++++++++ // The prison HR file with convicts and guards try { Scanner scFileHR = new Scanner(new File(theHRFile)); // Each line has one record - G for a guard and C for a convict while (scFileHR.hasNext()){ String line = scFileHR.nextLine(); Scanner scLine = new Scanner(line).useDelimiter(","); String id = scLine.next(); // C for convict prisoners if (id.endsWith("C")){ tIDC = id; tNameC = scLine.next(); tMonthsC = scLine.nextInt(); String tBlockCSt = scLine.next(); // Read as String tBlockC = tBlockCSt.charAt(0); // Cast to char convictArr[cCounter] = new Convict(tIDC, tNameC, tMonthsC, tBlockC); cCounter++; // counters the convicts } // G for guards else if (id.endsWith("G")){ tIDG = id; tNameG = scLine.next(); tRankG = scLine.next(); guardsArr[gCounter] = new Guards(tIDG, tNameG, tRankG); gCounter++; // counts the guards } else { System.out.println("Errot with C and G id codes"); System.exit(0); } } scFileHR.close(); // close inside the try and before the catch } catch (FileNotFoundException ex) { Logger.getLogger(PrisonManager.class.getName()).log(Level.SEVERE, null, ex); } // end HR file with convicts and guards // ++++++++++++++++++++++++++++++++++++++++ // The prison vehicle file with busses and capacity try { Scanner scFileV = new Scanner(new File(theVFile)); // One line has the bus make and name // The next line has the capacity of the bus while (scFileV.hasNext()){ String lineB = scFileV.nextLine(); tBusNameV = lineB; String lineC = scFileV.nextLine(); tBusCapacityV = Integer.parseInt(lineC); vehiclesArr[vCounter] = new Vehicles(tBusNameV, tBusCapacityV); vCounter++; // counts the vehicles } scFileV.close(); // close inside the try and before the catch } catch (FileNotFoundException ex) { Logger.getLogger(PrisonManager.class.getName()).log(Level.SEVERE, null, ex); } // end HR file with convicts and guards System.out.println("Success"); } // end constructor method // +++++++++++++++++++++++++++++++++++++++++++++++ public String allPrisonF(){ String line = ""; for (int i = 0; i < pCounter; i++){ line = line + prisonArr[i].toString() + "\n"; } return line; } // +++++++++++++++++++++++++++++++++++++++++++++++ public String allGuards(){ String line = ""; for (int i = 0; i < gCounter; i++){ line = line + guardsArr[i].toString() + "\n"; } return line; } // ++++++++++++++++++++++++++++++++++++++++++++++++++ public String allConvicts(){ String line = ""; for (int i = 0; i < cCounter; i++){ line = line + convictArr[i].toString() + "\n"; } return line; } // ++++++++++++++++++++++++++++++++++++++++++++++++++++ public String allVehicles(){ String line = ""; for (int i = 0; i < vCounter; i++){ line = line + vehiclesArr[i].toString() + "\n"; } return line; } } // end class // ================================================================== end class THE OBJECT CLASSES IE PRISONS, CONVICTS, GUARDS AND VEHICLES /* * The prisons class */ package irregulartextfiles; public class Prisons { private String name; public Prisons (String n) { name = n; } public String toString(){ return name; } } // ================================================================= end class /* * The convict class */ package irregulartextfiles; public class Convict { private String idC; private String name; private int months; private char block; public Convict(String id, String n, int m, char b) { idC = id; name = n; months = m; block = b; } public String toString(){ return idC + "\t" + name + "\t" + months + "\t" + block; } } // ================================================================= end class /* * The guard class */ package irregulartextfiles; public class Guards { private String idG; private String name; private String rank; public Guards(String id, String n, String r) { idG = id; name = n; rank = r; } public String toString(){ return idG + "\t" + name + "\t" + rank; } } // ================================================================= end class /* * The vehicle class */ package irregulartextfiles; public class Vehicles { private String nameMake; private int capacity; public Vehicles(String n, int c) { nameMake = n; capacity = c; } public String toString(){ return nameMake + "\t" + capacity; } } // ================================================================= end class THE OUTPUT run: Success Hekpoort Prison Roodepoort Prison Heidelberg Prison 37284G Peter Jack Corporal 76359G Lance Hackett Sargeant 48917C Thomas Corke 30 B 354821C Daniel Sneddon 13 A 78664C Bruce Bing 45 C 87204C Rori Ranckan 12 A Toyota Hilux 16 Toyota People Mover 25 Toyota Mini Bus 45 Toyota LuxLiner 65 BUILD SUCCESSFUL (total time: 26 seconds) // ================================================================= end output