我是靠谱客的博主 炙热银耳汤,最近开发中收集的这篇文章主要介绍《重构》 — Java示例:影片出租店程序(4、添加功能),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

示例:影片出租店程序(添加功能)

说明:

通过对计算逻辑的提炼,可以完成一个HtmlStatement(),并复用原本Statement()内的所有计算。

 

代码:

package Movie_Ref;

 

import java.util.Enumeration;

import java.util.Vector;

 

public class Customer {

       private String _name;//姓名

       private Vector _rentals = new Vector(); //租借记录

      

       public Customer(String name) {

              _name = name;

       }

 

       public void addRental(Rental obj) {

              _rentals.addElement(obj);

       }

 

       public String get_name() {

              return _name;

       }

 

       private int getTotalFrequentRenterPoints() {

              int result = 0; //--常客积点          

              Enumeration rentals = _rentals.elements();

             

              while(rentals.hasMoreElements()){

                     Rental each = (Rental) rentals.nextElement(); //--取得一笔租借记录       

                     //---累加常客积点

                     result += each.getFrequentRenterPoints();

              }

             

              return result;

       }

 

       private double getTotalCharge() {

              double result = 0; //--总消费金额       

              Enumeration rentals = _rentals.elements();

             

              while(rentals.hasMoreElements()){

                     Rental each = (Rental) rentals.nextElement(); //--取得一笔租借记录

                     result += each.getCharge();

              }           

             

              return result;

       }

      

       public String statement() {          

              Enumeration rentals = _rentals.elements();

              String result = "Rental Record for " + get_name() + "/n";

             

              while(rentals.hasMoreElements()){

                     Rental each = (Rental) rentals.nextElement(); //--取得一笔租借记录                                   

                     //---显示此笔租借数据

                     result += "/t" + each.get_movie().get_title() + "/t" +

                                     String.valueOf(each.getCharge()) + "/n";

              }

              //---结尾打印

              result += "Amount owed is " + String.valueOf(getTotalCharge()) + "/n";

              result += "You earned " + String.valueOf(getTotalFrequentRenterPoints()) +

                             " frequent renter points";

              return result;

       }

      

       public String htmlStatement() {    

              Enumeration rentals = _rentals.elements();

              String result = "<H1>Rentals for <EM>" + get_name() + "</EM></H1><P>/n";

             

              while(rentals.hasMoreElements()){

                     Rental each = (Rental) rentals.nextElement(); //--取得一笔租借记录                                   

                     //---显示此笔租借数据

                     result += each.get_movie().get_title() + ": " +

                                     String.valueOf(each.getCharge()) + "<BR>/n";

              }

              //---结尾打印

              result += "<P>You owe <EM>" + String.valueOf(getTotalCharge()) + "</EM><P>/n";

              result += "On this rental you earned <EM>" + String.valueOf(getTotalFrequentRenterPoints()) +

                             " </EM> frequent renter points<P>";

              return result;         

       }

}

 

最后

以上就是炙热银耳汤为你收集整理的《重构》 — Java示例:影片出租店程序(4、添加功能)的全部内容,希望文章能够帮你解决《重构》 — Java示例:影片出租店程序(4、添加功能)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部