当前位置:编程学习 > C#/ASP.NET >>

《重构》C#版实现(一)构建参考结果的测试用例

首先,列出的是第一个“电影租赁”案例的C#版初始代码:
[csharp]
using System; 
using System.Collections.Generic; 
 
namespace CH01_MovieRentalHouse 

    public class Movie 
    { 
        public const int CHILDRENS = 2; 
        public const int REGULAR = 0; 
        public const int NEW_RELEASE = 1; 
 
        public string Title { get; private set; } 
        public int PriceCode { get; private set; } 
         
        public Movie(string title, int priceCode) 
        { 
            Title = title; 
            PriceCode = priceCode; 
        } 
    } 
 
    public class Rental 
    { 
        public Movie Movie { get; private set; } 
        public int DaysRented { get; private set; } 
 
        public Rental(Movie rented, int days) 
        { 
            Movie = rented; 
            DaysRented = days; 
        } 
    } 
 
    public class Customer 
    { 
        public string Name { get; private set; } 
 
        private List<Rental> Rentals = new List<Rental>(); 
 
        public Customer(string name) 
        { 
            Name = name; 
        } 
 
        public void Add(Rental rental) 
        { 
            Rentals.Add(rental); 
        } 
 
        public string Statement() 
        { 
            double totalAmount = 0; 
            int frequentRenterPoints = 0; 
            string result = "Rental Record for " + Name + "\n"; 
            foreach (Rental rental in Rentals) 
            { 
                double thisAmount = 0; 
                 
                // determine amounts for each line 
                switch (rental.Movie.PriceCode) 
                { 
                    case Movie.REGULAR: 
                        thisAmount += 2; 
                        if (rental.DaysRented > 2) 
                            thisAmount += (rental.DaysRented - 2) * 1.5; 
                        break; 
                    case Movie.NEW_RELEASE: 
                        thisAmount += rental.DaysRented * 3; 
                        break; 
                    case Movie.CHILDRENS: 
                        thisAmount += 1.5; 
                        if (rental.DaysRented > 3) 
                            thisAmount += (rental.DaysRented - 3) * 1.5; 
                        break; 
                } 
 
                // add frequent renter points 
                frequentRenterPoints++; 
                // add bonus for a two day new release rental 
                if (rental.Movie.PriceCode == Movie.NEW_RELEASE && 
                    rental.DaysRented > 1) frequentRenterPoints++; 
                 
                // show figures for this rental 
                result += "\t" + rental.Movie.Title + "\t" + thisAmount.ToString() + "\n"; 
                totalAmount += thisAmount; 
  &nb

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,