Skip to content

Commit

Permalink
added sales analysis exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmusgrove committed Nov 9, 2014
1 parent a9f3405 commit 41dcd3e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.levelup.java.exercises.beginner;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.List;

/**
* This java exercises will demonstrate how to find statistics on sales
* information.
*
* @author Justin Musgrove
* @see <a href=
* 'http://www.leveluplunch.com/java/exercises/sales-analysis-data/'>Sales
* analysis data</a>
*
*/
public class SalesAnalysis {

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 (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());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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

0 comments on commit 41dcd3e

Please sign in to comment.