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,82 @@
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// cli —— 命令行入口(实际可运行)
|
||||
// npm start 显示用法
|
||||
// npm start -- list 商品目录
|
||||
// npm start -- order apple 3 cheese 1
|
||||
// npm start -- report 汇总报表
|
||||
// npm start -- stock 库存快照
|
||||
// ════════════════════════════════════════════════════════════
|
||||
|
||||
import { listProducts } from "./catalog";
|
||||
import { createOrder, listOrders, shipOrder } from "./orders";
|
||||
import { reportSummary } from "./reporting";
|
||||
import { stockReport } from "./inventory";
|
||||
|
||||
function printUsage(): void {
|
||||
console.log(
|
||||
[
|
||||
"Usage:",
|
||||
" list 商品目录",
|
||||
" order <id> <qty>… 下单(可多行,如 apple 3 milk 1)",
|
||||
" ship <id> 发货",
|
||||
" report 汇总报表",
|
||||
" demo 内置示例:下两单 → 发货 → 报表",
|
||||
" stock 库存快照",
|
||||
].join("\n"),
|
||||
);
|
||||
}
|
||||
|
||||
function parseOrderArgs(args: string[]) {
|
||||
const pairs: Array<{ productId: string; quantity: number }> = [];
|
||||
for (let i = 0; i + 1 < args.length; i += 2) {
|
||||
pairs.push({ productId: args[i], quantity: Number(args[i + 1]) });
|
||||
}
|
||||
if (pairs.length === 0 || pairs.some((p) => !Number.isFinite(p.quantity) || p.quantity <= 0)) {
|
||||
throw new Error("order 参数需成对出现:order <商品> <数量> …");
|
||||
}
|
||||
return pairs;
|
||||
}
|
||||
|
||||
function main(argv: string[]): void {
|
||||
const [command, ...rest] = argv;
|
||||
switch (command) {
|
||||
case "list":
|
||||
console.log(listProducts().join("\n"));
|
||||
break;
|
||||
case "order": {
|
||||
const order = createOrder(parseOrderArgs(rest));
|
||||
console.log(
|
||||
`order #${order.id} subtotal $${order.subtotal.toFixed(2)} tax $${order.tax.toFixed(2)} total $${order.total.toFixed(2)}`,
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "ship": {
|
||||
const id = Number(rest[0]);
|
||||
shipOrder(id);
|
||||
console.log(`order #${id} shipped`);
|
||||
break;
|
||||
}
|
||||
case "demo": {
|
||||
const one = createOrder([
|
||||
{ productId: "apple", quantity: 12 },
|
||||
{ productId: "milk", quantity: 2 },
|
||||
]);
|
||||
const two = createOrder([{ productId: "cheese", quantity: 6 }]);
|
||||
shipOrder(one.id);
|
||||
console.log(`#${one.id} ${listOrders()[0].lines.length} lines`);
|
||||
console.log(`#${two.id} ${listOrders()[1].lines.length} lines`);
|
||||
console.log(reportSummary().join("\n"));
|
||||
break;
|
||||
}
|
||||
case "report":
|
||||
console.log(reportSummary().join("\n"));
|
||||
break;
|
||||
case "stock":
|
||||
console.log(stockReport().join("\n"));
|
||||
break;
|
||||
default:
|
||||
printUsage();
|
||||
}
|
||||
}
|
||||
|
||||
main(process.argv.slice(2));
|
||||
Reference in New Issue
Block a user