/* * Read in a text file with delimiters * Three class - UI, manager and template class * Creating an array of team objects */ package textfilearrayobjects; public class TextFileObjectsUI { public static void main(String[] args) { TextFileObjectsManager sm = new TextFileObjectsManager(); sm.displayAll(); } } ==================================================================== /* * OOP version.Three class - UI, manager and template class * Array of team objects read from a text file * Reading in a text file with four separate values per line with sorting * p - planets for adult teams * m - moons for teenage teams * c - celetrial bodes for children teams * EXAMPLES: * Mercury#p#red and green#1767.99 * Astroid#c#orange#1784.87 * Io#m#light blue#2578.54 * * Display all method to display the array of team objects * The total funds from adults, teenagers and children is determined */ package textfilearrayobjects; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import javax.swing.JOptionPane; public class TextFileObjectsManager { // Create an array of team objects private Team [] teamArr = new Team[30]; private int size = 0; private double totalFunds = 0.0; TextFileObjectsManager(){ // Create an array of team objects try { Scanner scFile = new Scanner(new File("SixASideBeachTextE.txt")); while (scFile.hasNext()) { String line = scFile.nextLine(); Scanner scLine = new Scanner(line).useDelimiter("#"); String name = scLine.next(); String ageGroup = scLine.next(); String colours = scLine.next(); double funds = scLine.nextDouble(); // determine age group if(ageGroup.equals("p")) ageGroup = "adults"; if(ageGroup.equals("m")) ageGroup = "teenagers"; if(ageGroup.equals("c")) ageGroup = "children"; teamArr[size] = new Team(name, ageGroup,colours, funds); totalFunds = totalFunds + funds; size++; } // end while } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null, "Error! File not found"); System.exit(0); } System.out.println("Success"); } // end constructor public void displayAll() { System.out.println("========================================="); System.out.println("Adults teams"); System.out.println("========================================="); for (int i = 0; i < size; i ++){ String tempAge = teamArr[i].getAge(); if(tempAge.equals("adults")) System.out.println(teamArr[i].toString()); } System.out.println("=========================================="); System.out.println("Teenage teams"); System.out.println("=========================================="); for (int i = 0; i < size; i ++){ String tempAge = teamArr[i].getAge(); if(tempAge.equals("teenagers")) System.out.println(teamArr[i].toString()); } System.out.println("========================================="); System.out.println("Children teams"); System.out.println("========================================="); for (int i = 0; i < size; i ++){ String tempAge = teamArr[i].getAge(); if(tempAge.equals("children")) System.out.println(teamArr[i].toString()); } System.out.println(); System.out.println("======================================="); System.out.println("Total amount raised by all the teams is R" + totalFunds); } } // end manager class ============================================================================= /* * The template class for Teams * name of the team * p for planet for adults teams * m for moon for teenager teams * c for celetial body for children teams * team colours and the amount of money raised */ package textfilearrayobjects; public class Team { private String name = null; private String age = null; private String colours = null; private double funds = 0.0; public Team(String n, String a, String c, double f) { name = n; age = a; colours = c; funds = f; } // end constructor public String getName() { return name; } public String getAge() { return age; } public String getColours() { return colours; } public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public void setColours(String colours) { this.colours = colours; } public void setFunds(double funds) { this.funds = funds; } public double getFunds() { return funds; } @Override public String toString() { return (name + "\t" + "\t" + colours + "\t" + funds); } } // end of class ======================================================================= TEXT FILE LOOKS LIKE THIS Mercury#p#red, green#1767.99 Venus#p#red#1565.34 Mars#p#green#2316.39 Jupiter#p#white#1452.12 Saturn#p#white, black#2198.19 Uranus#p#green stripes#1554.98 Neptune#p#yellow, orange#1567.76 Astroid#c#orange#1784.87 Comet#c#red, yellow#2776.56 Rhea#m#blue#1655.45 Callisto#m#blue stripes#1589.34 Io#m#light blue#2578.54 Europa#m#purple, orange#1686.66 Ceres#c#blue, black#2686.44 Luna#m#purple#1654.34 Titan#m#black, red#1876.98 Hyperion#m#dark red#2876.34 Janus#m#grey, red#1879.45 Oberon#m#black and yellow#1879.53 Ariel#m#black stripes#2876.34 ========================================================================== OUTPUT: Ignore the messy columns at this stage run: Success ================================================ Adults teams ================================================ Mercury red, green 1767.99 Venus red 1565.34 Mars green 2316.39 Jupiter white 1452.12 Saturn white, black 2198.19 Uranus green stripes 1554.98 Neptune yellow, orange 1567.76 ================================================ Teenage teams ================================================ Rhea blue 1655.45 Callisto blue stripes 1589.34 Io light blue 2578.54 Europa purple, orange 1686.66 Luna purple 1654.34 Titan black, red 1876.98 Hyperion dark red 2876.34 Janus grey, red 1879.45 Oberon black and yellow 1879.53 Ariel black stripes 2876.34 ================================================ Children teams ================================================ Astroid orange 1784.87 Comet red, yellow 2776.56 Ceres blue, black 2686.44 ============================================= Total amount raised by all the teams is R40223.61 BUILD SUCCESSFUL (total time: 0 seconds)