Skip to content
Permalink
Browse files

第1章,第一個案例

原始代碼
  • Loading branch information...
marsen committed Feb 19, 2019
1 parent e6d114c commit 6e600db029fe2f62df724d0179b708c97a0b3313
@@ -0,0 +1,76 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Marsen.NetCore.Dojo.Refactoring_Improving_The_Design_of_Existing_Code
{
public class Customer
{
private string _name;
private List<Rental> _rentals = new List<Rental>();

/// <summary>
/// Initializes a new instance of the <see cref="Customer" /> class.
/// </summary>
public Customer(string name)
{
_name = name;
}

public string getName()
{
return _name;
}

public String statement()
{
double totalAmount = 0;
int frequentRenterPoints = 0;
IEnumerator rentals = _rentals.GetEnumerator();
String result = "Rental Record for " + getName() + "\n";
while (rentals.MoveNext())
{
double thisAmount = 0;
Rental each = (Rental) rentals.Current;
//determine amounts for each line
switch (each.getMovie().getPriceCode())
{
case 0://// Movie.REGULAR:
thisAmount += 2;
if (each.getDaysRented() > 2)

thisAmount += (each.getDaysRented() - 2) * 1.5;
break;
case 1://// Movie.NEW_RELEASE
thisAmount += each.getDaysRented() * 3;
break;
case 2://// Movie.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() == Movie.NEW_RELEASE)
&&
each.getDaysRented() > 1) frequentRenterPoints++;
//show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t" +
thisAmount.ToString() + "\n";
totalAmount += thisAmount;
}
//add footer lines
result += "Amount owed is " + totalAmount.ToString() +
"\n";
result += "You earned " + frequentRenterPoints.ToString()
+
" frequent renter points";
return result;
}

}
}
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Marsen.NetCore.Dojo.Refactoring_Improving_The_Design_of_Existing_Code
{
public class Movie
{
public static readonly int CHILDRENS = 2;
public static readonly int REGULAR = 0;
public static readonly int NEW_RELEASE = 1;
private string _title;
private int _priceCode;

/// <summary>
/// Initializes a new instance of the <see cref="Movie" /> class.
/// </summary>
public Movie(string title,int priceCode)
{
_title = title;
_priceCode = priceCode;
}

public int getPriceCode()
{
return _priceCode;
}

public void setPriceCode(int arg)
{
_priceCode = arg;
}

public string getTitle()
{
return _title;
}
}
}
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Marsen.NetCore.Dojo.Refactoring_Improving_The_Design_of_Existing_Code
{
public class Rental
{
private Movie _movie;
private int _daysRented;

public Rental(Movie movie, int daysRented)
{
_movie = movie;
_daysRented = daysRented;
}

public int getDaysRented()
{
return _daysRented;
}

public Movie getMovie()
{
return _movie;
}
}
}

0 comments on commit 6e600db

Please sign in to comment.
You can’t perform that action at this time.