Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Coffee
- # We can set the coffee flavor when creating the Coffee object
- # By default, the flavor is 'sweet, smoky, Sumatran'.
- def initialize(flavor = 'sweet, smoky, Sumatran')
- @temperature = 80
- @flavor = flavor
- @container = 'cup'
- @container_material = 'ceramic'
- @fullness = 50
- end
- def temperature
- @temperature
- end
- def temperature!(temp)
- @temperature = temp
- end
- def flavor
- @flavor
- end
- def container
- @container
- end
- def container_material
- @container_material
- end
- def fullness
- @fullness
- end
- def report
- puts "The #{@container_material} #{@container} is #{@fullness}% full of #{@temperature}-degree, #{@flavor} coffee."
- end
- # Is the cup empty (fullness zero or less)?
- # Return true if so and false if not.
- def empty?
- if @fullness <= 0
- return true
- else
- return false
- end
- end
- # Take a sip of the coffee, decreasing the fullness
- def sip
- # if we're already below 10%, finish off the cup
- if @fullness < 10
- @fullness = 0
- # otherwise, drink a little
- else
- @fullness -= 10
- end
- # Return the object for use in chaining
- self
- end # end sip
- end
- # Program begins here
- # Create a new object of class Coffee
- myCoffee = Coffee.new
- # Print out a status report of my tasty coffee
- myCoffee.report
- # Take a sip of coffee
- myCoffee.sip
- # Print another status report
- myCoffee.report
- # Take four sips, then tell me if the cup is empty
- myCoffee.sip.sip.sip.sip
- puts 'All gone :(' if myCoffee.empty?
- # Wayne gets coffee (of a sort) too
- waynesCoffee = Coffee.new('foul, watery')
- # Let's see how Wayne's coffee is doing
- waynesCoffee.report
Add Comment
Please, Sign In to add comment