Student Management System

 Object Oriented Programming의 정수는 implementation과 interface를 분리하여 정확한 설계 원리를 모르고도 사용할 수 있게 하는 것이다.

그런 의미에서 내가 만든 student management program을 올려본다. 자세한 implementation은 (설명하겠지만) 굳이 몰라도 무방하며, 각 객체가 가지는 interface만 적절히 사용하면 된다.

 //main ------------------------------------------------------------------------------------

#include<iostream>
#include<string>
#include<fstream>
#include "StudentManagement.h"
#include "Student.h"

using namespace std;
int checkint = 0; // 기존 file 유무 체크

void Run(StudentManagement stm,ifstream*myifilepointer,string argv) {
    int selection = 0; //switch 문 선택지
    string fileinput; // 기존 file에서 한 줄씩 읽어 저장

    if (checkint == 1) {
        while (!(*myifilepointer).eof()) {
            fileinput = "";
            getline((*myifilepointer), fileinput);
            Student* std = NULL;
            std = new Student;
            if (fileinput.size() == 0) break;
            (*std).set_name(fileinput.substr(0, 15)); // substring으로 잘라서 student의 각 member에 저장.
            (*std).set_StudentID(fileinput.substr(16, 10));
            (*std).set_BirthYear(fileinput.substr(27, 4));
            (*std).set_Department(fileinput.substr(32, 20)); //dept는 20자로 제한.
            (*std).set_Tel(fileinput.substr(53, 12));
            stm.insert(std); // stm stack에 기존 파일에 있던 학생들 모두 push.
        }
        (*myifilepointer).clear();
        (*myifilepointer).close();
    }
    ofstream myofile(argv, ios::out | ios::trunc); // 기존 파일을 다 읽고 (혹은 없다면) 새 파일 작성.
    ofstream* myofilepointer = &myofile;
    while (1) {
        cout << "\n1. Insertion\n2. Search\n3. Sorting Option\n4. Exit\n>_";
        selection = 0;
        cin >> selection; cin.ignore();
        switch (selection) {
        case 1:
            stm.insertion();
            break;
        case 2:
            stm.search();
            break;
        case 3:
            stm.sort();
            break;
        case 4:
            stm.filewrite(myofilepointer);
            cout << "filewrite complete!\n";
            myofile.close();
            return;
        default:
            cout << "Unkown input\n";
            break;
        }
    }
}


int main(int argc, char* argv[]) { //이렇게 하면 .exe가 있는 곳에 file.txt가 생성된다.
    ifstream myifile(argv[1]);
    ifstream* myifilepointer = &myifile;
    if (myifile.is_open()) { // 기존 파일 있는지 확인
        checkint = 1;
        cout << "existing file opened!\n";
    }
    else cout << "new file opened!\n";

    StudentManagement stm;
    Student* std=NULL;
    std = new Student;

    Run(stm,myifilepointer,argv[1]);
    cout << "run end" << endl;
    cout << "program ended successfully!\n";
    return 0;
}

//main ------------------------------------------------------------------------------------

 

//Student.h ------------------------------------------------------------------------------

#pragma once
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Student { // 개별 학생 클래스
private:
    string name;
    string StudentID;
    string BirthYear;
    string Department;
    string Tel;

public:
    void set_name(string name) {
        this->name = name;
    }
    void set_StudentID(string id) {
        this->StudentID = id;
    }
    void set_BirthYear(string bd) {
        this->BirthYear = bd;
    }
    void set_Department(string dept) {
        this->Department = dept;
    }
    void set_Tel(string tel) {
        this->Tel = tel;
    }

    string get_name() {
        return this->name;
    }
    string get_ID() {
        return this->StudentID;
    }
    string get_bd() {
        return this->BirthYear;
    }
    string get_dept() {
        return this->Department;
    }
    string get_tel() {
        return this->Tel;
    }
};

//Student.h ------------------------------------------------------------------------------

 

//StudentManagement.h ---------------------------------------------------------------

#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include "Student.h"

using namespace std;

class StudentManagement {
private:
    Student* studentList[100] = {}; //student list
    int selection=0; // switch 문 사용자 선택 입력값
    int top = 0; // student list top pointer
    string user_str; // 사용자 입력 string
    int user_int=0; // 사용자 입력 int
    int sortednumber = 0; // 사용자가 sort 했던 횟수

public:
    void insert(Student* student) {
        studentList[top] = student;
        top++;
    }
    
    Student* get_std(int top) {
        return studentList[top];
    }

    int get_top() {
        return top;
    }

