企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 简介 这是一个影片出租店用的程序,计算每一个顾客的消费金额并打印详单. 操作者告诉程序,顾客租了哪些影片,租期多长,程序便根据租赁时间和影片类型算费用,影片分三类:普通片,儿童片和新片.除了计算费用,还要为常客计算积分,积分会根据租片种类是否为新片而有不同 # 影片 ~~~ package com.refactor; /** * 影片 * 纯数据类 */ public class Movice { public static final int CHILDRENS = 2; public static final int REGULAR = 0; public static final int NEW_RELEASE = 1; private String _title; private int _priceCode; public Movice(String _title, int _priceCode) { this._title = _title; this._priceCode = _priceCode; } public String getTitle() { return _title; } public void setTitle(String _title) { this._title = _title; } public int getPriceCode() { return _priceCode; } public void setPriceCode(int _priceCode) { this._priceCode = _priceCode; } } ~~~ # 租赁 ~~~ package com.refactor; /** * 租赁 * 表示某个顾客租了一部电影 */ public class Rental { //影片 private Movice _movie; private int _daysRented; public Rental(Movice _movie, int _daysRented) { this._movie = _movie; this._daysRented = _daysRented; } public Movice getMovie() { return _movie; } public int getDaysRented() { return _daysRented; } } ~~~ # 顾客 ~~~ package com.refactor; import org.junit.Test; import java.util.Enumeration; import java.util.Vector; /** * 顾客 * 有数据和访问的函数 */ public class Customer { private String _name; //自动增长的对象数组 private Vector _rentals = new Vector(); public Customer(String _name) { this._name = _name; } public String getName() { return _name; } public void addRental(Rental arg) { _rentals.addElement(arg); } //生成详单的函数 public String statement() { //总金额 double totalAmount = 0; //频繁的租客 int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); //租赁记录 String result = "Rental Record for " + getName() + "\n"; while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); //determine amounts for each line,确定每一行的金额 switch (each.getMovie().getPriceCode()) { case Movice.REGULAR: thisAmount += 2; if (each.getDaysRented() > 2) thisAmount += (each.getDaysRented() - 2) * 1.5; break; case Movice.NEW_RELEASE: thisAmount += each.getDaysRented() * 3; break; case Movice.CHILDRENS: thisAmount += 1.5; if (each.getDaysRented() > 3) thisAmount += (each.getDaysRented() - 3) * 1.5; break; } //add frequent renter points,增加频繁的租客积分 frequentRenterPoints++; //add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movice.NEW_RELEASE) && (each.getDaysRented() > 1)) frequentRenterPoints++; //show figures for this rental,显示这个租金的数字 result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n"; totalAmount += thisAmount; } //add footer lines,添加页脚线 result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; result += "You earned " + String.valueOf(frequentRenterPoints) + "frequent renter points"; return result; } } ~~~ # 交互过程 ![](https://box.kancloud.cn/81a6637228eecf476816fba1e6a32ed8_529x353.png) # 对这个程序的评价 ![](https://box.kancloud.cn/8799adad8a72b1bc6668a0be60356281_674x635.png) # 总结 **如果你发现自己需要为程序添加一个特性,而代码结构使你无法很方便地达成目的,那就先重构那个程序,使特性的添加比较容易进行,然后再添加特性 **