#ifndef _LOC_
#define _LOC_
#define LOCKER 20 // 좌석수
class Locker {
protected:
char* studentID; // 사용자 ID
int lockerNumber; // 사물함 번호
int exist[LOCKER]; // 사물함 신청 여부 LockerStatus \ Status.txt (0 = 빈자리, 1 = 사용중)
public:
Locker(); // 생성자
~Locker(); // 소멸자
void saveID(char* id); // 사용자 ID 저장
void lockerStatus(); // 사물함 현황
void lockerChoice(); // 사물함 번호 선택
void writeStatus(); // LockerStatus \ Status.txt 에 사물함 현황을 쓴다. (0 = 빈자리, 1 = 사용중)
void readStatus(); // LockerStatus \ Status.txt 에서 자리 현황을 읽는다. (0 = 빈자리, 1 = 사용중)
void writeLocker(char* id, int site); // ID \ Locker.txt 에 신청된 사물함 번호를 쓴다.
void readLocker(); // ID \ Locer.txt 에서 신청된 사물함 번호를 읽는다
void printLocker(); // ID 와 신청된 사물함 번호를 출력
void returnLocker(); // 사물함 반납
};
#endif
#include <iostream>
#include <conio.h>
#include "Member.h"
#include "Locker.h"
#include "OccupiedPrint.h"
using namespace std;
Locker::Locker() {
studentID = new char[1];
studentID = NULL;
// LockerStatus \ Status.txt ( 0 = 빈자리 )
for(int i=0; i<LOCKER; i++) {
exist[i] = 0;
}
// ID \ Locker.txt ( 0 = 빈자리 )
lockerNumber = 0;
}
Locker::~Locker() {
delete []studentID;
}
void Locker::saveID(char* id) {
studentID = new char[strlen(id)+1];
strcpy(studentID, id);
}
void Locker::lockerStatus() {
// 사물함 현황 화면 설계
readStatus();
system("cls");
int i;
cout << "┌─01─┐┌─02─┐┌─03─┐┌─04─┐┌─05─┐"<< endl;
for(i=0; i<5; i++) {
if(exist[i] == 0) {
cout << "│";
cout << "";
cout << " │";
}else {
cout << "│";
cout << "사용중";
cout << "│";
}
}
cout << endl;
cout << "└───┘└───┘└───┘└───┘└───┘"<< endl;
cout << "┌─06─┐┌─07─┐┌─08─┐┌─09─┐┌─10─┐"<< endl;
for(i=5; i<10; i++) {
if(exist[i] == 0) {
cout << "│";
cout << "";
cout << " │";
}else {
cout << "│";
cout << "사용중";
cout << "│";
}
}
cout << endl;
cout << "└───┘└───┘└───┘└───┘└───┘"<< endl;
cout << "┌─11─┐┌─12─┐┌─13─┐┌─14─┐┌─15─┐"<< endl;
for(i=10; i<15; i++) {
if(exist[i] == 0) {
cout << "│";
cout << "";
cout << " │";
}else {
cout << "│";
cout << "사용중";
cout << "│";
}
}
cout << endl;
cout << "└───┘└───┘└───┘└───┘└───┘"<< endl;
cout << "┌─16─┐┌─17─┐┌─18─┐┌─19─┐┌─20─┐"<< endl;
for(i=15; i<20; i++) {
if(exist[i] == 0) {
cout << "│";
cout << "";
cout << " │";
}else {
cout << "│";
cout << "사용중";
cout << "│";
}
}
cout << endl;
cout << "└───┘└───┘└───┘└───┘└───┘"<< endl;
}
void Locker::lockerChoice() {
readStatus();
readLocker();
// 사용중인 사물함이 없으면.. ID \ Locker.txt ( 0 = 사용중인 사물함 없음 )
if(lockerNumber == 0) {
lockerStatus();
int choice;
do{
cout<<" 사물함 번호 입력 < 취소시 0 입력 > : ";
cin>> choice;
if(choice < 0 || choice > LOCKER){
cin.clear();
fflush(stdin);
cout << " 잘못된 입력입니다." << endl;
}
if(choice >= 0 && choice <= LOCKER){
if(choice == 0) {
break;
//아무도 사용하고 있지 않으면.. LockerStatus \ Status.txt ( 0 = 빈자리 )
}else if(exist[choice-1] == 0) {
exist[choice-1] = 1;
Member student;
student.readLog(studentID);
lockerNumber = choice;
writeStatus();
// Locker class
writeLocker(studentID, lockerNumber);
break;
}
}
}while(1);
}else {
OccupiedPrint p;
p.occupiedPrint();
getch();
}
}
void Locker::writeStatus() {
FILE* fp = fopen("LockerStatus\Status.txt", "wt");
if(fp != NULL)
{
// LockerStatus \ Status.txt
// 0 = 빈자리, 1 = 사용중
for(int i=0; i<LOCKER; i++) {
if(exist[i] == 0) {
fprintf(fp,"%d ", exist[i]);
}
else if(exist[i] == 1) {
fprintf(fp,"%d ", exist[i]);
}
}
}
fclose(fp);
}
void Locker::readStatus() {
FILE* fp = fopen("LockerStatus\Status.txt", "rt");
if(fp != NULL) {
for(int i=0; i<LOCKER; i++) {
fscanf(fp,"%d ",&exist[i]);
}
}
fclose(fp);
}
void Locker::writeLocker(char* id, int choice) {
//Locker.txt 경로 설정
char locker[] = "\Locker.txt";
char* filePath = new char[strlen(id) + strlen(locker) + 1];
//ID \ Locker.txt
strcpy(filePath, id);
strcat(filePath, locker);
FILE* fp = fopen(filePath,"wt");
fprintf(fp,"%d", choice);
fclose(fp);
delete []filePath;
}
void Locker::readLocker() {
//Locker.txt 경로 설정
char locker[] = "\Locker.txt";
char* filePath = new char[strlen(studentID) + strlen(locker) + 1];
//ID \ Locker.txt
strcpy(filePath, studentID);
strcat(filePath, locker);
FILE* fp = fopen(filePath,"rt");
fscanf(fp,"%d", &lockerNumber);
fclose(fp);
delete []filePath;
}
void Locker::printLocker() {
system("cls");
// 이미 사용중인 사물함이 있을때..
cout << " * 1개의 사물함을 사용중 입니다. *" << endl;
//cout << " ID : " << studentID << endl;
//cout << " Locker Number : " << lockerNumber << endl;
}
void Locker::returnLocker() {
readStatus();
readLocker();
if(lockerNumber !=0) {
exist[lockerNumber-1] = 0; //LockerStatus.txt//
lockerNumber = 0; // ID \ Locker.txt //
writeStatus();
writeLocker(studentID, 0);
system("cls");
cout<<" * 반납 완료 *";
getch();
}else {
system("cls");
cout<<" * 신청된 사물함이 없습니다. *";
getch();
}
}
이 클래스에 너무 많은 메소드가 있어서 분리 하려고하는데
자꾸 오류가난다 ㅠㅠ
일단 내가하는방법은 클래스 하는를 생성한다음 Locker클래스를 상속받은다음
Locker cpp에 그대로 객체를 생성해서 사용해봣는데
자꾸 버그가 난다...............
이거 어떤식으로 분리해야 돼는지좀 알려줘
네 해결법 메일로 보내드렸습니다. ^*