株式会社エス・スリー・フォー

C++/C#/VB.NETによる リファクタリング – リファクタリング前(VB.NET)

Movie.vb

Namespace visualbasic_before

  Public Class Movie

    Public Enum enumPriceCode
      CHILDRENS = 2
      REGULAR = 0
      NEW_RELEASE = 1
    End Enum

    Private _title As String
    Private _priceCode As enumPriceCode

    Public Sub New(ByVal title As String, ByVal priceCode As enumPriceCode)
      _title = title
      _priceCode = priceCode
    End Sub

    Public ReadOnly Property PriceCode() As enumPriceCode
      Get
        PriceCode = _priceCode
      End Get
    End Property

    Public ReadOnly Property Title() As String
      Get
        Title = _title
      End Get
    End Property

  End Class

End Namespace

Rental.vb

Namespace visualbasic_before

  Public Class Rental

    Private _movie As Movie
    Private _daysRented As Integer

    Public Sub New(ByVal movie As Movie, ByVal daysRented As Integer)
      _movie = movie
      _daysRented = daysRented
    End Sub

    Public ReadOnly Property DaysRented() As Integer
      Get
        DaysRented = _daysRented
      End Get
    End Property

    Public ReadOnly Property Movie() As Movie
      Get
        Movie = _movie
      End Get
    End Property

  End Class

End Namespace

Customer.vb

Namespace visualbasic_before

  Public Class Customer

    Private _name As String
    Private _rentals As System.Collections.ArrayList

    Public Sub New(ByVal name As String)
      _rentals = New System.Collections.ArrayList()
      _name = name
    End Sub

    Public Sub addRental(ByVal arg As Rental)
      _rentals.Add(arg)
    End Sub

    Public ReadOnly Property Name() As String
      Get
        Name = _name
      End Get
    End Property

    Public Function statement() As String
      Dim totalAmount As Double
      Dim frequentRenterPoints As Integer
      Dim result As String
      Dim item As Rental
      totalAmount = 0
      frequentRenterPoints = 0
      result = "Rental Record for " + Name + Chr(13)
      For Each item In _rentals
        Dim thisAmount As Double
        thisAmount = 0
        ' 一行ごとに金額を計算
        Select Case item.Movie.PriceCode
        Case Movie.enumPriceCode.REGULAR
          thisAmount += 2
          If item.DaysRented > 2 Then
            thisAmount += (item.DaysRented - 2) * 1.5
          End If
        Case Movie.enumPriceCode.NEW_RELEASE
          thisAmount += item.DaysRented * 3
        Case Movie.enumPriceCode.CHILDRENS
          thisAmount += 1.5
          If item.DaysRented > 3 Then
            thisAmount += (item.DaysRented - 3) * 1.5
          End If
        End Select
        ' レンタルポイントを加算
        frequentRenterPoints = frequentRenterPoints + 1
        ' 新作を二日以上借りた場合はボーナスポイント
        If item.Movie.PriceCode = Movie.enumPriceCode.NEW_RELEASE And item.DaysRented > 1 Then
          frequentRenterPoints = frequentRenterPoints + 1
        End If
        ' この貸し出しに対する数値の表示
        result += Chr(9) + item.Movie.Title + Chr(9) + thisAmount.ToString() + Chr(13) + Chr(10)
        totalAmount += thisAmount
      Next
      ' フッタ部分の追加
      result += "Amount owed is " + totalAmount.ToString() + Chr(13) + Chr(10)
      result += "You earned " + frequentRenterPoints.ToString() + " frequent renter points"

      statement = result
    End Function
End Class

End Namespace