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:

Setup

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

1245.67,1490.07,1679.87,2371.46,1783.92,1461.99,2059.77
2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36
2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36

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.

    
public static void main(String[] args) throws IOException {

    Path salesDataPath = Paths
            .get("src/main/resources/com/levelup/java/exercises/beginner/SalesData.txt")
            .toAbsolutePath();

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

    //convert each string into a DoubleSummaryStatistics object
    List<DoubleSummaryStatistics> weeksSummary = new ArrayList<>();
    for (String row : fileByLines) {
        // split on comma, map to double
        weeksSummary.add(Arrays.stream(row.split(","))
                .mapToDouble(Double::valueOf).summaryStatistics());
    }

    displayWeeklyStats(weeksSummary);
    displaySummaryResults(weeksSummary);

}

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: $"
            + weeksSummary.stream().mapToDouble(p -> p.getSum()).sum());

    System.out.println("Average weekly sales: $"
            + weeksSummary.stream().mapToDouble(p -> p.getSum()).average()
                    .getAsDouble());

    System.out.println("The highest sales was $"
            + weeksSummary.stream().mapToDouble(p -> p.getSum()).max()
                    .getAsDouble());

    System.out.println("The lowest sales were made during "
            + weeksSummary.stream().mapToDouble(p -> p.getSum()).min()
                    .getAsDouble());

}

Output

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