Project · Data
Inventory audit
Validate product identifiers and quantities before accepting an import.
All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.
Program.cs
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
var rows = new[] { new Stock("A1", 3), new Stock("B2", 0), new Stock("A1", 8), new Stock("C3", -1) };
var errors = InventoryAudit.Validate(rows);
Console.WriteLine(errors.Length == 0 ? "Import ready" : string.Join("\n", errors));
Models/Stock.cs
public sealed record Stock(string Code, int Quantity);
Validation/InventoryAudit.cs
using System;
using System.Collections.Generic;
public static class InventoryAudit
{
public static string[] Validate(IEnumerable<Stock> rows)
{
var seen = new HashSet<string>(StringComparer.Ordinal);
var errors = new List<string>();
var number = 0;
foreach (var row in rows)
{
number++;
if (string.IsNullOrWhiteSpace(row.Code)) errors.Add($"Row {number}: missing code");
else if (!seen.Add(row.Code)) errors.Add($"Row {number}: duplicate {row.Code}");
if (row.Quantity < 0) errors.Add($"Row {number}: quantity is negative");
}
return errors.ToArray();
}
}
Inventoryaudit.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>