Guest User

Untitled

a guest
Jun 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. class Coffee
  2.  
  3. # We can set the coffee flavor when creating the Coffee object
  4. # By default, the flavor is 'sweet, smoky, Sumatran'.
  5. def initialize(flavor = 'sweet, smoky, Sumatran')
  6. @temperature = 80
  7. @flavor = flavor
  8. @container = 'cup'
  9. @container_material = 'ceramic'
  10. @fullness = 50
  11. end
  12.  
  13. def temperature
  14. @temperature
  15. end
  16.  
  17. def temperature!(temp)
  18. @temperature = temp
  19. end
  20.  
  21. def flavor
  22. @flavor
  23. end
  24.  
  25. def container
  26. @container
  27. end
  28.  
  29. def container_material
  30. @container_material
  31. end
  32.  
  33. def fullness
  34. @fullness
  35. end
  36.  
  37. def report
  38. puts "The #{@container_material} #{@container} is #{@fullness}% full of #{@temperature}-degree, #{@flavor} coffee."
  39. end
  40.  
  41. # Is the cup empty (fullness zero or less)?
  42. # Return true if so and false if not.
  43. def empty?
  44. if @fullness <= 0
  45. return true
  46. else
  47. return false
  48. end
  49. end
  50.  
  51. # Take a sip of the coffee, decreasing the fullness
  52. def sip
  53.  
  54. # if we're already below 10%, finish off the cup
  55. if @fullness < 10
  56. @fullness = 0
  57. # otherwise, drink a little
  58. else
  59. @fullness -= 10
  60. end
  61.  
  62. # Return the object for use in chaining
  63. self
  64.  
  65. end # end sip
  66.  
  67. end
  68.  
  69. # Program begins here
  70.  
  71. # Create a new object of class Coffee
  72. myCoffee = Coffee.new
  73.  
  74. # Print out a status report of my tasty coffee
  75. myCoffee.report
  76.  
  77. # Take a sip of coffee
  78. myCoffee.sip
  79.  
  80. # Print another status report
  81. myCoffee.report
  82.  
  83. # Take four sips, then tell me if the cup is empty
  84. myCoffee.sip.sip.sip.sip
  85. puts 'All gone :(' if myCoffee.empty?
  86.  
  87. # Wayne gets coffee (of a sort) too
  88. waynesCoffee = Coffee.new('foul, watery')
  89.  
  90. # Let's see how Wayne's coffee is doing
  91. waynesCoffee.report
Add Comment
Please, Sign In to add comment