티스토리 뷰

내가 만든 프로그램

[C++] Console VideoShop

개발하는꼬물이 2018. 2. 17. 15:53

제작년 회사에 갓 입사했을 때, 일을 안주는데 놀긴 그렇고.. 해서 만들어 봤던 간단한 프로그램


#define _CRT_SECURE_NO_WARNINGS  // 보안 경고 무시 (vs 2013 이하 버전에서는 생략 가능)

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

const int NUMBER_LEN = 20;
const int CUSTNAME_LEN = 20;
const int VIDEONAME_LEN = 100;
const int GENRE_LEN = 20;

class Customer  // 회원 클래스 
{
private:
	char* customername;
	char* phone;
	int rental;

public:
	Customer() {}
	Customer(char* customername, char* phone, int rental)
	{
		this->customername = new char[strlen(customername) + 1];
		strcpy(this->customername, customername);
		this->phone = new char[strlen(phone) + 1];
		strcpy(this->phone, phone);
		this->rental = rental;
	}

	char* GetPhone()
	{
		return phone;
	}

	char* GetCustomerName()
	{
		return customername;
	}

	int GetRental()
	{
		return rental;
	}

	void GetCRentalInfo(char* phone)
	{
		phone = phone;
	}

	void ChangeRentalInfo(int videonumber)
	{
		rental = videonumber;
	}

	void ShowCustomer()
	{
		cout << "    " << customername << "       " << phone << "          " << rental << endl;
	}
};

class Video  // 비디오 클래스 
{
private:
	int videonumber;
	char* videoname;
	char* genre;
	char* rentalinfo;

public:
	Video() {}
	Video(int videonumber, char* videoname, char* genre, char* rentalinfo)
	{
		this->videonumber = videonumber;
		this->videoname = new char[strlen(videoname) + 1];
		strcpy(this->videoname, videoname);
		this->genre = new char[strlen(genre) + 1];
		strcpy(this->genre, genre);
		this->rentalinfo = new char[strlen(rentalinfo) + 1];
		strcpy(this->rentalinfo, rentalinfo);
	}

	int GetVideoNumber()
	{
		return videonumber;
	}

	char* GetVideoName()
	{
		return videoname;
	}

	char* GetGenre()
	{
		return genre;
	}

	char* GetVRentalInfo()
	{
		return rentalinfo;
	}

	void ChangeRentalInfo(const char* phone)
	{
		strcpy(rentalinfo, phone);
	}

	void ShowVideo()
	{
		cout << "     " << videonumber << "           " << videoname << "           " << genre << "       " << rentalinfo << endl;
	}
};

Customer *pCustomer[100];
Video *pVideo[100];

int custidx = 0;
int videoidx = 0;

void ShowMenu();
void AddCustomer();
void DeleteCustomer();
void AddVideo();
void DeleteVideo();
void RentalVideo();
void ReturnVideo();
void ShowCustomerList();
void ShowVideoList();
void Exit();

enum { ADDCUST = 1, DELCUST, ADDVID, DELVID, RENTAL, RETURN, SHOWCUST, SHOWVID, EXIT };

int main(void)
{
	int choice;

	while (1)
	{
		ShowMenu();
		cout << "어떤 작업을 수행하시겠습니까? (1~9 입력) : ";
		cin >> choice;
		cout << endl;

		switch (choice)
		{
		case ADDCUST:
			AddCustomer();
			break;
		case DELCUST:
			DeleteCustomer();
			break;
		case ADDVID:
			AddVideo();
			break;
		case DELVID:
			DeleteVideo();
			break;
		case RENTAL:
			RentalVideo();
			break;
		case RETURN:
			ReturnVideo();
			break;
		case SHOWCUST:
			ShowCustomerList();
			break;
		case SHOWVID:
			ShowVideoList();
			break;
		case EXIT:
			return 0;
		default:
			cout << "잘못된 선택입니다. 1~9사이의 숫자를 입력해주세요. " << endl;
			break;
		}
	}

	return 0;
}


void ShowMenu()
{
	cout << " ************ VIDEO SHOP ************ " << endl;
	cout << "            1. 회원 가입                " << endl;
	cout << "            2. 회원 탈퇴                " << endl;
	cout << "            3. 비디오 추가               " << endl;
	cout << "            4. 비디오 삭제                " << endl;
	cout << "            5. 비디오 대여               " << endl;
	cout << "            6. 비디오 반납               " << endl;
	cout << "            7. 회원 목록 조회             " << endl;
	cout << "            8. 비디오 목록 조회            " << endl;
	cout << "            9. 나가기                  " << endl;
	cout << " ************************************ " << endl;
}

void AddCustomer()
{
	char customername[CUSTNAME_LEN];
	char phone[NUMBER_LEN];
	int rental = 0;  // 대여 여부 (0이면 빌린 비디오가 없는 것)

	cout << " *************** 회원 가입 *************** " << endl;
	cout << " 회원이름 : ";
	cin >> customername;
	cout << " 전화번호 : ";
	cin >> phone;
	cout << " **************************************** " << endl;

	// 전화번호를 10, 11자리로 제대로 입력했는지 확인 -> 전화번호가 일치하는 회원이 이미 존재하면 가입 X
	// 일치하는 전화번호가 없으면 가입 O

	if (strlen(phone) == 10 || strlen(phone) == 11)
	{
		for (int i = 0; iGetPhone(), phone) == 0)
			{
				cout << "이미 등록된 회원입니다. " << endl;
				return;
			}
		}

		pCustomer[custidx++] = new Customer(customername, phone, rental);
	}
	else {
		cout << " 전화번호를 정확히 입력해주세요. " << endl;
		cout << " ex) 010xxxxxxxx(11자리), 032zzzzzzz(10자리) " << endl;
	}

}

