I am a beginner would like to hear some feedback on my code.
Thank you, guys!
// Using a hash map and vectors,
// create a text interface to allow a user to add employee names to a department in a company.
// For example, “Add Sally to Engineering” or “Add Amir to Sales.”
// Then let the user retrieve a list of all people in a department
// or all people in the company by department,
// sorted alphabetically.
use std::collections::HashMap;
use std::io;
fn main() {
let mut emp_list = HashMap::new();
loop {
println!("Select a number:");
println!("[1] Add Employee");
println!("[2] Retrieve a list");
println!("[3] Quit");
let mut response = String::new();
io::stdin().read_line(&mut response).expect("error");
let response = response.trim();
match response {
"1" => {
println!("Input Add <EMPLOYEE NAME> to <DEPARTMENT>");
let mut emp_rec = String::new();
io::stdin().read_line(&mut emp_rec).expect("error");
let emp_rec = emp_rec.trim();
let emp_rec_count = emp_rec.split_ascii_whitespace().count();
if emp_rec_count > 3 {
let emp_rec: Vec<&str> = emp_rec.split_ascii_whitespace().collect();
let emp_name = emp_rec.get(1).unwrap().to_string();
let dept = emp_rec.get(3).unwrap().to_string();
emp_list.insert(emp_name, dept);
} else {
println!("Format should be 'Add <EMPLOYEE NAME> to <DEPARTMENT>'");
}
}
"2" => {
println!("Select a number:");
println!("[1] list of all people in a department");
println!("[2] all people in the company by department, sorted alphabetically");
println!("[3] Quit");
let mut response = String::new();
io::stdin().read_line(&mut response).expect("error");
let response = response.trim();
match response {
"1" => {
println!("Input department to retrieve a list of all people in a department:");
let mut dept = String::new();
io::stdin().read_line(&mut dept).expect("error");
let dept = dept.trim();
for (k, v) in &emp_list {
if dept == v {
println!("{k} at {v}");
}
}
},
"2" => {
println!("All people in the company by department, sorted alphabetically");
let mut emp_rec: Vec<String> = Vec::new();
for (k, v) in &emp_list {
emp_rec.push(format!("{k} at {v}"));
}
emp_rec.sort();
println!("{:?}", emp_rec);
},
"3" => break,
_ => (),
}
}
"3" => break,
_ => (),
}
}
}