欢迎光临 kjtoday 's BLOG
日志首页 | 随便聊聊 | PHP源码 | ASP源码
用户登陆
用户:
密码:
 

站点日历
73 2025 - 12 48
 123456
78910111213
14151617181920
21222324252627
28293031


站点统计
日志:44 篇
评论:6 篇
引用:0 个
会员:2 人
访问:3072472 次
建立:2024-05-31

最新评论

日志搜索

 标题   内容

不知不觉间2025年都快过完了 隐藏日志,无权浏览
未知 今天终于成功运行了重构2的第一个代码   [ 日期:2025-12-01 ]   [ 来自:本站原创 ]
其实自从买书《重构2》后都一直想见到这个运行结果
但说真的主要自己有两个认知盲区
1、不知道需要node.js这个运行环境
2、自己也没那个能力写调用文件
现在把代码全部帖上来以作纪念

第一个文件plays.json
{
  "hamlet": { "name": "Hamlet", "type": "tragedy" },
  "as-like": { "name": "As You Like It", "type": "comedy" },
  "othello": { "name": "Othello", "type": "tragedy" }
}

第二个文件invoice.json
{
  "customer": "BigCo",
  "performances": [
    { "playId": "hamlet", "audience": 55 },
    { "playId": "as-like", "audience": 35 },
    { "playId": "othello", "audience": 40 }
  ]
}

第三个文件statement.js
function statement(invoice, plays) {
  let totalAmount = 0;
  let volumeCredits = 0;
  let result = `Statement for ${invoice.customer}\n`;
  const format = new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
    minimumFractionDigits: 2
  }).format;

  for (const perf of invoice.performances) {
    const play = plays[perf.playId];
    let thisAmount = 0;

    switch (play.type) {
      case "tragedy":
        thisAmount = 40000;
        if (perf.audience > 30) {
          thisAmount += 1000 * (perf.audience - 30);
        }
        break;
      case "comedy":
        thisAmount = 30000;
        if (perf.audience > 20) {
          thisAmount += 10000 + 500 * (perf.audience - 20);
        }
        thisAmount += 300 * perf.audience;
        break;
      default:
        throw new Error(`unknown type: ${play.type}`);
    }

    volumeCredits += Math.max(perf.audience - 30, 0);
    if (play.type === "comedy") volumeCredits += Math.floor(perf.audience / 5);
    result += `  ${play.name}: ${format(thisAmount / 100)} (${perf.audience} seats)\n`;
    totalAmount += thisAmount;
  }

  result += `Amount owed is ${format(totalAmount / 100)}\n`;
  result += `You earned ${volumeCredits} credits\n`;
  return result;
}

// 导出函数
module.exports = { statement };

第4个文件kjtoday.js
const { statement } = require('./statement.js');
// 引入Node.js文件系统模块
const fs = require('fs');
const path = require('path');

// 1. 读取两个JSON文件
const readJsonFile = (filename) => {
  const filePath = path.resolve(__dirname, filename);
  const data = fs.readFileSync(filePath, 'utf8');
  return JSON.parse(data);
};

// 定义 invoice 和 plays 数据
const invoice = readJsonFile('invoice.json');
const plays = readJsonFile('plays.json');

// 调用函数
const result = statement(invoice, plays);
console.log(result);

第五,执行办法
1、将四个文件放在同一个文件夹
2、安装好node.js 12及以上版本
3、在终端先定位打开到对应的文件夹,然后运行node kjtoday.js

就能得到和书本一样的代码和一样的运行结果


[本日志由 admin 于 2025-12-01 10:18 AM 编辑]
引用通告地址 (0):
复制引用地址http://www.kjtoday.cc/trackback.asp?tbID=43
复制引用地址http://www.kjtoday.cc/trackback.asp?tbID=43&CP=GBK
暂时没有评论
发表评论 - 不要忘了输入验证码哦!
作者: 用户:  密码:   注册? 验证: 
评论:

禁止表情
禁止UBB
禁止图片
识别链接
识别关键字
表  情
 
Powered by L-Blog V1.08 (SE) Final © 2003-04 , Processed in 0.531250 second(s) , 8 queries