From b7ebe2cd4ab6f4b12bdc2ed6430f1cdf45b2cc33 Mon Sep 17 00:00:00 2001 From: unintendedfraud Date: Fri, 6 Oct 2023 17:17:06 +0200 Subject: [PATCH] treehouse --- treehouse/Cargo.toml | 8 ++++ treehouse/src/main.rs | 91 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 treehouse/Cargo.toml create mode 100644 treehouse/src/main.rs diff --git a/treehouse/Cargo.toml b/treehouse/Cargo.toml new file mode 100644 index 0000000..912aa8c --- /dev/null +++ b/treehouse/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "treehouse" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/treehouse/src/main.rs b/treehouse/src/main.rs new file mode 100644 index 0000000..ee06f76 --- /dev/null +++ b/treehouse/src/main.rs @@ -0,0 +1,91 @@ +use std::io::stdin; + +#[derive(Debug)] +enum VisitorAction { + Accept, + AcceptWithNote { note: String }, + Refuse, + Probation, +} + +#[derive(Debug)] +struct Visitor { + name: String, + action: VisitorAction, + age: i8, +} + +impl Visitor { + fn new(name: &str, action: VisitorAction, age: i8) -> Self { + Self { + name: name.to_lowercase(), + action, + age, + } + } + + fn greet(&self) { + match &self.action { + VisitorAction::Accept => println!("Welcome to the treehouse !"), + VisitorAction::AcceptWithNote { note } => { + println!("Welcome to the treehouse ! {}", note); + if self.age < 18 { + println!("No alcohol"); + } + } + VisitorAction::Probation => println!("Probation welcome"), + _ => println!("Get out !!"), + } + } +} + +fn main() { + let mut list = get_allowed_list(); + + loop { + println!("Hello, what's your name? (leave empty and press ENTER to quit)"); + + let name = get_name(); + + let known_visitor = list.iter().find(|v| v.name.to_lowercase() == *name); + + match known_visitor { + Some(visitor) => visitor.greet(), + None => { + if name.is_empty() { + break; + } else { + println!("{} is not on the list", name); + list.push(Visitor::new(&name, VisitorAction::Probation, 0)); + } + } + } + } + + println!("The final list:"); + println!("{:#?}", list); +} + +fn get_name() -> String { + let mut name = String::new(); + + stdin() + .read_line(&mut name) + .expect("failed to read the name"); + + name.trim().to_lowercase() +} + +fn get_allowed_list() -> Vec { + return vec![ + Visitor::new("Brice", VisitorAction::Accept, 24), + Visitor::new( + "Robert", + VisitorAction::AcceptWithNote { + note: String::from("Lactose-free drink in the fridge"), + }, + 15, + ), + Visitor::new("Michelle", VisitorAction::Refuse, 33), + ]; +}