Rebuild training around a modular TS app
Replace the old exercise files (challenges, maze, sandbox, etc.) with a runnable order/inventory TypeScript app under training/ that ships with unit tests, typecheck, lint, and format scripts. Merge the full task checklist from TRAINING.md into README.md and drop the obsolete docs and challenge files. Add a root .gitignore for .zed, .reasonix, and build artifacts, untrack the previously committed local configs, and fix the root tsconfig to point at the new training sources.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// inventory —— 库存(被 orders 调用)
|
||||
// 练习:/stock/ 搜索、]m [m 方法跳转、gA 查看引用
|
||||
// ════════════════════════════════════════════════════════════
|
||||
|
||||
const stock = new Map<string, number>([
|
||||
["apple", 40],
|
||||
["banana", 60],
|
||||
["milk", 20],
|
||||
["bread", 15],
|
||||
["egg", 120],
|
||||
["cheese", 8],
|
||||
]);
|
||||
|
||||
export class StockError extends Error {}
|
||||
|
||||
export function available(productId: string): number {
|
||||
return stock.get(productId) ?? 0;
|
||||
}
|
||||
|
||||
export function reserve(productId: string, quantity: number): void {
|
||||
const left = available(productId);
|
||||
if (quantity > left) {
|
||||
throw new StockError(`Not enough stock for ${productId} (need ${quantity}, have ${left})`);
|
||||
}
|
||||
stock.set(productId, left - quantity);
|
||||
}
|
||||
|
||||
export function release(productId: string, quantity: number): void {
|
||||
stock.set(productId, available(productId) + quantity);
|
||||
}
|
||||
|
||||
export function stockReport(): string[] {
|
||||
const rows: string[] = [];
|
||||
for (const [id, left] of stock) {
|
||||
rows.push(`${id.padEnd(8)} ${String(left).padStart(3)} left`);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
Reference in New Issue
Block a user