Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::io::{self, Read};
- pub trait CaffeineBeverageWithHook {
- fn prpare_recipe(&mut self) {
- self.boil_water();
- self.brew();
- self.pour_in_cup();
- if self.customer_wants_condiments() {
- self.add_condiments();
- }
- }
- fn brew(&mut self);
- fn add_condiments(&mut self);
- fn boil_water(&mut self) {
- println!("Boiling water");
- }
- fn pour_in_cup(&mut self) {
- println!("Pouring into cup");
- }
- fn customer_wants_condiments(&mut self) -> bool {
- true
- }
- }
- struct CoffeeWithHook;
- impl CaffeineBeverageWithHook for CoffeeWithHook {
- fn brew(&mut self) {
- println!("Dripping Coffee through filter");
- }
- fn add_condiments(&mut self) {
- println!("Adding Sugar and Milk");
- }
- fn customer_wants_condiments(&mut self) -> bool {
- let answer: String = self.get_user_input();
- if answer.to_lowercase().starts_with("y") {
- true
- } else {
- false
- }
- }
- }
- impl CoffeeWithHook {
- fn get_user_input(&self) -> String {
- print!("Would you like milk and sugar with you coffee (y/n)");
- let mut buffer = String::new();
- match io::stdin().read_line(&mut buffer) {
- Err(_) => panic!("IO error trying to read your anser"),
- Ok(i) => {
- if i == 0 {
- "no".to_string()
- } else {
- buffer
- }
- }
- }
- }
- }
- fn main() {
- let mut coffee = CoffeeWithHook {};
- println!("\nMaking coffee...");
- coffee.prpare_recipe();
- }
Add Comment
Please, Sign In to add comment