Java Bootcamp – Workbooks and Challenges
)}

Global Superstore – Part 1

The Global Superstore application requires multiple threads.

Setup

Screen Shot 2021-09-20 at 1.25.27 PM.png

Task 1


Inside the data directory, a CSV file stores ~1,000,000 records. The main() method already contains boilerplate code that locates the Path of the csv file.

Path path = Paths.get(Thread.currentThread().getContextClassLoader().getResource(SALES).toURI());

Create a function based on the following Doc Comment:

/** * Function name: average * @param path (Path) * @param category (String) * @return Double * * Inside the function: * 1. Runs through every line from the CSV file as a stream. * 2. Maps every element in the stream to an array of three values. * 3. Filters every value by the @param category * 4. Maps every element in the stream to a double (price * quantity). * 5. Applies the terminal operation average. * 6. Returns the average as double. * */

The function receives two parameters and returns a Double average.

/** * Function name: average * @param path (Path) <-------- * @param category (String) <-------- * @return Double <-------- * * Inside the function: * 1. Runs through every line from the CSV file as a stream. * 2. Maps every element in the stream to an array of three String values. * 3. Filters every value by the @param category * 4. Maps every element in the stream to a double (price * quantity). * 5. Applies the terminal operation average. * 6. Returns the average as double. * */

Hints for 1, 2, 6:

  • You can run through all lines as a stream of elements using: Files.lines(path). This line throws a checked exception so place it in a try block.

  • You can skip the first element using .skip(element number).

  • You can "split" each String into an array of String values.

  • The terminal operation average() returns an OptionalDouble. Use getAsDouble to return a Double.

In the event of an exception, print its message and return 0..

Task 2


Create a function based on the following Doc Comment:

/** * Function name: totalAverage * @param path * @return Double * * Inside the function: * 1. Runs through every line from the CSV file as a stream. * 2. Maps every element in the stream to an array of three values. * 3. Maps every element in the stream to a double (price * quantity) * 4. Applies the terminal operation average * 5. Returns the average as double. * */

In the event of an exception, print its message and return 0..

Task 3


  • Call the average method for "Furniture", "Technology", and "Office Supplies".
  • Call the totalAverage method.

After calling your methods, copy the following at the end of your main() method.

Scanner scan = new Scanner(System.in); System.out.print("Please enter your name to access the Global Superstore dataset: "); String name = scan.nextLine(); System.out.println("Access Denied. We apologize for the inconvenience. Have a good day " + name + "."); scan.close();

Task 4


Run your code:

Screen Shot 2021-09-20 at 4.00.57 PM.png

Not good! The calculations are blocking the main() thread. The user has to wait a few seconds before they can answer the question. Blocking the main() thread negatively affects user experience.

Task 5


The calculations are time-intensive. Run each calculation on another thread.

Task 6


Run the app

Untitled.gif

Now the application is drastically faster. The calculations aren't blocking the main thread anymore. As the user interacts with the app, the calculations happen in the background.

Task 7


Compare your code with the solution: Solution: Global Superstore 1.

Good Luck!

Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.