    void insertion() {
        Student* student;
        student = new Student;
        string user_str = {};

        cout << "number of stored students : " << this->top << endl;
        cout << "\nName (up to 15digits) ?\n>_";
        getline(cin, user_str);
        if ((user_str.length() > 16) || (user_str.length() <= 0)) {
            cout << "wrong input!";
            return;
        }
        user_str.resize(15);
        (*student).set_name(user_str);
        
        cout << "Student ID (exactly 10 digits) ?\n>_";
        getline(cin, user_str);
        if ((user_str.length() != 10)) {
            cout << "wrong input!";
            return;
        }
        for (int i = 0; i < top; i++) { // if same id exists
            if ((*studentList[i]).get_ID().compare(user_str)==0) {
                cout << "Error : Already inserted";
                return;
            }
        }
        (*student).set_StudentID(user_str);

        cout << "BirthYear (exaclty 4 digits) ?\n>_";
        getline(cin, user_str);
        if (user_str.length() != 4) {
            cout << "wrong input!";
            return;
        }
        user_str.resize(4);
        (*student).set_BirthYear(user_str);

        cout << "Department (up to 20 character ?)\n>_";
        getline(cin, user_str);
        if (user_str.length() > 21) {
            cout << "wrong input!";
            return;
        }
        user_str.resize(20);
        (*student).set_Department(user_str);

        cout << "Tel ? (up to 12 digits)\n>_";
        getline(cin, user_str);
        if (user_str.length() > 13) {
            cout << "wrong input!\n";
            return;
        }
        user_str.resize(12);
        (*student).set_Tel(user_str);
        
        studentList[top] = student;
        top++;
        cout << "studentList[" << top << "]'s name is " << (*student).get_name() << endl;
    }

    void search() {
        cout << "\n- Search -\n1. Search by name\n2. Search by Student ID (10 numbers)\n3. Search by admission year (4 years)\n4. Search by department name\n5.List All\n>_";
        cin >> selection; cin.ignore();
        switch (selection) {
        case 1: // search by name
            cout << "Name (up to 15 digit) ?\n>_";
            getline(cin, user_str);
            user_str.resize(15);
            for (int i = 0; i < top; i++) {
                if ((*studentList[i]).get_name().compare(user_str)==0) {
                    cout << "    Name       Student ID   Birth  Department          Tel\n";
                    cout << (*studentList[i]).get_name() << " " << (*studentList[i]).get_ID() << " " << (*studentList[i]).get_bd() << " " << (*studentList[i]).get_dept() <<" " << (*studentList[i]).get_tel() << "\n";
                    return;
                }
            }
            cout << "no matches\n";
            break;
        case 2: // search by id
            cout << "Student ID (10 digit) ?\n>_";
            getline(cin, user_str);
            for (int i = 0; i < top; i++) {
                if ((*studentList[i]).get_ID().compare(user_str)==0) {
                    cout << "    Name       Student ID   Birth  Department          Tel\n";
                    cout << (*studentList[i]).get_name() << " " << (*studentList[i]).get_ID() << " " << (*studentList[i]).get_bd() << " " << (*studentList[i]).get_dept()<<" " << (*studentList[i]).get_tel() << "\n";
                    return;
                }
            }
            cout << "no matches\n";
            break;
        case 3: //search by adyear
            cout << "Addmission year (4 digit) ?\n>_";
            getline(cin, user_str);
            for (int i = 0; i < top; i++) {
                if ((*studentList[i]).get_ID().substr(0, 4).compare(user_str) == 0) { // student ID의 첫 4 digit이 admission year
                    cout << "    Name       Student ID   Birth  Department          Tel\n";
                    cout << (*studentList[i]).get_name() << " " << (*studentList[i]).get_ID() << " " << (*studentList[i]).get_bd() << " " << (*studentList[i]).get_dept() <<" " << (*studentList[i]).get_tel() << "\n";
                    return;
                }
            }
            cout << "no matches\n";
            break;
        case 4: // search by dept
            cout << "Department (up to 20 digit) ?\n>_";
            getline(cin, user_str);
            user_str.resize(20);
            for (int i = 0; i < top; i++) {
                if ((*studentList[i]).get_dept().compare(user_str)==0) {
                    cout << "    Name       Student ID   Birth  Department          Tel\n";
                    cout << (*studentList[i]).get_name() << " " << (*studentList[i]).get_ID() << " " << (*studentList[i]).get_bd() << " " << (*studentList[i]).get_dept()<<" " << (*studentList[i]).get_tel() << "\n";
                    return;
                }
            }
            cout << "no matches\n";
            break;
        case 5: // List all
            if (sortednumber == 0) {//sort by name if never sorted before
                Student* temp;
                string* unordered;
                unordered = new string[top];
                for (int i = 0; i < top; i++) {
                    unordered[i] = (*studentList[i]).get_name();
                }
                std::sort(unordered, unordered + top);
                for (int i = 0; i < top; i++) {
                    for (int j = 0; j < top; j++) {
                        if (unordered[i].compare((*studentList[j]).get_name()) == 0) {
                            temp = studentList[i];
                            studentList[i] = studentList[j];
                            studentList[j] = temp;
                        }
                    }
                }
            }
            cout << "                   - List All -\n";
            cout << "    Name       Student ID   Birth  Department          Tel\n";
            for (int i = 0; i < top; i++) {
                cout << (*studentList[i]).get_name() << " " << (*studentList[i]).get_ID() << " " << (*studentList[i]).get_bd() << " " << (*studentList[i]).get_dept()<<" " << (*studentList[i]).get_tel() << "\n";
            }
            break;
        default:
            cout << "wrong input!\n";
            break;
        }
    }

