Core Java · Chapter 45 of 48
Project: the shift report
By the end of this chapter you can turn a day's log file into a report: parse each line strictly into a sealed type, total it with streams, and save the result to a file. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Projects
- 6 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
At 2 pm the morning shift hands over, and Ravi wants one page on his phone: how much each station took, and what sold best. Today the shift lead adds it up from the counter's sales file with a calculator. "Last Tuesday NDLS counted a refund as a sale. We were ₹200 out."
You have met every piece on its own: files in chapter 37, sealed types in 23, collectors in 33. A real report needs them together, and the hard part is the mess in between: a line with a missing field, a time typed as 6 am, a refund that must count against a sale. A parser that guesses quietly gives Ravi a wrong number; one that crashes with a stack trace gives him nothing.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
SalesCsv.java: turn one line into a sale, refund or delivery
In SalesCsv.java, make parseLine(line) turn one line of the sales file into a ShiftEvent. A line has exactly six fields separated by commas: time, station, type, item, qty, amount, each possibly with spaces around it. The type SALE gives a ShiftEvent.Sale, REFUND a ShiftEvent.Refund, and STOCK_IN a ShiftEvent.StockIn (its amount, 0 in the file, is not kept). Throw an IllegalArgumentException whose message contains the whole line when there are not exactly six fields, the type is unknown, the time or a number cannot be read, or the quantity is below 1.
For example, "06:05,SBC,SALE,Masala chai,2,80" gives Sale[time=06:05, station=SBC, item=Masala chai, qty=2, amount=80]; "06:15, SBC, REFUND, Masala chai, 1, 40" gives the matching Refund; "06:00,CSMT,STOCK_IN,Bun maska,20,0" gives StockIn[time=06:00, station=CSMT, item=Bun maska, qty=20]. Five fields, seven fields, the type GIFT, the time 6 am, the quantity two and the quantity 0 all throw.
- Task 2
SalesCsv.java: read a whole day's sales file
In SalesCsv.java, make read(file) return every event in the file, in file order, each line turned into an event with parseLine. The first line is the header: skip it. Skip blank lines too. A file with only the header gives an empty list. When a line is bad, throw an IllegalArgumentException whose message starts with line, a space and that line's number in the file, counting the header as line 1.
For example, the sample file with a blank line added gives its ten events in order; a header, one good sale and then 06:06,SBC,SALE,Bun maska throws an exception whose message starts with line 3.
- Task 3
ShiftReport.java: total the money each station took
In ShiftReport.java, make takings() return, for every station with any event, the money it took: the amounts of its sales minus the amounts of its refunds, with stock deliveries counting 0. The map is sorted by station code. No events give an empty map.
For example, for the sample day it returns {CSMT=115, NDLS=0, SBC=210}, in that order: SBC sold 80, 50 and 120 and refunded 40.
- Task 4
ShiftReport.java: find the best-selling item at each station
In ShiftReport.java, make bestSellers() return, for each station, the item with the most net sales: the quantity sold minus the quantity refunded, stock deliveries ignored. Only items with a net quantity above 0 count, and a station with none is left out. A tie goes to the item that comes first alphabetically. The map is sorted by station code.
For example, for the sample day it returns {CSMT=Cutting chai, SBC=Masala chai}; NDLS refunded all it sold, so it is left out. Sales of 2 masala chai, 2 bun maska and 5 cutting chai with 4 cutting chai refunded give Bun maska (a tie on 2 with masala chai; cutting chai nets 1).
- Task 5
ShiftReport.java: format the shift report
In ShiftReport.java, make format() return the report text, every line ending with \n. First Shift report: and the number of stations, then the word stations. Then one line per station in code order: the code, a space, ₹ and its takings, then (best seller: and the item, or none when it has no best seller, and a closing bracket. Last, Total ₹ and the sum of all the takings.
For example, the sample day gives the five lines: Shift report: 3 stations / CSMT ₹115 (best seller: Cutting chai) / NDLS ₹0 (best seller: none) / SBC ₹210 (best seller: Masala chai) / Total ₹325. A day with no events gives Shift report: 0 stations and Total ₹0.
- Task 6
ShiftReport.java: save the report to a file
In ShiftReport.java, make save(dir) write format() into a file called shift-report.txt inside dir, as UTF-8, creating dir and any missing parent folders first. Return the path of the file written.
For example, saving into reports/2026-10-04 (neither folder exists yet) creates both, writes the sample day's report into reports/2026-10-04/shift-report.txt and returns that path.
What you will learn
- A sealed type for events: a switch that must handle every kind, and the _ pattern for parts you do not need.
- Strict parsing: split with a limit, and wrapping a low-level exception in one that says which line was bad.
- Grouping with streams: a sorted map of totals per key.
- Saving a report: creating folders and writing a file.
More in Projects
Build "Project: the shift report" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.