👀 간단한 도서관 관리 프로그램 구현
🎯참고한 클래스 다이어그램

#pragma once
#ifndef HW04_H
#define HW04_H
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
class Book {
public:
string title;
string author;
Book(string t, string a) :title(t), author(a) {}
const void displayBook();
};
class BookManager {
private:
vector<Book> books;
Book* searchByTitle(const string& title);
vector<Book*> searchByAuthor(const string& author);
public:
BookManager() {}
void addBook(const string& title, const string& author);
void displayAllBooks();
Book* getByTitle(const string& title);
vector<Book*> getByAuthor(const string& author);
};
class BorrowManager {
private:
BookManager& bookManager;
unordered_map<Book*, int> stock;
static const int max_quantity = 3;
public:
BorrowManager(BookManager& bm) :bookManager(bm) {}
void initializeStock(Book* book, int quantity = max_quantity);
void borrowBookByTitle(const string& title);
void returnBook(const string& title);
void displayAllStock();
void displayStockByTitle(const string& title);
void displayStockByAuthor(const string& author);
};
#endif
#include "HW04.h"
#include <iostream>
//Book Public
const void Book::displayBook() {
cout << "제목: " << this->title << " 작가: " << this->author;
}
// BookManager Private
Book* BookManager::searchByTitle(const string& title) {
Book* result = nullptr;
for (auto& item : this->books) {
if (item.title == title) {
result = &item;
return result;
}
}
return result;
}
vector<Book*> BookManager::searchByAuthor(const string& author) {
vector<Book*> listBooks;
for (auto &item : this->books) {
if (item.author == author) {
listBooks.push_back(&item);
}
}
return listBooks;
}
// BookManager Public
void BookManager::addBook(const string& title, const string& author) {
this->books.push_back(Book(title, author));
}
void BookManager::displayAllBooks() {
cout << endl << "---------------책 목록---------------" << endl;
for (auto &item : this->books) {
item.displayBook();
cout << endl;
}
}
Book* BookManager::getByTitle(const string& title) {
if (this->searchByTitle(title) == nullptr) {
cout << "제목: " << title << " 책을 찾을 수 없습니다." << endl;
}
return this->searchByTitle(title);
}
vector<Book*> BookManager::getByAuthor(const string& author) {
if (this->searchByAuthor(author).size() == 0) {
cout << "작가: " << author << " 의 책을 찾을 수 없습니다." << endl;
}
return this->searchByAuthor(author);
}
// BorrowManager Public
void BorrowManager::displayStockByTitle(const string& title) {
Book* item = bookManager.getByTitle(title);
if (item != nullptr) {
item->displayBook();
cout << " 재고: " << stock[item] << endl;
}
}
void BorrowManager::displayStockByAuthor(const string& author) {
vector<Book*> items = bookManager.getByAuthor(author);
for (auto b : items) {
b->displayBook();
cout << " 재고: " << stock[b] << endl;
}
}
void BorrowManager::displayAllStock() {
cout << endl << "---------------책 재고 목록---------------" << endl;
for (auto &item : this->stock) {
item.first->displayBook();
cout<< " 재고: " << item.second << endl;
}
}
void BorrowManager::initializeStock(Book* book, int quantity) {
if (book == nullptr) {
cout << "*******initializeStock Error*******" << endl;
}
else {
this->stock[book] = quantity;
}
}
void BorrowManager::borrowBookByTitle(const string& title) {
Book* temp = this->bookManager.getByTitle(title);
cout << endl << "---------------책 대여---------------" << endl;
temp->displayBook();
if (this->stock[temp] == 0) {
cout << " 재고 부족. 대여 불가능" << endl;
}
else {
cout << " 대여 완료" << endl;
this->stock[temp]--;
}
}
void BorrowManager::returnBook(const string& title) {
Book* temp = this->bookManager.getByTitle(title);
cout << endl << "---------------책 반납---------------" << endl;
temp->displayBook();
if (this->stock[temp] == max_quantity) {
cout << " 반납 오류: 최대 재고" << endl;
}
else {
cout << " 반납 완료" << endl;
this->stock[temp]++;
}
}
#include "HW04.h"
#include <iostream>
int main()
{
BookManager book_m;
book_m.addBook("책1-1", "작가1");
book_m.addBook("책1-2", "작가1");
book_m.addBook("책2-1", "작가2");
book_m.displayAllBooks();
BorrowManager borrow_m(book_m);
borrow_m.initializeStock(book_m.getByTitle("책1-1"));
borrow_m.initializeStock(book_m.getByTitle("책1-2"));
borrow_m.initializeStock(book_m.getByTitle("책2-1"));
borrow_m.initializeStock(book_m.getByTitle("책2-2"));
while (true) {
system("cls");
cout << "******BOOK MANAGER******" << endl;
cout << "1. 책 목록 및 재고 확인" << endl;
cout << "2. 책 추가하기" << endl;
cout << "3. 제목으로 책 찾기" << endl;
cout << "4. 작가로 책 찾기" << endl;
cout << "5. 책 대여하기" << endl;
cout << "6. 책 반납하기" << endl;
cout << "7. 종료" << endl;
cout << "원하는 항목의 번호를 입력하세요: ";
int inputNum;
cin >> inputNum;
cin.ignore();
if (inputNum == 1) {
borrow_m.displayAllStock();
}
else if (inputNum == 2) {
string title, author;
cout << "제목: ";
getline(cin, title);
cout << "작가: ";
getline(cin, author);
book_m.addBook(title, author);
borrow_m.initializeStock(book_m.getByTitle(title));
}
else if (inputNum == 3) {
string title;
cout << "제목: ";
getline(cin, title);
//get 못하면 display 에러남
borrow_m.displayStockByTitle(title);
}
else if (inputNum == 4) {
string author;
cout << "작가: ";
getline(cin, author);
borrow_m.displayStockByAuthor(author);
}
else if (inputNum == 5) {
string title;
cout << "대여할 책의 제목: ";
getline(cin, title);
borrow_m.borrowBookByTitle(title);
}
else if (inputNum == 6) {
string title;
cout << "반납할 책의 제목: ";
getline(cin, title);
borrow_m.returnBook(title);
}
else if (inputNum == 7) {
cout<<"종료" << endl;
break;
}
else {
cout << "유효하지 않은 번호입니다." << endl;
}
// press any key to continue...
system("pause");
}
return 0;
}