    void sort() {
        Student* temp=nullptr;
        string* unordered=nullptr; //입력된 학생 수만큼 동적할당.
        cout << "\n- Sorting Option -\n1. Sort by name\n2. Sort by Student ID (10 numbers)\n3. Sort by admission year (4 digits)\n4. Sort by department name\n>_";
        cin >> selection;
        switch (selection) {
        case 1: // sort by name(default)
            unordered = new string [top];
            for (int i = 0; i < top; i++) {
                unordered[i] = (*studentList[i]).get_name();
            }
            std::sort(unordered,unordered+top);
            for (int i = 0; i < top; i++) {
                for (int j = 0; j < top; j++) {
                    if (unordered[i].compare((*studentList[j]).get_name()) == 0) {
                        temp = studentList[i];
                        studentList[i] = studentList[j];
                        studentList[j] = temp;
                    }
                }
            }
            delete[] unordered;
            sortednumber++; //sorted 횟수 카운팅
            cout << "Sorting complete!\n";
            break;
        case 2: // sort by id
            unordered = new string[top];
            for (int i = 0; i < top; i++) {
                unordered[i] = (*studentList[i]).get_ID();
            }
            std::sort(unordered, unordered + top);
            for (int i = 0; i < top; i++) {
                for (int j = 0; j < top; j++) {
                    if (unordered[i].compare((*studentList[j]).get_ID()) == 0) {
                        temp = studentList[i];
                        studentList[i] = studentList[j];
                        studentList[j] = temp;
                    }
                }
            }
            delete[] unordered;
            sortednumber++;
            cout << "Sorting complete!\n";
            break;
        case 3: //sort by adyear
            unordered = new string[top];
            for (int i = 0; i < top; i++) {
                unordered[i] = (*studentList[i]).get_ID().substr(0,4);
            }
            std::sort(unordered, unordered + top);
            for (int i = 0; i < top; i++) {
                for (int j = 0; j < top; j++) {
                    if (unordered[i].compare((*studentList[j]).get_ID().substr(0,4)) == 0) {
                        temp = studentList[i];
                        studentList[i] = studentList[j];
                        studentList[j] = temp;
                    }
                }
            }
            delete[] unordered;
            sortednumber++;
            cout << "Sorting complete!\n";
            break;
        case 4: // sort by dept
            unordered = new string[top];
            for (int i = 0; i < top; i++) {
                unordered[i] = (*studentList[i]).get_dept();
            }
            std::sort(unordered, unordered + top);
            for (int i = 0; i < top; i++) {
                for (int j = 0; j < top; j++) {
                    if (unordered[i].compare((*studentList[j]).get_dept()) == 0) {
                        temp = studentList[i];
                        studentList[i] = studentList[j];
                        studentList[j] = temp;
                    }
                }
            }
            delete[] unordered;
            sortednumber++;
            cout << "Sorting complete!\n";
            break;
        default:
            cout << "wrong input!\n";
            cin.ignore();
            return;
            break;
        }
    }

    void filewrite(ofstream* myfile) { // write stack data into file1.txt
        for (int i = 0; i < top;i++) {
            (*myfile) << (*studentList[i]).get_name() << " " << (*studentList[i]).get_ID() << " " << (*studentList[i]).get_bd() << " " << (*studentList[i]).get_dept() << " " << (*studentList[i]).get_tel() << "\n";
        }
        cout << top << endl;
        for (int i = 0; i < top; i++) { // memory delete
            delete studentList[i];
        }
    }
};

//StudentManagement.h ---------------------------------------------------------------
 

 

 

 


댓글

이 블로그의 인기 게시물

IIKH Class from Timothy Budd's introduction to OOP

Compiler 9 - Efficient Code generation

Software Engineering 10 - V&V, SOLID principle