Guest User

Untitled

a guest
Jan 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. use std::io::{self, Read};
  2.  
  3. pub trait CaffeineBeverageWithHook {
  4. fn prpare_recipe(&mut self) {
  5. self.boil_water();
  6. self.brew();
  7. self.pour_in_cup();
  8. if self.customer_wants_condiments() {
  9. self.add_condiments();
  10. }
  11. }
  12. fn brew(&mut self);
  13. fn add_condiments(&mut self);
  14. fn boil_water(&mut self) {
  15. println!("Boiling water");
  16. }
  17. fn pour_in_cup(&mut self) {
  18. println!("Pouring into cup");
  19. }
  20. fn customer_wants_condiments(&mut self) -> bool {
  21. true
  22. }
  23. }
  24.  
  25. struct CoffeeWithHook;
  26. impl CaffeineBeverageWithHook for CoffeeWithHook {
  27. fn brew(&mut self) {
  28. println!("Dripping Coffee through filter");
  29. }
  30. fn add_condiments(&mut self) {
  31. println!("Adding Sugar and Milk");
  32. }
  33.  
  34. fn customer_wants_condiments(&mut self) -> bool {
  35. let answer: String = self.get_user_input();
  36. if answer.to_lowercase().starts_with("y") {
  37. true
  38. } else {
  39. false
  40. }
  41. }
  42. }
  43.  
  44. impl CoffeeWithHook {
  45. fn get_user_input(&self) -> String {
  46. print!("Would you like milk and sugar with you coffee (y/n)");
  47.  
  48. let mut buffer = String::new();
  49. match io::stdin().read_line(&mut buffer) {
  50. Err(_) => panic!("IO error trying to read your anser"),
  51. Ok(i) => {
  52. if i == 0 {
  53. "no".to_string()
  54. } else {
  55. buffer
  56. }
  57. }
  58. }
  59. }
  60. }
  61.  
  62. fn main() {
  63. let mut coffee = CoffeeWithHook {};
  64. println!("\nMaking coffee...");
  65. coffee.prpare_recipe();
  66. }
Add Comment
Please, Sign In to add comment