앞서 작성했던 기능을 클래스 사용 버전으로 변경한 예
헤더파일과 소스파일을 분리하지 않고 한개의 파일에 모든 클래스를 선언함
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstring>
using namespace std;
class MemVO {
private:
int id; string name; string phone;
public:
MemVO() {}
MemVO(int id, string name, string phone) :id(id), name(name), phone(phone) {
}
void SetId(int id) { this->id = id; }
void SetName(string name) { this->name = name; }
void SetPhone(string phone) { this->phone = phone; }
int GetId() { return id; }
string GetName() { return name; }
string GetPhone() { return phone; }
};
class MemDAO {
public:
static int MemCnt;
MemDAO() {};
MemVO** GetList();
int GetCount();
bool Insert(MemVO* pMem);
MemVO* Search(int id);
bool Update(MemVO* pMem);
bool Rewrite(MemVO** arr, int sz);
bool Delete(int id);
};
class MemView {
public:
char Menu() {
cout << "메뉴: 리스트(s), 추가(i), 검색(f), 수정(u), 삭제(d), 종료(x):";
char m;
cin >> m;
return m;
}
void PrintList(MemVO** arr) {
cout << " ***** 회원정보 리스트 *****" << endl;
for (int i = 0; i < MemDAO::MemCnt; i++) {
cout << arr[i]->GetId() << "\t";
cout << arr[i]->GetName() << "\t";
cout << arr[i]->GetPhone() << endl;
}
delete[] arr;
}
MemVO* InputMem() {
int id;
string name;
string phone;
cout << "번호 이름 전화(공백으로 구분):";
cin >> id >> name >> phone;
MemVO* pMem = new MemVO(id, name, phone);
return pMem;
}
int InputSearchKey() {
cout << "검색할 회원 번호:";
int id;
cin >> id;
return id;
}
int InputDeleteKey() {
cout << "삭제할 회원 번호:";
int id;
cin >> id;
return id;
}
MemVO* InputUpdateMem() {
cout << "수정할 회원번호 전화:";
int id;
string phone;
cin >> id >> phone;
MemVO* pMem = new MemVO;
pMem->SetId(id);
pMem->SetPhone(phone);
return pMem;
}
void PrintMem(MemVO* pMem) {
if (pMem) {
cout << " ***** 검색된 회원정보 *****" << endl;
cout << pMem->GetId() << "\t" << pMem->GetName() << "\t" << pMem->GetPhone() << endl;
delete pMem;
}
else {
cout << " 검색된 정보가 없습니다" << endl;
}
}
void PrintUpdateResult(bool updated) {
string msg;
if (updated) msg = "정상적으로 회원정보를 수정했습니댜";
else msg = "회원정보를 수정하지 못했습니다";
cout << " --> 회원정보 수정결과:" << msg << endl;
}
void PrintDeleteResult(bool delted) {
string msg;
if (delted) msg = "정상적으로 회원정보를 삭제했습니댜";
else msg = "회원정보를 삭제하지 못했습니다";
cout << " --> 회원정보 삭제결과:" << msg << endl;
}
void PrintMsg(string msg) {
cout << " --> " << msg << endl;
}
void PrintError(string msg) {
cerr << " --> " << msg << endl;
}
void PrintLine() {
cout << "------------------------------------------------------------" << endl;
}
};
MemVO** MemDAO::GetList() {
MemView view;
ifstream in("D:\\test\\members.txt");
if (!in.is_open()) {
view.PrintError("파일열기 실패");
return NULL;
}
char input[50];
int lineCnt = GetCount();
int id;
string name;
string phone;
MemVO** arr = new MemVO*[lineCnt];
for (int i = 0; i < lineCnt;i++) {
in.getline(input, 50);
stringstream ss(input);
ss >> id >> name >> phone;
MemVO* pMem = new MemVO(id, name, phone);
arr[i] = pMem;
}
MemDAO::MemCnt = lineCnt;
in.close();
return arr;
}
int MemDAO::GetCount() {
MemView view;
ifstream in("D:\\test\\members.txt");
if (!in.is_open()) {
view.PrintError("파일열기 실패");
return 0;
}
char line[50];
int cnt = 0;
while (!in.eof()) {
in.getline(line, 50);
if (strlen(line)) cnt++;
}
in.close();
return cnt;
}
bool MemDAO::Insert(MemVO* pMem) {
bool Inserted = false;
MemView view;
ofstream out;
out.open("D:\\test\\members.txt", ios::app);
if (!out.is_open()) {
view.PrintError("파일열기 실패");
return false;
}
out << pMem->GetId() << " " << pMem->GetName() << " " << pMem->GetPhone() << endl;
if (out.good()) {
Inserted = true;
}
else {
Inserted = false;
}
out.close();
delete pMem;
return Inserted;
}
MemVO* MemDAO::Search(int id) {
MemView view;
ifstream in("D:\\test\\members.txt");
if (!in.is_open()) {
view.PrintError("파일열기 실패");
return NULL;
}
char line[50];
int usrId;
MemVO* pMem = NULL;
while (!in.eof()) {
in.getline(line, 50);
stringstream ss(line);
ss >> usrId;
if (id == usrId) {
string name;
string phone;
ss >> name;
ss >> phone;
pMem = new MemVO(usrId, name, phone);
break;
}
}
in.close();
return pMem;
}
bool MemDAO::Update(MemVO* pMem) {
MemView view;
ifstream in("D:\\test\\members.txt");
if (!in.is_open()) {
view.PrintError("파일열기 실패");
return false;
}
int lineCount = GetCount();
MemVO** arr = new MemVO*[lineCount];
int usrId;
string name;
bool found = false;
int i = 0;
char* line;
for (int j = 0; j < lineCount; j++) {
line = new char[50];
in.getline(line, 50);
stringstream ss(line);
ss >> usrId;
if (pMem->GetId() == usrId) {
found = true;
ss >> name;
string newLine = usrId + " " + name + " " + pMem->GetPhone();
MemVO* pM = new MemVO(usrId, name, pMem->GetPhone());
arr[i++] = pM;
}
else {
string name,phone;
ss >> name>>phone;
MemVO* pM = new MemVO(usrId, name, phone);
arr[i++] = pM;
}
}
in.close();
if (found) {
Rewrite(arr, lineCount);
}
else {
view.PrintMsg("검색결과가 없습니다");
}
}
bool MemDAO::Delete(int id) {
MemView view;
ifstream in("D:\\test\\members.txt");
if (!in.is_open()) {
view.PrintError("파일열기 실패");
return false;
}
int lineCount = GetCount();
MemVO** arr = new MemVO*[lineCount-1];
int usrId;
string name;
bool found = false;
int i = 0;
char* line;
for (int j = 0; j < lineCount; j++) {
line = new char[50];
in.getline(line, 50);
stringstream ss(line);
ss >> usrId;
if (id == usrId) {
found = true;
}
else {
string name, phone;
ss >> name >> phone;
MemVO* pM = new MemVO(usrId, name, phone);
arr[i++] = pM;
}
}
in.close();
if (found) {
bool rewrited = Rewrite(arr, lineCount-1);
return rewrited;
}
return false;
}
bool MemDAO::Rewrite(MemVO** arr, int sz) {
MemView view;
ofstream out("D:\\test\\members.txt");
if (!out.is_open()) {
view.PrintError("파일열기 실패");
return false;
}
for (int i = 0; i < sz; i++) {
out << arr[i]->GetId()<<" "<< arr[i]->GetName()<<" "<< arr[i]->GetPhone() << endl;
}
bool updated = false;
if (out.good()) updated = true;
out.close();
for (int i = 0; i < sz; i++) {
delete arr[i];
}
delete[] arr;
return updated;
}
class MemMgr
{
public:
MemView view;
MemDAO dao;
void Process()
{
bool go = true;
while (go)
{
char m = view.Menu();
switch (m)
{
case 's': memList(); break; // 파일에서 리스트를 읽어와서 MemVO객체에 저장, View에 전달
case 'i': insert(); break;
case 'f': search(); break;
case 'u': update(); break;
case 'd': remove(); break;
case 'x': go = false; break;
default: view.PrintError("입력오류");
}
}
}
void memList() {
MemVO** arr = dao.GetList();
view.PrintLine();
view.PrintList(arr);
view.PrintLine();
}
void insert() {
MemVO* pMem = view.InputMem();
string msg;
if (dao.Insert(pMem)) msg = "회원정보 추가 성공";
else msg = "회원정보 추가 실패";
view.PrintMsg(msg);
view.PrintLine();
view.PrintList(dao.GetList());
view.PrintLine();
}
void search() {
MemVO* pMem = dao.Search(view.InputSearchKey());
view.PrintLine();
view.PrintMem(pMem);
view.PrintLine();
}
void update() {
MemVO* pMem = view.InputUpdateMem();
if (!pMem) {
view.PrintMsg("검색 결과가 없습니다");
return;
}
bool updated = dao.Update(pMem);
view.PrintUpdateResult(updated);
view.PrintLine();
view.PrintList(dao.GetList());
view.PrintLine();
}
void remove() {
int del = view.InputDeleteKey();
bool deleted = dao.Delete(del);
view.PrintDeleteResult(deleted);
view.PrintLine();
view.PrintList(dao.GetList());
view.PrintLine();
}
};
int MemDAO::MemCnt = 0;
int main()
{
cout << "MVC 패턴으로 텍스트 파일 다루기" << endl;
MemMgr mgr;
mgr.Process();
cout << "프로그램 종료" << endl;
return 0;
}