C++

간단한 책 대여 프로그램

mita2024 2024. 6. 25. 23:56

 

동영상 서비스가 종료되어 해당 콘텐츠를 재생할 수 없습니다.

 

 

 

실행영상.

 

어소트락 게임아카데미 강의 영상에서 숙제로 내주었던, 간단한 책 대여 프로그램을 만들어보았습니다.

책 정보:

1. 책 제목

2. 책 번호

3. 대여 가격

4. 대여 여부

4가지를 구조체로 묶고,

등록, 대여확인, 반납, 목록, 종료 기능들을 구현하는 것입니다.

 

등록할 때, 책 제목에 글자가 하나도 없거나, 가격이 음수이면, 다시 등록을 하도록 하였습니다.

 

대여 여부에서, 먼저 책이름을 검색한 후, 일치하는 책이 있는지 확인하고, 책이 있다면 대여가 가능한지 확인한 후 대여의사를 묻도록 구현하였습니다.

 

반납 기능에서는, 빌린 책이 없으면, 대여한 책은 없다고 출력하고, 빌린 책이 있다면, 반납 의사를 물은 후, 한번에 모두 반납하도록 만들었습니다.

 

목록은 한번에, 모든 책들에 대한 정보를 출력해줍니다.

 

미숙하지만, 직접 짜본 코드를 올리겠습니다.

 

코드:

#include <iostream>

using namespace std;

#define NAME_SIZE 100
#define BOOK_SIZE 5

struct Book
{
char book_name[NAME_SIZE];
int price;
int book_id;
bool Rent_flag;

void Info()
{
cout << "Book title: " << book_name << endl;
cout << "Price: " << price <<"$"<< endl;
cout << "Book id: " << book_id << endl;

if (Rent_flag) cout << "Rent is available" << endl;
else cout << "Rent is unavailable" << endl;
}
};

enum MENU
{
MENU_NONE,
MENU_INSERT,
MENU_CHECK,
MENU_RETURN,
MENU_LIST,
MENU_EXIT
};


int main()
{
Book book_list[BOOK_SIZE] = {};
char SearchName[NAME_SIZE] = {};
int book_count = 0;
int book_number = 1;


while (true)
{
system("cls");

cout << "1. Register a book" << endl;
cout << "2. Check Rent" << endl;
cout << "3. Return books " << endl;
cout << "4. Output book list" << endl;
cout << "5. Exit" << endl;


cout << "Choose a menu: ";
int input_menu;
cin >> input_menu;

if (cin.fail())
{
cin.clear();

cin.ignore(INT32_MAX, '\n');

cout << "Wrong input." << endl;
system("pause");
continue;
}

if (input_menu == MENU_EXIT)
break;

switch (input_menu)
{
case MENU_INSERT:

system("cls");
cout << "=================== Register a book ===================" << endl;

if (book_count >= BOOK_SIZE)
{
cout << "Can not register books anymore" << endl;
break;
}

cout << "Name: ";
cin.ignore(INT32_MAX, '\n');
cin.getline(book_list[book_count].book_name, NAME_SIZE);

// Check name
int i;
i = 0;
bool name_flag;

name_flag = false;

while (book_list[book_count].book_name[i] != '\0')
{
if (isalnum(book_list[book_count].book_name[i]))
{
name_flag = true;
break;
}
i++;
}

if (!name_flag)
{
cout << "The book name is wrong" << endl;
break;
}

cout << "Price: ";
cin >> book_list[book_count].price;

// Check price
if (cin.fail())
{
cin.clear();

cin.ignore(INT32_MAX, '\n');
cout << "Wrong input in price. Try again." << endl;
break;
}
else if (book_list[book_count].price < 0)
{
cout << "The price is negative. Try again." << endl;
break;
}

book_list[book_count].book_id = book_number;
book_list[book_count].Rent_flag = true;

cout << "Register is completed" << endl;

++book_count;
++book_number;

break;

case MENU_CHECK:

system("cls");

cout << "=================== Check the availability of books ===================" << endl;

cout << "Search the name: ";
cin.ignore(INT32_MAX, '\n');
cin.getline(SearchName, NAME_SIZE);

bool fail;

fail = true;

for (int i = 0; i < book_count; ++i)
{
if (strcmp(SearchName, book_list[i].book_name) == 0)
{
cout << "Found the book." << endl;
fail = false;

if (book_list[i].Rent_flag)
{
char answer;

cout << "You can rent the book." << endl;
cout << "Do you want to rent? Press Y." << endl;

cin >> answer;

if (answer == 'y' || answer == 'Y')
{
book_list[i].Rent_flag = false;
cout << "You rented " << book_list[i].book_name << endl;
}
else
{
cout << "Rent is failed" << endl;
}
break;
}

else
cout << "You can't rent the book." << endl;

break;
}
}

if(fail)
cout << "Couldn't find the book." << endl;
break;

case MENU_RETURN:
system("cls");

cout << "=================== Return books ===================" << endl;

// search books which you borrowed
bool flag;
flag = true;

for (int i = 0; i < book_count; ++i)
{
if (book_list[i].Rent_flag == false)
flag = false;
}

if (flag)
cout << "You didn't rent any book" << endl;
else
{
char key;
cout << "If you want to books, press y" << endl;
cin >> key;

if (key == 'y' || key == 'Y')
{
for (int i = 0; i < book_count; ++i)
book_list[i].Rent_flag = true;

cout << "Return is successful." << endl;
}
else
cout << "Cancle the return." << endl;
}
break;

case MENU_LIST:
system("cls");

cout << "=================== List of books ===================" << endl;

if (book_count <= 0)
{
cout << "Here is no book" << endl;
break;
}

for (int i = 0; i < book_count; ++i)
{
cout << "No." << i + 1 << endl;
book_list[i].Info();
cout << endl;
}
break;

default:
cout << "Wrong number." << endl;
break;
}

system("pause");

}


return 0;
}

'C++' 카테고리의 다른 글

간단한 마리오 게임  (0) 2024.10.25
간단한 미로탈출 게임  (2) 2024.07.15
TEXT RPG 완료  (0) 2024.07.02
TEXT RPG 중간단계  (0) 2024.07.01
C++ 숫자빙고게임  (0) 2024.06.21