#!/usr/local/bin/ruby require 'test/unit' #require 'foo' #require 'test' #require "./self_samplecode" #self_samplecode.rb class Movie REGULAR = 0 NEW_RELEASE = 1 CHILDRENS = 2 attr_reader :title attr_accessor :price_code def initialize(title, price_code) @title, @price_code = title, price_code end end class Rental attr_reader :movie, :days_rented def initialize(movie, days_rented) @movie, @days_rented = movie, days_rented end end class Customer attr_reader :name def initialize(name) @name = name @rentals = [] end def add_rental(arg) @rentals << arg end def statement total_amount, frequent_renter_points = 0, 0 result = "Rental Record for #{@name}\n " @rentals.each do |element| this_amount = 0 # 各行の金額を計算 case element.movie.price_code when Movie::REGULAR this_amount += 2 this_amount += (element.days_rented - 2) * 1.5 if element.days_rented > 2 when Movie::NEW_RELEASE this_amount += element.days_rented * 3 when Movie::CHILDRENS this_amount += 1.5 this_amount += (element.days_rented - 3) * 1.5 if element.days_rented > 3 end # レンタルポイントを加算 frequent_renter_points += 1 # 新作2日間レンタルでボーナス点を加算 if element.movie.price_code == Movie::NEW_RELEASE && element.days_rented > 1 frequent_renter_points += 1 end # このレンタルの料金を表示 result += "\t" + element.movie.title + "\t" + this_amount.to_s + "\n" total_amount += this_amount end # フッター行を追加 result += "Amount owed is #{total_amount}\n" result += "You earned #{frequent_renter_points} frequent renter points" result end end #require "./samplecode" class Aaa < Test::Unit::TestCase def setup @jaws = Movie.new("Jaws", Movie::NEW_RELEASE) @first_rental = Rental.new(@jaws, 3) @snowmi = Customer.new("Snowmi") @snowmi.add_rental(@first_rental) end def test_statement # assert_equal(予想される値, 変数やメソッド等) assert_equal("aaa", @snowmi.add_rental(@first_rental)) end end =begin jaws = Movie.new("Jaws", Movie::NEW_RELEASE) et = Movie.new("ET", Movie::REGULAR) anpanman = Movie.new("Anpanman", Movie::CHILDRENS) first_rental = Rental.new(jaws, 3) second_rental = Rental.new(et, 5) third_rental = Rental.new(anpanman, 2) snowmi = Customer.new("Snowmi") snowmi.add_rental(first_rental) puts "----- Only first_rental -----" puts snowmi.statement snowmi.add_rental(second_rental) puts "----- Add second_rental -----" puts snowmi.statement snowmi.add_rental(third_rental) puts "----- Add third_rental -----" puts snowmi.statement =end #puts "html section" #puts snowmi.html_statement