[내일배움캠프/C++] 도서관 관리 프로그램

김세희·2025년 6월 13일

✍️도서관 관리 프로그램

👀 간단한 도서관 관리 프로그램 구현


필수 기능

  1. 책 제목으로 검색이 가능해야 한다.
  2. 작가 이름으로 검색이 가능해야 한다.
  3. 책 제목이 동일한 경우는 없다.

도전 기능

  1. 특정 책의 대여 여부를 알 수 있어야 한다.
  2. 책의 이름으로 대여 여부를 검색하고 대여가 아닌 경우 대여할 수 있어야 한다.
  3. 책의 작가로 대여 여부를 검색하고 대여가 아닌 경우 대여할 수 있어야 한다.
  4. 책을 반납할 수 있어야 한다.
  5. 모든 책의 재고는 3권으로 통일한다.

🎯참고한 클래스 다이어그램

최종 코드

HW04.h

#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

HW04.cpp

#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]++;
	}
	
}

Main.cpp

#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;

}

실행 화면


🤔🤨🧐

  1. 처음 조건을 보고 한 작가는 여러 책을 가질 수 있을텐데 어떻게 대여를 한다는 것인지 고민하다가 간단하게 작가로 검색했을 때 책 목록과 재고만 확인할 수 있게 하고 대여나 반납은 책 제목으로만 가능하게 했다.
  2. 원래 main에서 진행할 프로세스의 입력을 받는 부분은 구현하지 않고 내가 만든 클래스가 제대로 작동하는지 확인하는 코드들만 넣어놨었다. 과제 해설에서 입력을 받는 부분까지 구현하신 것을 보고 따라해보았다.
  3. 다이어그램 없이도 클래스 구조를 빠르게 정리할 수 있도록 연습하고 공부해야겠다.

0개의 댓글