/* Display2.java CIS 111 10/31/07 David Klick This program displays the text file generated by Create1.java using a nicely formatted report using control breaks. */ public class Display2 extends CIS111App { public static void main(String[] args) { int year; int month; int day; double amt; double monthTot; double grandTot; int prevMonth; String filename; String rptname; int recs; SequentialFile filein, fileout; clearConsole(); recs = 0; monthTot = 0.0; grandTot = 0.0; prevMonth = 0; // open report (output) file and print header rptname = "report.txt"; fileout = new SequentialFile(rptname, FileMode.OUTPUT); fileout.println("Daily sales (month totals)"); fileout.println(); // open data (input) file filein = new SequentialFile("sales.txt", FileMode.INPUT); // process each data record while (!filein.eof()) { // read in next record year = filein.readInt(); month = filein.readInt(); day = filein.readInt(); amt = filein.readDouble(); recs = recs + 1; // check for control break // does current record's month = last record's month? // Note: This won't work properly if data is not sorted or // if gaps of 12 months in the data are allowed. if (prevMonth != month && recs > 1) { fileout.printf("Month %2d %13s\n", prevMonth, formatCurrency(monthTot, "##,###,##0.00")); fileout.println(); grandTot = grandTot + monthTot; monthTot = 0; } // display details for record prevMonth = month; fileout.printf("%02d/%02d/%04d %13s\n", month, day, year, formatCurrency(amt, "##,###,##0.00")); monthTot = monthTot + amt; } // do control break processing automatically for last record fileout.printf("Month %2d %13s\n", prevMonth, formatCurrency(monthTot, "##,###,##0.00")); grandTot = grandTot + monthTot; monthTot = 0; // print grand totals to report fileout.println(); fileout.printf("Total %13s\n", formatCurrency(grandTot, "##,###,##0.00")); fileout.println(); // print out end of report (footer) fileout.printf("End of report (%d records processed)\n", recs); // display something to let user know program worked printf("End of report (%d records processed)\n", recs); // close files filein.close(); fileout.close(); } }