Sales analysis data

The problem

The file SalesData.txt contains the dollar amount of sales that a retail store made each day for a number of weeks. Each line in the file contains seven numbers, which are the sales numbers for one week. The numbers are separeted by a comma.

Write a program that opens the file and processes its comments. The program should display the following:


The following is the entire SalesData.txt file from the file.


Breaking it down

The exercise has requirements to display statistics at the weekly level and at the file or total sales level. This exercise could get complicated fast for instance, you could create object to represent the weekly and daily sales or the code could get fairly ugly. We tried to find the balance of each and know that you could reduce the number of lines of code.

In the main method we read the text file and break up each line by a comma creating a List of DoubleSummaryStatistics objects which represents the weekly stats. DoubleSummaryStatistics is an object for collecting statistics such as count, min, max, sum, and average provided in java 8. To get the overall stats, we will call various methods on DoubleSummaryStatistics method and summarize the values.


    Path salesDataPath = Paths

    //read all lines into a list of strings 
    List<String> fileByLines = java.nio.file.Files

    //convert each string into a DoubleSummaryStatistics object
    List<DoubleSummaryStatistics> weeksSummary = new ArrayList<>();
    for (String row : fileByLines) {
        // split on comma, map to double



public static void displayWeeklyStats(
        List<DoubleSummaryStatistics> weeksSummary) {

    // for every week output the stats
    for (int x = 0; x < weeksSummary.size(); x++) {
        DoubleSummaryStatistics weekStat = weeksSummary.get(x);

        System.out.println("Week #" + x + " sales: $" + weekStat.getSum());
        System.out.println("Average daily sales for week #" + x + ": $"
                + weekStat.getAverage());

public static void displaySummaryResults(
        List<DoubleSummaryStatistics> weeksSummary) {

    System.out.println("Total sales for all weeks: $"
            + -> p.getSum()).sum());

    System.out.println("Average weekly sales: $"
            + -> p.getSum()).average()

    System.out.println("The highest sales was $"
            + -> p.getSum()).max()

    System.out.println("The lowest sales were made during "
            + -> p.getSum()).min()



Week #0 sales: $12092.75
Average daily sales for week #0: $1727.5357142857142
Week #1 sales: $27461.0
Average daily sales for week #1: $3923.0
Week #2 sales: $12058.34
Average daily sales for week #2: $1722.6200000000001
Total sales for all weeks: $51612.09
Average weekly sales: $17204.03
The highest sales was $27461.0
The lowest sales were made during 12058.34