重构(第一章) 代码

时间:2021-10-14
本文章向大家介绍重构(第一章) 代码,主要包括重构(第一章) 代码使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

第一章所用到的初始代码:

 1 public class Movie {
 2     //儿童
 3     public static final int CHILDRENS = 2;
 4     //规律?
 5     public static final int REGULAR = 0;
 6     //新发行
 7     public static final int NEW_RELEASE=1;
 8 
 9     private  String _title;
10     private  int _priceCode;
11 
12     public String getTitle() {
13         return _title;
14     }
15 
16     public int getPriceCode() {
17         return _priceCode;
18     }
19 
20     public void setPriceCode(int arg) {
21         this._priceCode = arg;
22     }
23 }
 1 public class Rental {
 2     //电影类
 3     private Movie _movie;
 4     //借出天数
 5     private  int _daysRented;
 6 
 7     public Rental(Movie movie, int daysRented) {
 8         this._movie = movie;
 9         this._daysRented = daysRented;
10     }
11 
12     public Movie getMovie() {
13         return _movie;
14     }
15 
16     public int getDaysRented() {
17         return _daysRented;
18     }
19 }
 1 public class Customer {
 2     private String _name;
 3     //租金
 4     private Vector _rentals = new Vector();
 5 
 6     public Customer(String _name) {
 7         this._name = _name;
 8     }
 9 
10     public void addRental(Rental arg) {
11         _rentals.addElement(arg);
12     }
13 
14     public String get_name() {
15         return _name;
16     }
17 
18     public String statement() {
19         double totalAmount = 0;
20         int frequentRenterPoints = 0;
21         //被Vector替代了,现在基本上不用了
22         Enumeration rentals = _rentals.elements();
23 
24         String result = " [ " + get_name() + " ] 的借出记录\n";
25 
26         //遍历租赁  计算金额和积分点数
27         result+="**********详细记录**********\n";
28         while (rentals.hasMoreElements()) {
29             double thisAmount = 0;
30             Rental each = (Rental) rentals.nextElement();
31             //计算金额
32             switch (each.getMovie().getPriceCode()) {
33                 case Movie.REGULAR:
34                     thisAmount += 2;
35                     if (each.getDaysRented() > 2) {
36                         thisAmount += (each.getDaysRented() - 2) * 1.5;
37                     }
38                     break;
39                 case Movie.NEW_RELEASE:
40                     thisAmount += each.getDaysRented() * 3;
41                     break;
42                 case Movie.CHILDRENS:
43                     thisAmount += 1.5;
44                     if (each.getDaysRented() > 3) {
45                         thisAmount += (each.getDaysRented() - 3) * 1.5;
46                     }
47                     break;
48                 default:
49                     break;
50             }
51             //增加点数
52             frequentRenterPoints++;
53             //根据规则增加额外点数
54             if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) {
55                 frequentRenterPoints++;
56             }
57             //在返回值中加上 影片名称 金额
58             result += "影片名称: " + each.getMovie().getTitle() + "\n金  额:" + String.valueOf(thisAmount) + "\n";
59             totalAmount += thisAmount;
60         }
61         result+="**********总计**********\n";
62         result += "所欠金额为: " + String.valueOf(totalAmount) + "\n";
63         result += "共累计积分: " + String.valueOf(frequentRenterPoints);
64 
65         return result;
66     }
67 }

使用例:

    public static void main(String[] args) {

        Movie movie = new Movie("《新发行》", Movie.NEW_RELEASE);
        Rental rental = new Rental(movie, 5);
        Customer customer = new Customer("顾客甲");
        customer.addRental(rental);

        System.out.println(customer.statement());

    }

结果:

原文地址:https://www.cnblogs.com/zaoai/p/15405362.html