皇家方舟

          重構(gòu)——改善既有代碼的設(shè)計 源代碼

          package cn.com;


          import java.util.Enumeration;
          import java.util.Vector;


          public class Customer
          {
          ?private String _name;// 姓名
          ?private Vector<Rental> _rentals = new Vector<Rental>();// 租借記錄


          ?public Customer(String name)
          ?{
          ??this._name = name;
          ?}


          ?public String getName()
          ?{
          ??return _name;
          ?}


          ?public void addRental(Rental arg)
          ?{
          ??this._rentals.addElement(arg);
          ?}


          ?public String statement()
          ?{
          ??double totalAmount = 0;// 總消費金額
          ??int frequentRenterPoints = 0;// 常客積點
          ??Enumeration rentals = this._rentals.elements();
          ??String result = "Rental Record for" + getName() + "\n";

          ??while(rentals.hasMoreElements())
          ??{
          ???double thisAmount = 0;
          ???Rental each = (Rental)rentals.nextElement();

          ???// determin amounts for each line
          ???switch(each.get_movie().get_priceCode())
          ???{
          ????case Movie.REGULAR:
          ?????thisAmount += 2;
          ?????if(each.get_dayRented() > 2)
          ??????thisAmount += (each.get_dayRented() - 2) * 1.5;
          ?????break;
          ????case Movie.NEW_RELEASE:
          ?????thisAmount += each.get_dayRented() * 3;
          ?????break;
          ????case Movie.CHILDRENS:
          ?????thisAmount += 1.5;
          ?????if(each.get_dayRented() > 3)
          ??????thisAmount += (each.get_dayRented() - 3) * 1.5;
          ?????break;
          ???}

          ???// add frequent renter points(累加 ??头e點)
          ???frequentRenterPoints++;

          ???// add bonus for a two day new release rental
          ???if((each.get_movie().get_priceCode()) == Movie.NEW_RELEASE
          ????&& each.get_dayRented() > 1)
          ????frequentRenterPoints++;
          ???// show figures for this rental(顯示此筆租借數(shù)據(jù))
          ???result += "\t" + each.get_movie().get_title() + "\t"
          ??????+ String.valueOf(thisAmount) + "\n";

          ???totalAmount += thisAmount;
          ??}

          ??// add footer lines(結(jié)尾打印)
          ??result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
          ??result += "You earned " + String.valueOf(frequentRenterPoints)
          ?????+ "frequent renter points";
          ??return result;
          ?}
          }

          package cn.com;


          public class Movie
          {
          ?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 Movie(String _title, int _priceCode)
          ?{
          ??this._title = _title;
          ??this._priceCode = _priceCode;
          ?}


          ?/**
          ? * @return 返回 _priceCode。
          ? */
          ?public int get_priceCode()
          ?{
          ??return _priceCode;
          ?}


          ?/**
          ? * @param code
          ? *??????? 要設(shè)置的 _priceCode。
          ? */
          ?public void set_priceCode(int code)
          ?{
          ??_priceCode = code;
          ?}


          ?/**
          ? * @return 返回 _title。
          ? */
          ?public String get_title()
          ?{
          ??return _title;
          ?}
          }

          package cn.com;


          public class Rental
          {
          ?private Movie _movie;// 影片
          ?private int _dayRented;// 租期


          ?public Rental(Movie _movie, int _dayRented)
          ?{
          ??this._movie = _movie;
          ??this._dayRented = _dayRented;
          ?}


          ?public int get_dayRented()
          ?{
          ??return _dayRented;
          ?}


          ?public Movie get_movie()
          ?{
          ??return _movie;
          ?}
          }



          posted on 2006-11-15 15:14 阿輝 閱讀(1068) 評論(0)  編輯  收藏 所屬分類: 學(xué)習(xí)日志

          My Links

          Blog Stats

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汽车| 威海市| 新乡市| 普兰店市| 聂荣县| 五峰| 衡阳县| 如东县| 达孜县| 禹城市| 依安县| 南汇区| 广水市| 当雄县| 东光县| 海林市| 咸宁市| 云南省| 库尔勒市| 焉耆| 平顶山市| 屏山县| 茶陵县| 伊春市| 郑州市| 塔河县| 丰原市| 关岭| 大邑县| 建阳市| 庆元县| 三台县| 五大连池市| 临夏县| 梁山县| 威信县| 武夷山市| 津市市| 丘北县| 东光县| 靖江市|