1. 필수 기능 구현
▸ 문제

▸ 완성 코드
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
class Book {
public:
string title_;
string author_;
Book(const string& title, const string& author)
: title_(title), author_(author) {
}
};
class BookManager {
private:
vector<Book> books_;
public:
void AddBook(const string& title, const string& author) {
books_.push_back(Book(title, author));
cout << "책이 추가되었습니다: " << title << " by " << author << endl;
}
void DisplayAllBooks() const {
if (books_.empty()) {
cout << "현재 등록된 책이 없습니다." << endl;
return;
}
cout << "현재 도서 목록:" << endl;
for (Book book : books_) {
PrintBook(book);
}
}
void PrintBook(Book book) const
{
cout << "- " << book.title_ << " by " << book.author_ << endl;
}
void SerchByTitle(string title) const
{
bool is_exist = false;
for (Book book : books_)
{
if (book.title_ == title)
{
cout << "검색하신 제목과 일치하는 책의 정보입니다.\n";
PrintBook(book);
is_exist = true;
break;
}
}
if (!is_exist)
cout << "찾으시는 책이 없습니다.\n";
}
void SerchByAuthor(string author) const
{
bool is_exist = false;
for (Book book : books_)
{
if (book.author_ == author)
{
PrintBook(book);
is_exist = true;
}
}
if (!is_exist)
cout << "찾으시는 책이 없습니다.\n";
}
};
2. 도전 기능 구현
▸ 문제

▸ 완성 코드
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
class Book {
public:
string title_;
string author_;
Book(const string& title, const string& author)
: title_(title), author_(author) {
}
};
class BookManager {
private:
vector<Book> books_;
public:
void AddBook(const Book book) {
books_.push_back(book);
cout << "책이 추가되었습니다: " << book.title_ << " by " << book.author_ << endl;
}
void DisplayAllBooks() const {
if (books_.empty()) {
cout << "현재 등록된 책이 없습니다." << endl;
return;
}
cout << "현재 도서 목록:" << endl;
for (Book book : books_) {
PrintBook(book);
}
}
void PrintBook(Book book) const
{
cout << "- " << book.title_ << " by " << book.author_ << endl;
}
Book* GetBookByTitle(string title)
{
for (int i=0; i<books_.size(); i++)
{
if (books_[i].title_ == title)
return &books_[i];
}
return nullptr;
}
Book* GetBookByAuthor(string author)
{
for (int i = 0; i < books_.size(); i++)
{
if (books_[i].author_ == author)
return &books_[i];
}
return nullptr;
}
};
class BorrowManager
{
private:
unordered_map<string, int> stock_;
public:
void InitializeStock(Book book, int quantity = 3)
{
stock_.insert({ book.title_, quantity });
}
void BorrowBook(string title)
{
auto iter = stock_.find(title);
if (iter != stock_.end())
{
if (iter->second > 0)
{
cout << "대여를 완료했습니다. 남은 수량: " << --iter->second << '\n';
}
else
{
cout << "남은 수량이 부족해 대여하지 못했습니다.\n";
}
}
else
{
cout << "현재 도서 목록에 없는 책입니다.\n";
}
}
void ReturnBook(string title)
{
auto iter = stock_.find(title);
if (iter != stock_.end())
{
iter->second++;
cout << "도서 반납을 완료하였습니다.\n";
}
else
{
cout << "현재 도서 목록에 없는 책입니다.\n";
}
}
void SearchStock(string title) const
{
auto iter = stock_.find(title);
if (iter != stock_.end())
{
cout << "==> 남은 수량: " << iter->second << '\n';
}
else
{
cout << "현재 도서 목록에 없는 책입니다.\n";
}
}
};
class Librarian
{
private:
BookManager bookM_;
BorrowManager borrowM_;
public:
void AddBook(const string& title, const string& author)
{
Book book(title, author);
bookM_.AddBook(book);
borrowM_.InitializeStock(book);
}
void PrintBookList() const
{
bookM_.DisplayAllBooks();
}
Book* SearchBook(int type, string str)
{
Book* book = nullptr;
if (type == 1)
{
book = bookM_.GetBookByTitle(str);
}
else if (type == 2)
{
book = bookM_.GetBookByAuthor(str);
}
if (book != nullptr)
{
bookM_.PrintBook(*book);
borrowM_.SearchStock(book->title_);
}
else
{
cout << "찾으시는 책이 없습니다.\n";
}
return book;
}
void RentalBook(int type, string str)
{
Book* book = SearchBook(type, str);
if (book != nullptr)
{
borrowM_.BorrowBook(book->title_);
}
}
void ReturnBook(string title)
{
borrowM_.ReturnBook(title);
}
};