์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- ์๋ฐ ๊ธฐ์ด
- OS
- ํ์ด์ฌ ์ฒผ๋ฆฐ์ง
- ๋ฌ์คํธ ํ๋ก๊ทธ๋๋ฐ ๊ณต์ ๊ฐ์ด๋
- java
- Operating System
- ์๊ณ ๋ฆฌ์ฆ
- ์๋ฐ ๊ฐ๋
- ๋ฐ์ดํฐ ํต์
- ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ
- ์ค๋ผํด
- Database
- Reversing
- Python challenge
- data communication
- ๋ฐฑ์ค ๋ฌ์คํธ
- ๋ฐฑ์ค
- ์ค๋ผํดDB
- C
- ํ์ด์ฌ ์ฑ๋ฆฐ์ง
- ๋ฌ์คํธ
- ์ฐ๋ถํฌ
- ์๋ฐ
- ์ด์์ฒด์
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Python
- ubuntu
- ๋ฌ์คํธ ์์
- Rust
- ํ์ด์ฌ
Archives
- Today
- Total
IT’s Portfolio
[Rust] Start Rust (Day 17) - Rust Example Script 5 [Simple Employees DataBase] ๋ณธ๋ฌธ
Development Study/Rust
[Rust] Start Rust (Day 17) - Rust Example Script 5 [Simple Employees DataBase]
f1r3_r41n 2023. 2. 27. 18:58728x90
๋ฐ์ํ
๐ฆ Rust Day 17
๐ณ๏ธ Rust Example Script 5 - Simple Employees DataBase
1๏ธโฃ Description
- ๊ฐ๋จํ ์ง์ ์ ๋ณด๋ฅผ
HashMap
๊ณผVector
๋ฅผ ์ด์ฉํด ์ ์ฅํ๋ ํ๋ก๊ทธ๋จ ์์ฑ - ์ด๋ฆ์ด
Sally
์ธ ์ง์์Engineering
๋ถ์์ ์ถ๊ฐํ ๊ฒฝ์ฐadd Sally to Engineering
- ์ด๋ฆ์ด
Amir
์ธ ์ง์์Sales
๋ถ์์ ์ถ๊ฐํ ๊ฒฝ์ฐadd Amir to Sales
- ํ์ฌ ๋ด์ ๋ชจ๋ ์ง์๋ค์ ์ํ๋ฒณ ์์ผ๋ก ์ถ๋ ฅ
- ํ์ฌ ๋ด์ ๊ฐ ๋ถ์์ ์ง์๋ค์ ์ถ๋ ฅ
2๏ธโฃ how it works
- ๋ฌธ์์ด ์ ๋ ฅ๋ฐ๊ธฐ
- ์ํฉ์ ๋ง๋ ๊ฒฐ๊ณผ ์ถ๋ ฅ
3๏ธโฃ Code
// src/main.rs
mod command;
use command::*;
pub use std::{io, collections::HashMap};
fn main() {
let mut enterprise: HashMap<String, Vec<String>> = HashMap::new();
println!("**** Add Employee Information Text Interface ****");
println!("* Add => \"add {{name}} to {{department}}\" *");
println!("* Check department employees => \"{{department}}\" *");
println!("* Check enterprise employees => \"enterprise\" *");
println!("* Exit => \"exit\" *");
println!("*************************************************");
loop {
let mut query = String::new();
let mut check = 0;
println!("Input: ");
match io::stdin().read_line(&mut query) {
Ok(_) => {
if &query == "exit\n" {
break;
} else if &query == "enterprise\n" {
view_enterprise(&enterprise);
} else {
for (k, _) in &enterprise {
if k == &query.trim() {
view_department_employees(&enterprise, k);
check = 1;
}
}
if check == 0 {
match add_employee(&mut enterprise, query) {
false => {
println!("Input Error!");
continue;
},
true => {
println!("Complete!");
},
}
}
}
},
Err(_) => {
println!("Input Error!");
continue;
},
}
println!("Running...");
}
}
// src/command.rs
use std::collections::HashMap;
// ์ง์์ ํด๋น ๋ถ์์ ์ถ๊ฐํ๋ ํจ์
pub fn add_employee(map: &mut HashMap<String, Vec<String>>, query: String) -> bool {
// ๋ฉ์ธ์์ ์
๋ ฅํ ์ฟผ๋ฆฌ์ ์์ ๊ถ์ ์ด ํจ์๊ฐ ๊ฐ์ง
// ์ฟผ๋ฆฌ๋ฅผ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๋๋ ๋ฒกํฐ๋ก ๋ง๋ฌ
// ์ง์ ์ถ๊ฐ ์ฟผ๋ฆฌ๋ ํญ์ ์ผ์ ํ ๊ฒ์ด๋ค๋ฅผ ๊ฐ์ ํ๋ฉด ํด๋น ๋ฒกํฐ์ ๋ ๋ฒ์งธ ์ธ๋ฑ์ค์ ๊ฐ์
// ์ง์์ ์ด๋ฆ์ด๋ฉฐ, ๋ค ๋ฒ์งธ ์ธ๋ฑ์ค์ ๊ฐ์ ํด๋น ๋ถ์์
let split_query: Vec<&str> = query.trim().split(' ')
.map(|x| x).collect();
// ์ด์ํ ์ฟผ๋ฆฌ๊ฐ ๋ค์ด์ฌ ์ ํ๋ณํ์ฌ ๋ฐํํ bool
let mut check = false;
if split_query.len() != 4 {
check
} else {
check = true;
let k = split_query[3].to_string();
let v = split_query[1].to_string();
// ์ ๋ฌ๋ฐ์ ๊ฐ๋ณ ์ฐธ์กฐ HashMap์ ๋ณํํ๋ ์ค์ ์ฝ๋
// ๊ฐ์ ๋ฒกํฐ ์์ฒด๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ๋ฒกํฐ์ ๊ฐ์ pushํ๋ ์ฝ๋์
let data = map.entry(k)
.or_insert({
let tmp = Vec::new();
tmp
});
data.push(v);
data.sort();
check
}
}
// ํ์ฌ ๋ด์ ๋ชจ๋ ์ง์๋ค์ ์ถ๋ ฅํ๋ ํจ์
pub fn view_enterprise(map: &HashMap<String, Vec<String>>) {
let mut everyone = Vec::new();
println!("===[Enterprise]===");
for (_, v) in map {
let mut tmp = v.clone();
everyone.append(&mut tmp);
}
everyone.sort();
for i in everyone {
println!("- {}", i);
}
println!("==================");
}
// ํ์ฌ ๋ด ๊ฐ ๋ถ์์ ๋ชจ๋ ์ง์๋ค์ ์ถ๋ ฅํ๋ ํจ์
pub fn view_department_employees(map: &HashMap<String, Vec<String>>, dpm: &String) {
println!("===[Department]===");
println!("- {}", dpm);
println!("=====[Staffs]=====");
match map.get(dpm) {
Some(tmp) => {
for i in tmp {
println!("- {i}");
}
},
None => (),
}
println!("==================")
}
4๏ธโฃ Result
**** Add Employee Information Text Interface ****
* Add => "add {name} to {department}" *
* Check department employees => "{department}" *
* Check enterprise employees => "enterprise" *
* Exit => "exit" *
*************************************************
Input:
add Sally to Engineering
Complete!
Running...
Input:
add Amir to Sales
Complete!
Running...
Input:
add Hwabee to Development
Complete!
Running...
Input:
add Bokyung to Development
Complete!
Running...
Input:
enterprise
===[Enterprise]===
- Amir
- Bokyung
- Hwabee
- Sally
==================
Running...
Input:
Development
===[Department]===
- Development
=====[Staffs]=====
- Bokyung
- Hwabee
==================
Running...
Input:
Engineering
===[Department]===
- Engineering
=====[Staffs]=====
- Sally
==================
Running...
Input:
exit
728x90
๋ฐ์ํ
'Development Study > Rust' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm] Baekjoon - ์กฐ๊ฑด๋ฌธ ๋จ๊ณ (0) | 2023.04.04 |
---|---|
[Rust] Start Rust (Day 18) - Error Handling (0) | 2023.03.09 |
[Rust] Start Rust (Day 16) - Rust Example Script 4 [Pig Latin] (0) | 2023.02.25 |
[Rust] Start Rust (Day 15) - Rust Example Script 3 [Get Avg & Mid & Mode] (0) | 2023.02.25 |
[Rust] Start Rust (Day 14) - Common Collections (2) | 2023.02.20 |
Comments