THINK FIRST·CODE LATER

← All labs

An order report as a stream pipeline

Problem

Write OrderPipeline. The first number is a threshold; then come orders customer product quantity unitPrice until the end of input. Using streams only (no explicit loops), print:

  1. the revenue (mapToDouble(...).sum()),
  2. how many orders are worth at least the threshold (filter + count),
  3. the distinct products, sorted, joined with , ,
  4. the top 3 orders by total (sorted with a reversed comparator, limit, forEach),
  5. the units sold (map + reduce),
  6. whether every order has at most 10 units (allMatch).

For the input

100
Lina pen 10 2.5
Omar laptop 1 4200
Mei mouse 3 60
Lina mouse 1 60
Ana desk 2 350

the program prints

Revenue: 5165.00
Orders of at least 100.0: 3
Products: desk, laptop, mouse, pen
Top 3 orders:
  Omar laptop 4200.00
  Ana desk 700.00
  Mei mouse 180.00
Units sold: 17
Every order has at most 10 units: true

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d3-3-data-1.txt

100
Lina pen 10 2.5
Omar laptop 1 4200
Mei mouse 3 60
Lina mouse 1 60
Ana desk 2 350
What your program must print for d3-3-data-1.txt
Revenue: 5165.00
Orders of at least 100.0: 3
Products: desk, laptop, mouse, pen
Top 3 orders:
  Omar laptop 4200.00
  Ana desk 700.00
  Mei mouse 180.00
Units sold: 17
Every order has at most 10 units: true

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d3-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content d3-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.