From: Pat Eyler Date: 2001-09-05T04:15:50+09:00 Subject: [ruby-talk:20885] Re: newbie question On 05 Sep 2001 03:48:48 +0900, Ned Konz wrote: > On Tuesday 04 September 2001 11:18 am, Pat Eyler wrote: > > I never did translate: > > > > public class Movie { > > public static final int CHILDREN = 2; > > public static final int REGULAR = 0; > > public static final int NEW_RELEASE = 1; > > } > > > > into ruby, instead I just used the magic numbers 0, 1, and 2 as needed > > in methods of other classes and ignored it figuring I would have a > > blinding flash of the obvious and go back and fix things. As can be > > expected, the blinding flash never came. > > > > Would someone mind enlightening me? > > class Movie > CHILDREN = 2 > REGULAR = 0 > NEW_RELEASE = 1 > # ... > end I'd tried this, but it fails when I try to use the values outside a Movie (see the class Price hierarchy) in the code below. This probably means that I'm doing something else wrong... ----------------------------------------------------------------------- class Movie CHILDRENS = 2 NEW_RELEASE = 1 REGULAR = 0 attr_accessor :title, :pricecode def initialize(title,pricecode) @title = title setPriceCode(pricecode) @pricecode = getPriceCode end def getPriceCode @price.getPriceCode end def setPriceCode(pricecode) case pricecode when REGULAR @price = RegularPrice.new when NEW_RELEASE @price = NewReleasePrice.new when CHILDRENS @price = ChildrensPrice.new end end def getCharge(daysRented) @price.getCharge(daysRented) end def getFrequentRenterPoints(daysRented) @price.getFrequentRenterPoints(daysRented) end end class Rental attr_accessor :aMovie, :daysRented def initialize(aMovie, daysRented) @aMovie, @daysRented = aMovie, daysRented end def getCharge aMovie.getCharge(daysRented) end def getFrequentRenterPoints aMovie.getFrequentRenterPoints(daysRented) end end class Customer attr_accessor :name def initialize(name) @name = name @rentals = Array.new end def addRental(aRental) @rentals.push(aRental) end def statement result = "\nRental Record for #{@name}\n" @rentals.each do |each| # show figures for this rental result +="\t#{each.aMovie.title}\t#{each.getCharge}\n" end result += "Amount owed is #{getTotalCharge}\n" result += "You earned #{getFrequentRenterPoints} frequent renter points" end def htmlStatement result = "\n

Rentals for #{name}

\n" @rentals.each do |each| result += "#{each.aMovie.title}: #{each.getCharge}
\n" end result += "You owe #{getTotalCharge}

\n" result += "On this rental you earned #{getFrequentRenterPoints}" + " frequent renter points

" end def getTotalCharge result = 0.0 @rentals.each do |each| result += each.getCharge() end result end def getFrequentRenterPoints result = 0 @rentals.each do |each| result += each.getFrequentRenterPoints end result end end class Price def getPrice end def getCharge(daysRented) end def getFrequentRenterPoints(daysRented) 1 end end class ChildrensPrice < Price def getPriceCode Movie.CHILDRENS end def getCharge(daysRented) result = 1.5 if daysRented > 3 result += (daysRented - 3) * 1.5 end end end class NewReleasePrice < Price def getPriceCode Movie.NEW_RELEASE end def getCharge(daysRented) daysRented * 3.0 end def getFrequentRenterPoints(daysRented) frequentRenterPoints = 1 # add bonus for a two day new release rental if ( daysRented > 1) frequentRenterPoints += 1 end frequentRenterPoints end end class RegularPrice < Price def getPriceCode Movie.REGULAR end def getCharge(daysRented) result = 2.0 if (daysRented > 2) result += (daysRented -2) * 1.5 end return result end end ------------------------------------------------------------------------- thanks, -pate > > -- > Ned Konz > currently: Stanwood, WA > email: ned@bike-nomad.com > homepage: http://bike-nomad.com