Guest User

Untitled

a guest
Oct 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. class Person
  2. attr_accessor :name,:age,:drink
  3.  
  4. def cry
  5. puts "#{self.name}: Whaaa-Whaaa"
  6. end
  7.  
  8. def laugh
  9. puts "#{self.name}: hahahaha great"
  10. end
  11.  
  12. def ask_name(person)
  13. puts self.name+": What is your name?"
  14. puts "#{person.name}: My name is #{person.name}"
  15. person.name #the return keyword is not needed
  16. end
  17.  
  18. def ask_age(person)
  19. puts self.name+": How old are you?"
  20. person.say_age
  21. person.age #the return keyword is not needed
  22. end
  23.  
  24. def say_age
  25. puts "#{self.name}: I am #{self.age} years old"
  26. end
  27.  
  28. def order(bartender,drink)
  29. if bartender.class == Bartender
  30. puts self.name+": I would like a "+drink
  31. @drink = bartender.give(self,drink)
  32. @drink.nil? ? cry : laugh
  33. else
  34. puts "#{self.name}: Oh sorry! I thought you were a bartender"
  35. end
  36. end
  37. end
  38.  
  39.  
  40. class Bartender < Person
  41. $drinks = ["Beer","Whiskey","Rum","Mojito"]
  42.  
  43. def give(person,drink)
  44. if(person.age.to_i >=18)
  45. if $drinks.include?(drink)
  46. puts "#{self.name}: Here is your #{drink}"
  47. drink
  48. else
  49. puts "#{self.name}: I dont have any #{drink}"
  50. end
  51. else
  52. puts "#{self.name}: You are a minor, you cant buy alcohol"
  53. nil
  54. end
  55. end
  56.  
  57. def say_age
  58. puts "#{self.name}: I dont say my age im a bartender"
  59. end
  60. end
  61.  
  62.  
  63. john = Bartender.new
  64. john.age = 35
  65. john.name = "John Petrucci"
  66.  
  67. steve = Person.new
  68. steve.age = 23
  69. steve.name = "Steve Vai"
  70.  
  71. kid = Person.new
  72. kid.age = "16"
  73. kid.name = "Joe Satriani"
  74.  
  75. steve.ask_name(john)
  76. steve.ask_age(john)
  77. steve.order(john,"Beer")
  78. kid.order(john,"Whiskey")
  79. steve.ask_age(kid)
Add Comment
Please, Sign In to add comment