OOP APPROACH THE UI CLASS package moneyraisednetOOP; // steve eilertsen // Gr 10 - Reading from a simple text file // and totalling the money raised per house. // OOP approah with a UI class and a manager class // Using the Scanner class to access the text file. // Using basic String handling methods instead of the delimiter to separate // the house from the amount. // The while loop, the for loop, if else. // The use of individual counters. // Roounding off of final answers to two decimal places // TEXT FILE LOOKS LIKE THIS . . . // C 84.81 // W 46.52 // I 42.59 // W 48.81 // K 79.97 // C 41.85 // W 87.71 // The learners house, followed by the amount of money the learner raised. // Houses are - Ibus, Kingfisher, Weaver and Cormorant, always in the order. public class MoneyRaisedUI { public static void main(String[]args) { System.out.println("Hello amount"); MoneyRaisedManager mm = new MoneyRaisedManager(); mm.calcIbus(); mm.calcCormorant(); mm.calcKingfisher(); mm.calcWeaver(); mm.calcGrandAmount(); String wholeReport = mm.report(); System.out.println(wholeReport); } } ============================================================ THE MANAGER CLASS package moneyraisednetOOP; // steve eilertsen // Gr 10 - Reading from a simple text file // and totalling the money raised per house. // OOP approah with a UI class and a manager class // Using the Scanner class to access the text file. // Using basic String handling methods instead of the delimiter to separate // the house from the amount. // The while loop, the for loop, if else. // The use of individual counters. // Roounding off of final answers to two decimal places // TEXT FILE LOOKS LIKE THIS . . . // C 84.81 // W 46.52 // I 42.59 // W 48.81 // K 79.97 // C 41.85 // W 87.71 // The learners house, followed by the amount of money the learner raised. // Houses are - Ibus, Kingfisher, Weaver and Cormorant, always in the order. import java.io.File; import java.io.FileNotFoundException; import java.text.DecimalFormat; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class MoneyRaisedManager { // FIVE COUNTERS NEEDED // One overall counter for line by line error checking in txt file. // Lines start at one, not zero. private int oCounter = 1; // One counter for each house. private int iCounter = 0; private int kCounter = 0; private int wCounter = 0; private int cCounter = 0; // One total amount for each house. private double iAmount = 0.0; private double kAmount = 0.0; private double wAmount = 0.0; private double cAmount = 0.0; // Grand total for all the houses private double grandTotal = 0.0; // One array for each house to store the money raised per house. private double[] ibusArr = new double[200]; private double[] kingfisherArr = new double[200]; private double[] weaverArr = new double[200]; private double[] cormorantArr = new double[200]; // Rounding off to 2 decimal places. Decimal rounding object called "d" private DecimalFormat d = new DecimalFormat("0.00"); // Consturctor method public MoneyRaisedManager() { try { Scanner scFile = new Scanner(new File("funday4.txt")); System.out.println("File found"); while(scFile.hasNextLine()) { String line = scFile.nextLine(); // One can also use the switch case statement here. // Each house needs its own array and its own counter. // Use String handling to separate the house from the amount. if(line.charAt(0) == 'I') { String amountSt = line.substring(2); double amount = Double.parseDouble(amountSt); ibusArr[iCounter] = amount; iCounter++; } else if(line.charAt(0) == 'K') { String amountSt = line.substring(2); double amount = Double.parseDouble(amountSt); kingfisherArr[kCounter] = amount; kCounter++; } else if(line.charAt(0) == 'W') { String amountSt = line.substring(2); double amount = Double.parseDouble(amountSt); weaverArr[wCounter] = amount; wCounter++; } else if(line.charAt(0) == 'C') { String amountSt = line.substring(2); double amount = Double.parseDouble(amountSt); cormorantArr[cCounter] = amount; cCounter++; } else { System.out.println("Error with house in line " + oCounter); System.exit(0); } // Overall counter for error checking. oCounter++; } // end while } catch (FileNotFoundException ex) { Logger.getLogger(MoneyRaisedManager.class.getName()).log(Level.SEVERE, null, ex); } } // end constructor public void calcIbus() { // Total Ibus house. for(int x = 0; x < iCounter; x++) { iAmount = iAmount + ibusArr[x]; } } public void calcKingfisher() { // Total Kingfisher house. for(int x = 0; x < kCounter; x++) { kAmount = kAmount + kingfisherArr[x]; } } public void calcWeaver() { // Total Weaver house. for(int x = 0; x < wCounter; x++) { wAmount = wAmount + weaverArr[x]; } } public void calcCormorant() { // Total Cormorant house. for(int x = 0; x < cCounter; x++) { cAmount = cAmount + cormorantArr[x]; } } public void calcGrandAmount() { grandTotal = (iAmount + kAmount + wAmount + cAmount); } public String report() { String wholeReport = ""; // Build ouptput String wholeReport = "The total for Ibus house is R" + d.format(iAmount) + "\n" + "The total for Kingfisher house is R" + d.format(kAmount) + "\n" + "The total for Weaver house is R" + d.format(wAmount) + "\n" + "The total for Cormorant house is R" + d.format(cAmount) + "\n" + "===============================================" + "\n" + "The grand total is R" + d.format(grandTotal); return wholeReport; } } // end class ==================================================