diff --git a/src/main/java/com/levelup/java/exercises/beginner/SalesAnalysis.java b/src/main/java/com/levelup/java/exercises/beginner/SalesAnalysis.java new file mode 100644 index 0000000..ea0755b --- /dev/null +++ b/src/main/java/com/levelup/java/exercises/beginner/SalesAnalysis.java @@ -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 Sales + * analysis data + * + */ +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 fileByLines = java.nio.file.Files + .readAllLines(salesDataPath); + + //convert each string into a DoubleSummaryStatistics object + List 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 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 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()); + + } +} diff --git a/src/main/resources/com/levelup/java/exercises/beginner/SalesData.txt b/src/main/resources/com/levelup/java/exercises/beginner/SalesData.txt new file mode 100644 index 0000000..c086dc5 --- /dev/null +++ b/src/main/resources/com/levelup/java/exercises/beginner/SalesData.txt @@ -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 \ No newline at end of file