[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;
}
}
~~~
# 交互過程

# 對這個程序的評價

# 總結
**如果你發現自己需要為程序添加一個特性,而代碼結構使你無法很方便地達成目的,那就先重構那個程序,使特性的添加比較容易進行,然后再添加特性
**
- 書列表
- laravel框架關鍵技術
- 第一章 組件化開發與composer使用
- 簡介
- composer
- 添加路由組件
- 添加控制器模塊
- 添加模型組件
- 添加視圖組件
- 第三章 laravel框架中常用的php語法
- 匿名函數
- 文件包含
- 魔術方法
- 魔術常量
- 反射
- 后期靜態綁定
- traits
- 第四章 laravel框架中使用的HTTP協議基礎
- HTTP協議
- 數據庫
- 數據遷移
- 第六章 laravel框架中的設計模式
- IOC模式
- php核心技術與最佳實踐
- 第一章面向對象核心
- 反射
- 簡單ORM
- 異常和錯誤
- 接口
- 第二章,面向對象設計
- 設計原則
- 單一職責
- 接口隔離
- 開放封閉
- 替換原則
- 依賴倒置
- linux是怎么寫的呢?
- 第三章 正則表達
- 認識正則
- 第四章 php網絡技術應用
- HTTP協議詳解
- php和http相關函數
- 垃圾信息防御措施
- 現代操作系統
- 引論
- sql必知必會
- 限制結果
- 按位置排序
- where求職順序
- IN操作符
- like
- 函數
- group by
- 組合查詢
- 插入檢索出的數據
- 視圖
- 高性能mysql
- 第一章節 mysql架構與歷史
- mysql架構邏輯圖
- 連接與管理
- 優化與運行
- 讀寫鎖
- 鎖粒度
- 表鎖(table lock)
- 行級鎖(row lock)
- ACID
- 隔離級別
- 死鎖
- 隱式和顯式鎖定
- 多版本并發控制
- Innodb概覽
- 第四章節 Schema與數據類型優化
- 選擇優化的數據類型
- 日期和時間類型
- 標識列
- 特殊類型數據
- 表設計中的缺陷
- 范式
- 計數器表
- 第五章 創建高性能索引
- 索引基礎
- 索引類型
- 索引的優點
- 高性能索引策略
- 選擇合適的索引列順序
- 聚簇索引
- 順序的主鍵什么時候會造成更壞的后果
- 覆蓋索引
- 使用索引掃描來做排序
- 壓縮索引
- 冗余和重復索引
- 索引和鎖
- 支持多種過濾條件
- 什么是范圍條件
- 優化排序
- 維護索引和表
- 表損壞
- 減少索引和數據的碎片
- 第六章 查詢性能優化
- 掃描的行數和訪問類型
- 重構查詢方式
- 查詢執行的基礎
- 重構-改善既有代碼設計
- 第一章-重構
- 什么是重構
- 第一個案列
- 重構第一步
- 王垠博客
- 多態取代價格相關邏輯