Answer to Question #271703 in C++ for Kelvin Tito

Question #271703

Using Code block, create an executable C++ program for Library management system


1
Expert's answer
2021-11-26T07:11:14-0500



#include <iostream>

#include <vector>



using namespace std;




class book

{

  std::string book_name;

  static unsigned count;

  unsigned id;

public:

  book() {}

  book(std::string n)

  {

    this->book_name = n;

    count++;

    id = count;

  }

  void set_name(std::string n)

  {

    this->book_name = n;

  }

  void show_book()

  {

    std::cout << id << '\t' << book_name << std::endl;

  }

  const std::string& get_name()

  {

    return this->book_name;

  }


};     //class ends here

unsigned book::count = 0;

class student

{

  bool has_book;

  book b;

  std::string name;

public:

  student(std::string name)

  {

    this->name = name;

    has_book = false;

  }

  void take_book(std::string book_name)

  {

    has_book = true;

    b.set_name(book_name);

  }

  void give_book()

  {

    has_book = false;

  }

  const std::string& get_name()

  {

    return this->name;

  }

  void show()

  {

    std::string has = " has ";

    if (has_book)

    {

      std::cout << this->name << has << b.get_name() << std::endl;

    }

    else

    {

      std::cout << this->name << " has no book" << std::endl;

    }

     

  }

};


class library

{

private:

  std::vector<student> students;

  std::vector<book> books = { {"Leaving Time"}, {"First to die"}, {"Three"}, {"Mr.Mercedes"}, {"Silent screem"}};

public:

  void print_books()

  {

    for (std::size_t i = 0; i!= books.size(); ++i)

    {

      books[i].show_book();

    }

  }

  void print_students()

  {

    for (auto& s : students)

    {

      s.show();

    }

  }

  void student_took(std::string book_name, std::string student_name)

  {

    student s(student_name);

    s.take_book(book_name);


    students.push_back(s);

    for (auto& a : books)

    {

      std::cout << a.get_name() << std::endl;

      if (a.get_name() == book_name)

      {

        int a = 1;

      }

    }

    books.erase(std::remove_if(books.begin(), books.end(), [book_name](book& b) {return b.get_name() == book_name; }), books.end());

  }

  void student_give(std::string book_name, std::string student_name)

  {

    for (auto& s : students)

    {

      if (s.get_name() == student_name)

      {

        s.give_book();

      }

    }

    books.push_back({ book_name });

  }

};




int main()

{

  library l;

  l.print_books();

  l.student_took("First to die", "Nazar");

  l.student_took( "Three", "Yuliia");

  l.print_students();

  l.print_books();


  return 0;

}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS