Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cinttypes>
- class Book {
- private:
- std::string name, author, publisher;
- uint32_t publishYear, pageCount;
- public:
- Book(const std::string name, const std::string author, const std::string publisher, uint32_t publishYear,
- uint32_t pageCount) {
- this->name = name;
- this->author = author;
- this->publisher = publisher;
- this->publishYear = publishYear;
- this->pageCount = pageCount;
- }
- void printBook(bool isList = false) {
- if(isList) std::cout << " - ";
- std::cout << "Name: " << this->name
- << " | Author: " << this->author
- << " | Publisher: " << this->publisher
- << " | Publish Year: " << this->publishYear
- << " | Page Count: " << this->pageCount
- << std::endl;
- }
- uint32_t getYear() { return this->publishYear; }
- };
- class Library {
- private:
- std::string name;
- uint32_t bookCount;
- Book** books;
- public:
- Library(const std::string name) {
- this->name = name;
- this->bookCount = 0;
- this->books = new Book*[bookCount];
- }
- void insertBooks() {
- bool insertMoreBooks = true;
- char userDecision;
- while(insertMoreBooks) {
- std::cout << "Do you want to insert book? (Y/N) ";
- std::cin >> userDecision;
- if(userDecision == 'N' || userDecision == 'n') {
- insertMoreBooks = false;
- break;
- }
- std::string name, author, publisher;
- uint32_t publishYear, pagesCount;
- std::cin.ignore();
- std::cout << "Name: ";
- std::getline(std::cin, name);
- std::cout << "Author: ";
- std::getline(std::cin, author);
- std::cout << "Publisher: ";
- std::getline(std::cin, publisher);
- std::cout << "Publish Year: ";
- std::cin >> publishYear;
- std::cout << "Page Count: ";
- std::cin >> pagesCount;
- Book* insertedBook = this->addBook(name, author, publisher, publishYear, pagesCount);
- std::cout << "Inserted book: ";
- insertedBook->printBook();
- }
- }
- Book* addBook(const std::string name, const std::string author, const std::string publisher, uint32_t publishYear,
- uint32_t pageCount) {
- this->bookCount++;
- Book** oldBooks = this->books;
- this->books = new Book*[bookCount];
- for(int i = 0; i < bookCount - 1; i++) {
- this->books[i] = oldBooks[i];
- }
- this->books[bookCount-1] = new Book(name, author, publisher, publishYear, pageCount);
- return this->books[bookCount-1];
- }
- void printBooks() {
- for(int i = 0; i < bookCount; i++) {
- this->books[i]->printBook(true);
- }
- }
- Book* getOldestBook() {
- Book* oldestBook = this->books[0];
- for(int i = 0; i < bookCount; i++) {
- if(this->books[i]->getYear() < oldestBook->getYear()) oldestBook = this->books[i];
- }
- return oldestBook;
- }
- uint32_t getAvgBookYear() {
- uint32_t yearSum = 0;
- for(int i = 0; i < bookCount; i++) {
- yearSum += this->books[i]->getYear();
- }
- return static_cast<uint32_t>(yearSum / bookCount);
- }
- };
- int main() {
- // Create library
- Library myLib("My Library");
- std::cout << "Librarian, welcome to your new workplace! Be prepared for everything..."
- << std::endl
- << "--------------------------------------------------"
- << std::endl;
- // Add books to library
- //myLib.addBook("Don Quixote", "Miguel de Cervantes", "Ancient Stuff", 1615, 928);
- //myLib.addBook("1984", "George Orwell", "Harvill Secker", 1949, 336);
- std::cout << "Well, I guess we should start somewhere..."
- << std::endl;
- myLib.insertBooks();
- std::cout << "--------------------------------------------------"
- << std::endl;
- // Print all books
- std::cout << std::endl
- << "Phew... After all these keystrokes I guess you want to see your work... Look below :D "
- << std::endl;
- myLib.printBooks();
- std::cout << "--------------------------------------------------"
- << std::endl;
- // Find oldest book
- std::cout << std::endl
- << "But... you must keep an eye on the oldest one because some people would want to steal it."
- << std::endl
- << "The oldest book is: "
- << std::endl;
- Book* oldestBook = myLib.getOldestBook();
- oldestBook->printBook();
- std::cout << "--------------------------------------------------"
- << std::endl;
- // Get avg publish year
- std::cout << std::endl
- << "The legend says that the last Librarian was born at the average book year in the library when the new one step into the library for the first time..."
- << std::endl
- << "The last librarian's birth year was "
- << myLib.getAvgBookYear()
- << std::endl;
- std::cout << "--------------------------------------------------"
- << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement