Java File I/O validate file contents then use those contents to create sales report closed


I am in a fresher Java course...and my latest assignment appears super hard for little reason. The assignment is focused around File I/O. It involves various parts but this segment is what's actually confusing me. It involves:
Reading a Sales file where every line is supposed to have 1st the
Division (N S E or W) then the Quarter (1-4) on the next line
then the Sales Amount on the third line and then repeats. (can do
this ok)
We have to validate that every line has one of the allowable
options/file kinds (can do this ok)
Then dump any errors to an error file and ask the member to supply
other file & repeat until the input file is in the right format.
(have this working)
Once it's in the right format we have to be capable to generate a
sales report in the following format (can not figure out how I am
supposed to be capable to get this stored...)
Sales Report

Total Sales by division:
North $total
South $total
East $total
West $total

Total Sales by quarter:
Jan-Mar $total
Apr-Jun $total
Jul-Sep $total
Oct-Dec $total

Year End Total: $total
One menu option has to be to Validate the file is in the right format and then other menu option seems to generate the Sales Report.
Note: We are not expected to use arrays yet (That is the next assignment).
 import java.io.*;
import java.util.Scanner;
public class SalesReporting {
public static void main(String args) throws Exception {
String inputData;
String divisionRead;
int qtrRead;
double salesRead;
String filePath;
String menuChoice;
File fileName;
PrintWriter outputErrorLog;
PrintWriter outputSalesReport;
Scanner keyboard = new Scanner(System.in);
Scanner inputFile;
String overwriteFile;
Boolean isDataFileError;
Boolean isValidSalesFile = false;
Boolean isValidMenuChoice;
Boolean isToOverwrite = false;
System.out.println("\tHarmway Corporation" +
"Quarterly Sales Report Generator");
do {
do {
isValidMenuChoice = true;
// show main menu options to member & prompt for choice
System.out.print("
\tMain Menu" +
"
" +
"
Validate Sales Data");
// only show generate sales report option if a sales report has been validated
if (isValidSalesFile) {
System.out.print("
Generate Sales Report");
}
System.out.print("
Exit program" +
"
");
System.out.print("
Enter option: ");
menuChoice = keyboard.next();
if (!menuChoice.equalsIgnoreCase("v") &&
!menuChoice.equalsIgnoreCase("g") &&
!menuChoice.equalsIgnoreCase("x")) {
isValidMenuChoice = false;
System.out.print("
\t**Error** - Invalid menu item");
}
} while (isValidMenuChoice == false);
if (menuChoice.equalsIgnoreCase("v")) {
do {
// prompt member for the sales data file path
System.out.print("
Sales data file path: ");
filePath = keyboard.next();
fileName = new File(filePath);
// if the file path does not exist error displayed
if (!fileName.exists()) {
System.out.println("
" + fileName + " not found");
}
} while (!fileName.exists());
// generate a scanner for the input file
inputFile = new Scanner(fileName);
// generate an error log to dump invalid sales data errors to
fileName = new File("errorlog.txt");
outputErrorLog = new PrintWriter(fileName);
// resets boolean to permit for error free data file check
isDataFileError = false;
// validate and store the sales data
while (inputFile.hasNext())
{
// 1st line must be division N S E or W to be valid
inputData = inputFile.next();
try
{
divisionRead = inputData;
if (isValidDivision(divisionRead)) {
System.out.println("ok division");
}
else
{
isDataFileError = true;
outputErrorLog.println(divisionRead + " Invalid division value");
}
}
catch (Exception e)
{
isDataFileError = true;
outputErrorLog.println(inputData + " Invalid division value");
}
// if second line is a valid quarter
inputData = inputFile.next();
try
{
qtrRead = Integer.parseInt(inputData);
if (isValidQuarter(qtrRead)) {
System.out.println("ok quarter");
}
else
{
isDataFileError = true;
outputErrorLog.println(qtrRead + " Invalid quarter value");
}
}
catch (Exception e)
{
isDataFileError = true;
outputErrorLog.println(inputData + " Invalid quarter value");
}
inputData = inputFile.next();
try
{
salesRead = Double.parseDouble(inputData);
if (isValidSales(salesRead)) {
System.out.println("ok sales");
}
else
{
isDataFileError = true;
outputErrorLog.println(salesRead + " Invalid sales amount value");
}
}
catch (Exception e)
{
isDataFileError = true;
outputErrorLog.println(inputData + " Invalid sales amount value");
}
}
// close the input sales data file
inputFile.close();
// close the error log file to write
outputErrorLog.close();
if (isDataFileError)
{
// there was an error in the sales file; not a valid sales data file
isValidSalesFile = false;
System.out.print("
The data file having data errors"
+ " (See error log for details)");
}
else
{
// there were no errors; valid sales data file
isValidSalesFile = true;
System.out.print("
The data file is validated");
}
}
if (menuChoice.equalsIgnoreCase("g")) {
System.out.println("
temp g");
do {
// prompt member for file path to save sales report
System.out.print("
Save report as: ");
filePath = keyboard.next();
// identify file to be used
fileName = new File(filePath);
// file already exists; ask whether to overwrite file or not
if (fileName.exists())
{
do
{
System.out.print("
Overwrite (y/n): ");
overwriteFile = keyboard.next();
if (overwriteFile.equalsIgnoreCase("y"))
{
isToOverwrite = true;
// generate printwriter for the sales data file
outputSalesReport = new PrintWriter(filePath);
System.out.println("
Generating sales report...");
// add code snippet to write to the output to the file
// close sales report
outputSalesReport.close();
System.out.println("
Report produced successfully!");
}
} while (!overwriteFile.equalsIgnoreCase("n") &&
!overwriteFile.equalsIgnoreCase("y"));
}
// file does not already exist; save the file
else if (!fileName.exists())
{
// generate printwriter for the sales data file
outputSalesReport = new PrintWriter(filePath);
System.out.println("
Generating sales report...");
// add code snippet to write to the output to the file
// close sales report
outputSalesReport.close();
System.out.println("
Report produced successfully!");
}
} while (!isToOverwrite);
}
if (menuChoice.equalsIgnoreCase("x")) {
System.out.println("
Program has been terminated.");
System.exit(0);
}
} while (!menuChoice.equalsIgnoreCase("x"));
}
public static boolean isValidDivision(String divisionRead) {
if (divisionRead.equalsIgnoreCase("N") ||
divisionRead.equalsIgnoreCase("S") ||
divisionRead.equalsIgnoreCase("E") ||
divisionRead.equalsIgnoreCase("W")) {
return true;
}
else {
return false;
}
}
public static boolean isValidQuarter(int qtrRead) {
if (qtrRead >= 1 && qtrRead <= 4) {
return true;
}
else {
return false;
}
}
public static boolean isValidSales(double salesRead) {
if (salesRead >= 0) {
return true;
}
else {
return false;
}
}
}

I am not asking for particular code snippet but just little guidance on how I should best proceed. Any advice would be apprised. I have been stuck on this assignment for over a month!
illustration of what the Sales Data file would seem like if in a valid format (every would be on it is own line...the post is ignoring the breaks):
N
1
35.50
N
2
26.99
N
3
77.45
N
4
58.30
S
1
132.15
S
2
81.19
S
3
159.06
S
4
83.55
E
1
99.40
E
2
25.39
E
3
50.25
E
4
43.21
W
1
120.89
W
2
392.11
W
3
105.76
W
4
299.95
N
2
66.15
N
3
38.22
N
4
27.66
E
2
135.32
E
3
37.50
E
4
9.10



Get Source Code

  Views 42   Posted on : Saturday, May 18, 2013 8:26:22 AM
Related Tags:




Replies on "Java File I/O validate file contents then use those contents to create sales report closed" :

Nobody has replied on this post , be first to post answer.


Post Your Answer/Comment:

Please Login first to post Reply !


Note: Please Put Code into <code></code> block.




Also See: