๊ด€๋ฆฌ ๋ฉ”๋‰ด

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:58
728x90
๋ฐ˜์‘ํ˜•

๐Ÿฆ€ 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
๋ฐ˜์‘ํ˜•
Comments