-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9f3405
commit 41dcd3e
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/com/levelup/java/exercises/beginner/SalesAnalysis.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/resources/com/levelup/java/exercises/beginner/SalesData.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |