Show Some Love
JAVA

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

This forum was posted on Saturday, May 18, 2013 8:26:22 AM


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 = 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


53 Viewed this Question



 Recent 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 Solution for this Question


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

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

Scanner NoSuchElement Exception Java

Can someone tell me why this code snippet snippet is throwing the NoSuchElementException?I hv used scanner before for input and o/p but have not ever had a problem with it.import java.util.*;public class TaskRunner{ public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter the year: "); int year = keyboard.nextInt(); System.out.print("Enter the month: "); int month = (keyboard.nextInt() - 1); System.out.print("Enter the day: &quo

How to Hide First level cascading Parameter Dynamically

I hv a case where i require to hide first level parameter in a cascading parameter Group.I will pass that argument as segment of url(ie.as querystring?).Now i made two parameters under cascading parameter group(ie:officecode and empno).If user is passing officecode parameter through url(it means user is not admin) then i require to hide first level parameter (ie officecode) and directly load all the empno corresponding to that office.If user is not passing officecode as segment of url(it means m

GAE Datastore API drop-in replacement

I'm trying to find a similar solution with this:Drop-in replacement for AppEngine Datastore API (Python)However this API seems to be outdated already is there any effort to do Datastore API using MongoDB as backend for Java?

Controlling Process Name of my Jar

i need ed to know is there any approach I could control the name the process my jar is starting i.e I made a .jar file in java and whenever I am clicking on it it is causing a process named javaw.exe and i require to control this name.i require to do so becoz when I click on my jar file then if it already running it should stop and a new one should start i.e. i require to run a new thread (process) everytime I click on it by stopping the older one.If I kill the process named javaw.exe all

Does .equals on Hashset return true regardless of order?

For the Hashset in java There is a .equals method comparing elements in each set.Will this return true regardless of order?illustration let's say we have one set with elements {a b c} and other with elements {b c a}if you use .equals on these two sets will it return true or does it have to be sorted?

jsp servlet file upload doGet

I can upload a file in a servlet using the apache commons FileUpload. The code snippet snippet below worked in the processRequest method off the servlet but I copy Pasted the code snippet snippet in the doPost method and now it doesn't work anymore. the line List fileItems = upload.parseRequest(request);makes an blank array of fileItems.How can this be?This is the full doPost method@Overrideprotected void doPost(HttpServletRequest request HttpServletResponse o/p) throws ServletException IOException

Use Browser Scrollbar when JavaFX Applet has Focus

I am trying to seamlessely embed an applet into a webpage. If a user clicks a button in the applet instantly the applet has focus and when you try to scroll thereafter it does nothing (since the applet has no scrollbar). do you know approach to keep the browser in control of the scrollbar so even after clicking inside an applet it will continue to scroll on the webpage when the scroll wheel is used?

a method that returns a ip of machine that access web app

I hv a jsp web application hosted on a computer hosted in localhost.I can access this web application from other computer on LAN. What I'm doing here is I made a bean class which has a method that returns a IP of computer that access web app. But when i was accessing from other computer I got the IP of hosted computer itself . Can anyone tell why it occurs? Tell me How can i get the IP of other computer that accesses web app hosted in local host.

how to connect java servlet from c# windows form?

I already have an existing application designed using java in app engine....now i require to connect this servlet from c# forms program ? ...this is the tried out code snippet snippet for requestHttpWebRequest authRequest = (HttpWebRequest)HttpWebRequest.generate(googleLoginUrl);byte buffer = Encoding.ASCII.GetBytes(postData);authRequest.ContentLength = buffer.Length; Stream postDataStr=authRequest.GetRequestStream();postDataStr.Write(buffer 0 buffer.Length);postDataStr.Close();now it is connected

Custom Jackson serializer for a generic tree

Say I hv a parametrized tree designed in Java as following:public class Tree { private static class Node { E element; List children. } Node root; //... You get the logic.}The logic here is that the implementation above is only concerned with the topology of the tree but does not know anything about the elements that will be stored in the tree by an instantiation.Now say i require my tree elements to be geographies. The reason they are organized in trees is becoz continents hold countries countries