void DeleteCustomer()
{
	char phone[NUMBER_LEN];
	int custmark = 0;
	int changeidx = 0;

	cout << " *************** 회원 탈퇴 *************** " << endl;
	cout << " 전화번호 : ";
	cin >> phone;
	cout << " **************************************** " << endl;

	//전화번호가 존재하는 전화번호인지 확인 -> 있으면 대여중인 비디오가 있는지 확인 -> 없으면 탈퇴 후
	//삭제된 회원 인덱스보다 큰 인덱스를 가진 배열 요소의 인덱스를 -1씩 바꿈   

	for (int i = 0; iGetPhone(), phone) == 0)
		{
			custmark = 1;

			if (pCustomer[i]->GetRental() == 0)
			{
				delete pCustomer[i];
				changeidx = i;

				for (int j = changeidx; j> videonumber;
	cout << " 비디오 이름 : ";
	cin >> videoname;
	cout << " 비디오 장르 : ";
	cin >> genre;
	cout << " **************************************** " << endl;

	// 비디오 번호가 0보다 큰 양수인지 확인 -> 맞으면 입력한 비디오번호가 이미 존재하는지 확인 -> 없으면 추가 

	if (videonumber == 0 || videonumber < 0)
	{
		cout << "비디오 번호는 1 이상의 숫자로 입력해야 합니다. " << endl;
		return;
	}

	for (int i = 0; iGetVideoNumber() == videonumber)
		{
			cout << "이미 동일한 비디오 번호가 존재합니다. " << endl;
			return;
		}
	}

	pVideo[videoidx++] = new Video(videonumber, videoname, genre, rentalinfo);
}

void DeleteVideo()
{
	int videonumber;
	int videomark = 0;
	int changeidx = 0;

	cout << " ************* 비디오 삭제 *************** " << endl;
	cout << " 비디오 번호 : ";
	cin >> videonumber;
	cout << " **************************************** " << endl;

	//입력한 비디오번호를 가진 비디오가 존재하는지 확인 -> 있으면 비디오가 대여중인지 확인 -> 
	//대여중이 아니라면 삭제하고, 삭제된 비디오 인덱스보다 큰 인덱스를 가진 배열 요소의 인덱스를 -1씩 바꿈   

	for (int i = 0; iGetVideoNumber() == videonumber)
		{
			videomark = 1;

			if (strcmp(pVideo[i]->GetVRentalInfo(), "0") == 0)
			{
				delete pVideo[i];
				changeidx = i;

				for (int j = changeidx; j> phone;
	cout << " 대여 할 비디오 번호 : ";
	cin >> videonumber;
	cout << " ***************************************** " << endl;

	//입력한 전화번호를 가진 회원이 존재하는지 확인 ->  있으면 비디오를 대여중인지 확인 -> 빌리고있지 않다면 
	//입력한 비디오번호를 가진 비디오가 존재하는지 확인 -> 있으면 비디오가 대여중인지 확인 -> 아무도 빌리지 않았다면 대여해줌 

	for (int i = 0; iGetPhone(), phone) == 0)
		{
			custmark = 1;

			if (pCustomer[i]->GetRental() != 0)
			{
				videomark = 1;

				cout << "비디오를 1개 이상 빌릴 수 없습니다. 반납 후 대여해주세요. " << endl;
			}
			else {

				for (int j = 0; jGetVideoNumber() == videonumber)
					{
						videomark = 1;
						if (strcmp(pVideo[j]->GetVRentalInfo(), "0") != 0)
						{
							cout << "선택하신 비디오는 현재 대여가 불가능합니다. " << endl;

						}
						else {
							pCustomer[i]->ChangeRentalInfo(videonumber);
							pVideo[j]->ChangeRentalInfo(phone);

							cout << " 대여가 완료되었습니다. " << endl;

						}
					}
				}

			}
		}
	}

	if (custmark == 1 && videomark == 0)
	{
		cout << "비디오 정보가 존재하지 않습니다." << endl;
	}
	else if (custmark == 0) {
		cout << "회원 정보가 존재하지 않습니다." << endl;
	}

}

void ReturnVideo()
{
	char phone[NUMBER_LEN];
	int videonumber;
	char returninfo[NUMBER_LEN] = "0";
	int custmark = 0;
	int videomark = 0;

	cout << " ************* 비디오 반납 *************** " << endl;
	cout << " 회원 전화번호 : ";
	cin >> phone;
	cout << " 반납 할 비디오 번호 : ";
	cin >> videonumber;
	cout << " ***************************************** " << endl;

	//회원이 존재하는지 확인 -> 존재하면 비디오가 존재하는지 확인 -> 존재하면 회원이 빌린 비디오인지 확인 -> 
	//맞으면 회원의 전화번호가 맞는지 확인->반납해줌 

	for (int i = 0; iGetPhone(), phone) == 0)
		{
			custmark = 1;

			for (int j = 0; jGetVideoNumber() == videonumber)
				{
					videomark = 1;
					if (pCustomer[i]->GetRental() == videonumber)
					{

						if (strcmp(pVideo[j]->GetVRentalInfo(), phone) == 0)
						{

							pCustomer[i]->ChangeRentalInfo(0);
							pVideo[j]->ChangeRentalInfo("0");

							cout << "비디오 반납이 완료되었습니다. " << endl;

						}
						else {
							cout << " 회원 정보를 다시 확인하고 시도해주세요. " << endl;
						}
					}
					else {
						cout << "빌린 비디오 정보를 다시 확인하고 시도해주세요. " << endl;

					}
				}
			}
		}
	}

	if (custmark == 0) {
		cout << "회원 정보가 존재하지 않습니다. " << endl;
	}
	else if (custmark == 1 && videomark == 0) {
		cout << "비디오 정보가 존재하지 않습니다. " << endl;
	}


}

void ShowCustomerList()
{
	cout << " ********************************************* " << endl;
	cout << "   회원이름       전화번호      대여비디오번호 " << endl;
	cout << " --------------------------------------------- " << endl;

	for (int i = 0; iShowCustomer();
	}

	cout << " ********************************************* " << endl;
}

void ShowVideoList()
{
	cout << " ************************************************************ " << endl;
	cout << "  비디오번호    비디오이름       장르       대여자정보  " << endl;
	cout << " ------------------------------------------------------------ " << endl;

	for (int i = 0; iShowVideo();
	}

	cout << " ************************************************************ " << endl;
}



'내가 만든 프로그램' 카테고리의 다른 글

ASCII -> Binary File Converter  (0) 2019.06.23
댓글