#C13043. Sales Report Generator
Sales Report Generator
Sales Report Generator
You are given a CSV formatted input from standard input. The input consists of several lines. The first line is a header of the form product_id,category,sale_amount
and each subsequent line contains data corresponding to a product sale. Your task is to compute the total sales amount for each product category and print the results sorted in descending order by the sales amount. Each output line should be formatted as Category: Total
, where Total
is a number rounded to two decimal places.
If the CSV input is empty (only header) output nothing. If a line does not have exactly three comma-separated values or if the sale amount is not a valid number, print an error message in the format:
An error occurred: Invalid CSV format
The solution should read input from stdin and write the output to stdout.
Note: Use proper error handling to catch formatting issues and ensure robustness.
inputFormat
The input is provided via stdin and contains multiple lines. The first line is a header: product_id,category,sale_amount
. Each subsequent line represents a record with the following comma-separated fields:
product_id
: an identifier for the product (ignored in processing).category
: a string representing the product category.sale_amount
: a floating point number representing the sale amount.
If the CSV content after the header is empty, then no output should be produced. If any line is malformed, print the error message.
outputFormat
The output should be written to stdout and consists of multiple lines. Each line corresponds to a product category and its total sales amount, formatted as:
Category: Total
The totals must be rounded to two decimal places and the categories must be sorted in descending order based on the computed total.
If an error occurs during processing (e.g. due to invalid CSV format), output a single line:
An error occurred: Invalid CSV format## sample
product_id,category,sale_amount
1,Electronics,100.50
2,Books,15.75
3,Electronics,89.99
4,Clothing,45.00
5,Books,22.50
Electronics: 190.49
Clothing: 45.00
Books: 38.25
</p>