首页 > 其他分享 >重构第一个示例

重构第一个示例

时间:2023-09-09 11:33:55浏览次数:39  
标签:重构 plays 第一个 示例 type thisAmount audience ------------------------- perf

《重构 改善既有代码的设计》 马丁 富勒

 

第一章

戏剧演出团原始代码

invoices.json
[
  {
    "customer": "BigCo",
    "performances": [
      {
        "playId": "hamlet",
        "audience": 55
      },
      {
        "playId": "as-like",
        "audience": 35
      },
      {
        "playId": "othello",
        "audience": 40
      }
    ]
  }
]

 

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

 

main.js

const invoices = require('./invoices.json')
const plays = require('./plays.json')

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 (let 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(`unknow type: ${play.type}`);
        }

        // add volume credits
        volumeCredits += Math.max(perf.audience - 30, 0);
        // add extra credit for every ten comedy attendees
        if("comedy" === play.type) volumeCredits += Math.floor(perf.audience / 5);

        // print line for this order
        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;
}

console.log(statement(invoices[0], plays));

 

运行结果

 ------------------------- ------------------------- ------------------------- ------------------------- ------------------------- ------------------------- ------------------------- ------------------------- ------------------------- ------------------------- ------------------------- -------------------------

 

标签:重构,plays,第一个,示例,type,thisAmount,audience,-------------------------,perf
From: https://www.cnblogs.com/ives-xu/p/17689105.html

相关文章

  • java 支持 超大上G,多附件上传示例解析
    ​ 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现。先说下要求:PC端全平台支持,要求支持Windows,Mac,Linux支持所有浏览器。支持文件批量上传支持文件夹上传,且要求在服务端保留层级结构。文件夹数量要求支持到10W......
  • 剑指 Offer 52. 两个链表的第一个公共节点
    题目链接:剑指Offer52.两个链表的第一个公共节点题目描述:输入两个链表,找出它们的第一个公共节点。解法思路:代码:/***Definitionforsingly-linkedlist.*typeListNodestruct{*Valint*Next*ListNode*}*/funcgetIntersectionNode(headA,h......
  • 华为云ES导出数据脚本示例
      #!bash#接口用法#shexport-es-data.sh索引名称css密码cssIPindex=$1password=$2ip=$3value=$(curl-XPOST-uadmin:$2-k"https://${ip}:9200/${index}/_doc/_search?scroll=1m"-H'Content-Type:application/json'-d'{"siz......
  • 第一个批处理程序【.bat】
     echoon:显示回显,将如下命令复制到记事本中,更改名称为文件名为a.bat,测试效果@echoonechohelloa.batpauseechooff:关闭回显echooffechohellob.batpause @echo不让同行命令显示,@ECHOOFF指不显示ECHOOFF和它后面的命令回显@echooffechohell......
  • 初学者学习Auto.js的示例和教程资源
    1.Auto.js官方文档:您可以前往Auto.js官方网站(https://hyb1996.github.io/AutoJs-Docs/#/)查看官方文档,其中包含了基本使用方法、API参考和示例代码等。2.Auto.js示例脚本:Auto.js官方文档中提供了一些示例脚本,您可以下载并运行这些脚本,以了解其基本用法和功能。3.Auto.js论坛:Auto.......
  • spring中的aop(面向切面编程)需要到导入的包与简单示例
    2023-09-07<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://......
  • Android 调试桥 (adb) 使用教程/示例
    sidebar:autoAndroid调试桥(adb)Android调试桥(adb)是一种功能多样的命令行工具,可让您与设备进行通信。adb命令可用于执行各种设备操作,例如安装和调试应用。adb提供对Unixshell(可用来在设备上运行各种命令)的访问权限。它是一种客户端-服务器程序,包括以下三个组件:客......
  • 【ROS2机器人入门到实战】你的第一个MicroROS节点
    2.你的第一个MicroROS节点写在前面当前平台文章汇总地址:ROS2机器人从入门到实战获取完整教程及配套资料代码,请关注公众号<鱼香ROS>获取教程配套机器人开发平台:两驱版|四驱版为方便交流,搭建了机器人技术问答社区:地址fishros.org.cn你好,我是爱吃鱼香ROS的小鱼。上一节我们介绍了Mic......
  • pagehelper分页框架进行定时跑批分页 在插入与sql语句的编写,当插入有库中有报异常显示
    以下是一个示例的业务类,使用PageHelper分页框架进行定时跑批分页插入操作,并在遇到重复数据时跳过继续插入:importcom.github.pagehelper.PageHelper;importcom.github.pagehelper.PageInfo;importorg.springframework.beans.factory.annotation.Autowired;importorg.spring......
  • Apache Ant入门示例
    一直以来对ant总是有一种特别陌生的感觉,对它很迷茫可也就不管它,绕过它通过其他方式来做到。现在终于醒悟了不再逃避它了。网上找了一篇文章然后跟着入了个门。 “ApacheAnt是一个基于Java的生成工具。据最初的创始人JamesDuncanDavidson介绍,这个工具的名称是anotherneatto......