-1

我正在学习 Java初学者课程……由于某种原因,我的最新作业似乎非常困难。任务集中在文件 I/O 上。它涉及很多部分,但这部分是真正让我感到困惑的部分。它涉及:

  1. 读取销售文件,其中每行应该首先包含部门(N、S、E 或 W),然后是下一行的季度 (1-4),然后是第三行的销售额,然后重复。(可以这样做)
  2. 我们必须验证每一行是否具有允许的选项/文件类型之一(可以这样做)
  3. 然后将任何错误转储到错误文件并要求用户提供另一个文件并重复,直到输入文件的格式正确。 (有这个工作)
  4. 一旦它采用正确的格式,我们必须能够生成以下格式的销售报告(无法弄清楚我应该如何能够收集到这个......)

销售报告

按部门划分的总销售额:
北 $total
南 $total
东 $total
西 $total

季度总销售额:
一月至三月 $total
Apr-Jun $total
Jul-Sep $total
Oct-Dec $total

年终总计:$total

一个菜单选项必须是验证文件格式是否正确,然后出现另一个菜单选项以生成销售报告。

注意:我们还不需要使用数组(这是下一个任务)。

 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" +
            "\nQuarterly Sales Report Generator");

    do { 
        do {
            isValidMenuChoice = true;

            // display main menu options to user & prompt for choice
            System.out.print("\n\tMain Menu" +
                "\n" +
                "\n[V]alidate Sales Data");
            // only display generate sales report option if a sales report has been validated
            if (isValidSalesFile) {
                System.out.print("\n[G]enerate Sales Report");
            }
            System.out.print("\nE[x]it program" +
                "\n");

            System.out.print("\nEnter choice: ");

            menuChoice = keyboard.next();

            if (!menuChoice.equalsIgnoreCase("v") &&
                !menuChoice.equalsIgnoreCase("g") &&
                !menuChoice.equalsIgnoreCase("x")) {
                isValidMenuChoice = false;
                System.out.print("\n\t**Error** - Invalid menu item");
            }
        } while (isValidMenuChoice == false);

        if (menuChoice.equalsIgnoreCase("v")) {
            do {
                // prompt user for the sales data file path
                System.out.print("\nSales data file path: ");
                filePath = keyboard.next();

                fileName = new File(filePath);

                // if the file path doesn't exist, error displayed
                if (!fileName.exists()) {
                    System.out.println("\n" + fileName + " not found");
                }
            } while (!fileName.exists());

            // create a scanner for the input file
            inputFile = new Scanner(fileName);

            // create an error log to dump invalid sales data errors to
            fileName = new File("errorlog.txt");
            outputErrorLog = new PrintWriter(fileName);

            // resets boolean to allow for error free data file check
            isDataFileError = false;

            // validate and store the sales data
            while (inputFile.hasNext())
            {
                // first 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("\nThe data file contains data errors"
                    + " (See error log for details)");
            }
            else
            {
                // there were no errors; valid sales data file
                isValidSalesFile = true;
                System.out.print("\nThe data file is validated");
            }
        }

        if (menuChoice.equalsIgnoreCase("g")) {
            System.out.println("\ntemp g");

            do {
                // prompt user for file path to save sales report
                System.out.print("\nSave 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("\nOverwrite (y/n): ");
                        overwriteFile = keyboard.next();

                        if (overwriteFile.equalsIgnoreCase("y"))
                        {
                            isToOverwrite = true;

                            // create printwriter for the sales data file
                            outputSalesReport = new PrintWriter(filePath);

                            System.out.println("\nGenerating sales report...");



                            // add code to write to the results to the file





                            // close sales report
                            outputSalesReport.close();

                            System.out.println("\nReport generated successfully!"); 
                        }

                    } while (!overwriteFile.equalsIgnoreCase("n") &&
                            !overwriteFile.equalsIgnoreCase("y"));
                }
                // file doesn't already exist; save the file
                else if (!fileName.exists())
                {
                    // create printwriter for the sales data file
                    outputSalesReport = new PrintWriter(filePath);

                    System.out.println("\nGenerating sales report...");



                    // add code to write to the results to the file





                    // close sales report
                    outputSalesReport.close();

                    System.out.println("\nReport generated successfully!");
                }   
            } while (!isToOverwrite);


        }

        if (menuChoice.equalsIgnoreCase("x")) {
            System.out.println("\nProgram 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;
    }
}
}

我不是在要求具体的代码,而只是关于我应该如何最好地进行的一些指导。任何意见,将不胜感激。我已经被这个任务困了一个多月了!

如果采用有效格式,销售数据文件将是什么样子的示例(每个都在自己的行上......论坛忽略了中断):

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 2
25.39
E
3
50.25
E
4
43.21
W
1
120.89
_
_
_
_
_
_
_
_ 66.15 牛





3
38.22
N
4
27.66
E
2
135.32
E
3
37.50
E
4
9.10

4

2 回答 2

0

为自己创建一个包含您需要存储的信息的类(SalesData例如),当您验证文件的每个部分时,创建一个新SalesData对象,并使用您刚刚读取的值对其进行播种。从那里你将它添加到一个java.util.List实现中,比如ArrayList.

这将允许您在读取文件并生成报告后遍历列表。

您的另一个选择是保持每个部门和季度的运行记录。这有点简单,占用的内存更少,但如果报告的内容发生变化,灵活性会降低。

于 2013-01-28T04:11:28.897 回答
0

由于您不处理(不想处理)数组或集合,您可以考虑为 North、South、East、West、年终总计创建变量,并且 4 个季度在您遍历文件时更新这些变量的值。

使用收集这个任务会有点简化,但工作量几乎相同。

要生成实际报告,您可以使用 String 类进行连接,例如

String.format("%s:%s",strDivision, doubleAmount);

这将为您提供所需格式的信息,这可以扩展到解决报告的其余部分。

我认为您在解析文件时不需要帮助,因为您说您能够验证它。

于 2013-01-28T04:11:48.037 回答