StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 6 · LINQ and Collections

Sales Grouping Report

easy—No data yet

Problem statement

Finance needs revenue summarized per category, and the current code loops with nested dictionaries and gets totals wrong. Complete SalesReportService with LINQ, so that the recorded sales are gathered into one group per category and each group is reported with how many sales it holds, what those sales add up to, and its average order value.

Sample Data

The sales store holds twelve recorded sales in memory, spread across five categories, one of which carries a single sale. It hands them back in an order that groups nothing: sales of the same category are scattered through it, and the order of the categories in it is neither their alphabetical order nor the order the report asks for.

Here is an example of a SaleRecord, the entity the store holds:

{
  "id": 12,
  "category": "training",
  "amount": 187.25
}

Service

SalesReportService

Implement BuildReport, which builds one CategoryReportLine per category and hands the lines back in report order:

  • BuildReport:
    • Read the sales from the sales store every time it is called, never from a copy kept from an earlier call, so the same service asked twice with a sale recorded in between reports that sale the second time.
    • Gather the sales into one group per Category, with every sale counted in exactly one group. Every category that carries at least one sale gets a line, and it gets exactly one line however many sales it holds, so a sale recorded in a category no earlier sale carried adds one more line to the report.
    • Set Category to the name of the category the line reports on.
    • Set SaleCount to the number of sales in that category, so a category carrying a single sale reports 1.
    • Set TotalRevenue to the sum of the Amount of the sales in that category, so a category carrying a single sale reports that sale's amount.
    • Set AverageOrderValue to that category's own TotalRevenue divided by its own SaleCount, rounded to two decimal places, with a value sitting exactly halfway between two hundredths rounded away from zero. An average that is already a whole number is reported unchanged, 100.00 over 3 sales is reported as 33.33, and 40.05 over 2 sales is 20.025 and is therefore reported as 20.03.
    • Return the lines ordered by TotalRevenue from the highest to the lowest, and when two categories are on the same TotalRevenue, the one whose Category comes first alphabetically comes first, whichever of the two the store happens to hold first.
Sample Requests & Responses

GET /api/sales/report

The five categories the twelve recorded sales fall into, richest first. hardware collects the four sales of 120.00, 80.00, 45.50 and 54.50, which add up to 300.00 and average 75.00. software collects 30.00, 40.00 and 30.00, which add up to 100.00 and average 33.3333..., reported as 33.33. services collects 25.05 and 15.00, which add up to 40.05 and average 20.025, reported as 20.03. support collects 12.10 and 33.40, which add up to 45.50 and average 22.75. training carries the single sale of 187.25, so its count is 1 and its total and its average are both that one amount.

Response code: 200

[
  { "category": "hardware", "saleCount": 4, "totalRevenue": 300.00, "averageOrderValue": 75.00 },
  { "category": "training", "saleCount": 1, "totalRevenue": 187.25, "averageOrderValue": 187.25 },
  { "category": "software", "saleCount": 3, "totalRevenue": 100.00, "averageOrderValue": 33.33 },
  { "category": "support", "saleCount": 2, "totalRevenue": 45.50, "averageOrderValue": 22.75 },
  { "category": "services", "saleCount": 2, "totalRevenue": 40.05, "averageOrderValue": 20.03 }
]

Recording a further sale changes only the line it belongs to, and the report reorders itself around it:

  • 100.00 in hardware takes that line to 5 sales, 400.00 and an average of 80.00, and it stays at the top.
  • 500.00 in consulting, a category no sale carried before, adds a sixth line carrying that one sale, and 500.00 puts it above hardware.
  • 5.45 in services takes it to 3 sales and 45.50, level with support. Two lines on the same total are ordered by name, so services comes first, averaging 15.17 against the 22.75 of support.
  • 200.00 in software takes it to 4 sales and 300.00, level with hardware. Ordered by name again, hardware comes first, both averaging 75.00.

More in LINQ and Collections

See the module
Catalog Filter And ProjectionThis problem covers turning the catalog the warehouse keeps into a listing a customer can see, dropping what is out of stock or outside the asked-for price range, ordering what is left by price, and leaving the internal fields behind.—No data yeteasyDeferred Execution BugThis problem covers taking a handover report and settling it then and there, so the copy kept says what the log said at that moment however the log changes afterwards, while a fresh run always reports the log as it stands.—No data yeteasyTop Customers AnalyticsThis problem covers ranking customers by what they have spent, reporting how many orders each placed and what they average, and pinning a ranking so the pinned board keeps saying what it said when it was taken.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.