Unreal Track 1기 [25.01.03] - C++ Summary

chooha·2025년 1월 3일

TIL

목록 보기
13/60

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)); // push_back 사용
        cout << "책이 추가되었습니다: " << title << " by " << author << endl;
    }

    // 모든 책 출력 메서드
    void DisplayAllBooks() const {
        if (books_.empty()) {
            cout << "현재 등록된 책이 없습니다." << endl;
            return;
        }

        cout << "현재 도서 목록:" << endl;
        for (Book book : books_) { // 일반적인 for문 사용
            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); // push_back 사용
        cout << "책이 추가되었습니다: " << book.title_ << " by " << book.author_ << endl;
    }

    // 모든 책 출력 메서드
    void DisplayAllBooks() const {
        if (books_.empty()) {
            cout << "현재 등록된 책이 없습니다." << endl;
            return;
        }

        cout << "현재 도서 목록:" << endl;
        for (Book book : books_) { // 일반적인 for문 사용
            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:
    //새로운 책이 들어오면 책을 stock에 저장
    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);
    }
};

0개의 댓글