#include <iostream>
#include <string>
using namespace std;

const int MAXPHONEBOOKENTRY = 20;//저장 갯수제한

string nameData[] = {"Park SH", "Kang JH", "Kim KS", "Lee YH", "Kang SH",
"Bae JM", "Lee DI", "Lee BH", "Chang WH", "Chun WY"};
int numberData[] = {5023, 5002, 5008, 5067, 5038, 5381, 5125, 5165, 5684, 5752};

class Entry {
public:
        Entry(){//디폴트 생성자
                phoneName = "";
                phoneNumber = -1;
        }
        Entry(string s, int n) {//생성자
                phoneName = s;
                phoneNumber = n;
        }
        string phoneName;//이름 변수
        int phoneNumber;//번호 변수
};

Entry arrayPhoneBook[MAXPHONEBOOKENTRY];//배열선언
int lastp = 0;//현재저장된 갯수

void insert(string name, int number);
void remove(string name);
void update(string name, int number);
int find(string name);//함수 오버로딩사용
void find(int num);

int findLoc(string name){//이름 검색 함수
        for (int i=0; i<lastp; ++i){
                if (arrayPhoneBook[i].phoneName == name)
                        return i;
        }
        return -1; // not found 비어있을때 반환
}

void find(int num){//번호 검색함수 
        for(int i=0;i<lastp;++i){
                if(arrayPhoneBook[i].phoneNumber==num){
                        cout<<"이름 : " <<arrayPhoneBook[i].phoneName<<"번호 : "<<arrayPhoneBook[i].phoneNumber<<endl;
                        return;
                }
        }
        cout<<"**Error -- No data"<<endl;
}

int find(string name){//이름 검색 함수
        int loc = findLoc(name);
        if (loc == -1)
                return -1; // not found
        return arrayPhoneBook[loc].phoneNumber;//전화번호 출력
}

void insert(string name, int number){//추가함수
        int loc = findLoc(name);
        if (loc == -1){ // insert 에러 메세지 출력 
                if (lastp > MAXPHONEBOOKENTRY){//최대 저장 갯수보다 더 저장하려 했을때
                        cout << "***Error -- PhoneBook Overflow" << endl;
                }
                else {//lastp가 MAXPHONEBOOKENTRY보다 적으면 저장
                        arrayPhoneBook[lastp].phoneName = name;
                        arrayPhoneBook[lastp].phoneNumber = number;
                        lastp++;//카운트 변수 +1
                }
        }
        else {//그자리에 이름이 저장됐을시
                cout << "***Error -- Duplicated Name" << endl;
        }
}
void remove(string name){//지우는 함수
        int loc = findLoc(name);
        if (loc != -1){ // there exist name
                // remove array entry at loc
                for (int i=loc+1; i<lastp; ++i){//지우는 배열 번째수부터 뒤에 번호들을 앞으로 당김
                        arrayPhoneBook[i-1].phoneName = arrayPhoneBook[i].phoneName;
                        arrayPhoneBook[i-1].phoneNumber = arrayPhoneBook[i].phoneNumber;
                }
                lastp--;//카운트 변수 -1
        }
        else {//존재 하지 않을때 출력
                cout << "***Error -- Name not found" << endl;
        }
}
void update(string name, int number){//입력 or 제거 함수
        remove(name);
        insert(name, number);
}
void listAll(){//모든 번호 출력
        cout << "Name" << "        " << "Number" << endl;
        for (int i=0; i<lastp; ++i){
                cout << arrayPhoneBook[i].phoneName << "        "
                        <<        arrayPhoneBook[i].phoneNumber << endl;
        }
}
int main ()
{
        int incount = 9;
        
        for (int i=0; i < incount; ++i){//array 초기화
                insert(nameData[i], numberData[i]);
        }

        insert("Kim SH", 1883);//5개 입력
        insert("Lee EJ", 0562);
        insert("Choi KJ", 9523);
        insert("Jung SW", 2341);
        insert("Jeon JH", 7818);

        remove("Kim KS");//삭제
        update("Chang WH",5555);//업데이트


        cout << "List All Inserted Entry" << endl;
        listAll();        //모든 리스트 출력
        find(5002);//검색

        
        return 0;
}