Library Management System

PHOTO EMBED

Wed May 15 2024 17:32:15 GMT+0000 (Coordinated Universal Time)

Saved by @Mohamedshariif #c++

#include <iostream>
#include <istream>
#include <string>
#include <vector>

using namespace std;

class Book {
    int BookID;
    std::string Title;
    std::string Author;
    double price;
    static int numOfBooks;
    
  friend class Library; // Declare Library as a friend class

    public:
    Book(int BookID, std::string Title, std::string Author, double price){
        this->BookID = BookID;
        this->Title = Title;
        this->Author = Author;
        this->price = price;
        numOfBooks ++;
    }
    int getBookID() const {
    return BookID;
}

    
    std::string getTitle() const {
    return Title;
}
   std::string getAuthor() const {
    return Author;
}
   double getPrice() const {
    return price;
}
    //Static Function
   static int getTotalNumOfBooks(){
        return numOfBooks;
    };

};

int Book::numOfBooks = 0;


//Class User
class Users{
    
    public:
    std::string name;
    std::string address;
    std::string email;
    int phoneNum;
    
    Users(std::string n, std::string addre,std::string em, int pN){
        name = n;
        address = addre;
        email = em;
        phoneNum = pN;
    }
    
    //Virtual Function & Overiding function
    virtual void display(){
    }
    
};
//Derived Class from Base class User
class Student: public Users{
    
    int studentID;
    public:
    Student(int studentID, std::string name, std::string address, std::string email, int phoneNum):Users(name, address, email, phoneNum){
        this->studentID = studentID;
    }
    
    int getStudentID() const {
    return studentID;
}
    
    //Function Overloading, Same Name but different arguments.
    void print(std::string name){
        cout<<"student Name: "<<name<<endl;
    }
    void print(std::string name, int studentID){
        cout<<"Student Name: "<<name<<endl;
        cout<<"Student ID: "<<studentID<<endl;
    }
    //Default arguments
    void print(std::string name, std::string email, int studentID = 1111){
        cout<<"Student Name: "<<name<<endl;
        cout<<"Student Email: "<<email<<endl;
        cout<<"Student ID: "<<studentID<<endl;
    }
    
    void display(){
        cout<<"\n__Student Info:_ "<<endl;
        cout<<"ID: "<<studentID<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Address: "<<address<<endl;
        cout<<"Email: "<<email<<endl;
        cout<<"Phone Number: "<<phoneNum<<endl;
    }
    
    //Friend Function
    friend void globalFunction(Student &stud);
};
class Staff: public Users{
    int staffID;
    public:
    Staff(int staffID, std::string name, std::string address, std::string email, int phoneNum):Users(name, address, email, phoneNum){
        this->staffID = staffID;
    }
    int getStaffID(){
        return staffID;
    }
    
    void display(){
        cout<<"\n___Staff Info:_ "<<endl;
        cout<<"ID: "<<staffID<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Address: "<<address<<endl;
        cout<<"Email: "<<email<<endl;
        cout<<"Phone Number: "<<phoneNum<<endl;
    }
    friend void globalFunction(Staff &staf);
};

//Friend Function implementation
void globalFunction(Student &stud, Staff &staf){
    cout<<"\nAccessing Student ID: "<<stud.getStudentID()<<endl;
    cout<<"Accessing Staff ID: "<<staf.getStaffID()<<endl;
}

 class Library{

vector<Book*> books; // Declaration of books vector
vector<Student*> students;
    
    Book *book;
    std::string name;
    std::string address;
    
    public:
    Library(std::string name, std::string address){
        this->name = name;
        this->address = address;
    }
    
    void setBook(Book *book){
        this->book = book;
    }
    Book* getBook(){
        return book;
    }
    std::string getName(){
        return name;
    }
    void setName(std::string name){
        this->name = name;
    }
    std::string getAddress(){
        return address;
    }
    void setAddress(std::string address){
        this->address = address;
    }
    void addBook(const std::string& title, const std::string& author, double price) {
        int id = books.size() + 1; // Generate ID automatically based on the number of books
        books.push_back(new Book(id, title, author, price));
        cout << "Book added successfully with ID: " << id << endl;
    }
    bool deleteBook(int id) {
        auto it = find_if(books.begin(), books.end(), [id](const Book* book) {
            return book->getBookID() == id;
        });
        if (it != books.end()) {
            books.erase(it);
            return true;
        }
        return false;
    }
    void editBook(int id, std::string newTitle, std::string newAuthor, double newPrice) {
        auto it = find_if(books.begin(), books.end(), [id](const Book* book) {
            return book->getBookID() == id;
        });
        if (it != books.end()) {
            (*it)->Title = newTitle;
            (*it)->Author = newAuthor;
            (*it)->price = newPrice;
        }
    }
    void showBookInfo(int id) {
        auto it = std::find_if(books.begin(), books.end(), [id](const Book* book) {
            return book->getBookID() == id;
        });
        if (it != books.end()) {
            std::cout << "Book ID: " << (*it)->getBookID() << std::endl;
            std::cout << "Book Title: " << (*it)->getTitle() << std::endl;
            std::cout << "Book Author: " << (*it)->getAuthor() << std::endl;
            std::cout << "Book Price: " << (*it)->getPrice() << std::endl;
        }
    }

    std::vector<Book*>& getBooks() { // Return a reference to the vector of books
        return books;
    }


    void addStudent(const std::string& name, const std::string& address, const std::string& email, int phoneNum) {
        int id = students.size() + 1; // Generate ID automatically based on the number of students
        students.push_back(new Student(id, name, address, email, phoneNum));
        cout << "Student added successfully with ID: " << id << endl;
    }

    bool deleteStudent(int id) {
        auto it = find_if(students.begin(), students.end(), [id](const Student* student) {
            return student->getStudentID() == id;
        });
        if (it != students.end()) {
            students.erase(it);
            return true;
        }
        return false;
    }

    void editStudent(int id, const string& newName, const string& newAddress, const string& newEmail, int newPhoneNum) {
        auto it = find_if(students.begin(), students.end(), [id](const Student* student) {
            return student->getStudentID() == id;
        });
        if (it != students.end()) {
            (*it)->name = newName;
            (*it)->address = newAddress;
            (*it)->email = newEmail;
            (*it)->phoneNum = newPhoneNum;
        }
    }

    void showStudentInfo(int id) {
    auto it = find_if(students.begin(), students.end(), [id](const Student* student) {
        return student->getStudentID() == id;
    });
    if (it != students.end()) {
        cout << "Student ID: " << (*it)->getStudentID() << endl;
        cout << "Student Name: " << (*it)->name << endl;
        cout << "Student Address: " << (*it)->address << endl;
        cout << "Student Email: " << (*it)->email << endl;
        cout << "Student Phone Number: " << (*it)->phoneNum << endl;
    }
}
};





int main() {
    Library library("University Library", "Uskudar");

    while (true) {
        cout << "\nMenu:" << endl;
        cout << "1. Manage Books" << endl;
        cout << "2. Manage Students" << endl;
        cout << "3. Exit" << endl;
        cout << "Enter your choice: ";

        int choice;
        cin >> choice;

        switch (choice) {
        case 1: {
            cout << "\nBooks Menu:" << endl;
            cout << "1. Add Book" << endl;
            cout << "2. Delete Book" << endl;
            cout << "3. Edit Book" << endl;
            cout << "4. Show Book Information" << endl;
            cout << "5. Book List" << endl;
            cout << "Enter your choice: ";

            int bookChoice;
            cin >> bookChoice;

            switch (bookChoice) {
            case 1: {
              std::string title, author;
              double price;
              cout << "Enter book title: ";
                 cin.ignore();
                 getline(cin, title);
                 cout << "Enter book author: ";
                     getline(cin, author);
                   cout << "Enter book price: ";
                   cin >> price;
                   library.addBook(title, author, price);

                
                break;
            }
            case 2: {
                int id;
                cout << "Enter ID of the book to delete: ";
                cin >> id;
                if (library.deleteBook(id)) {
                    cout << "Book with ID " << id << " deleted successfully." << endl;
                }
                else {
                    cout << "Book with ID " << id << " not found." << endl;
                }
                break;
            }
            case 3: {
                int id;
                string newTitle, newAuthor;
                double newPrice;
                cout << "Enter ID of the book to edit: ";
                cin >> id;
                cout << "Enter new title: ";
                cin.ignore();
                getline(cin, newTitle);
                cout << "Enter new author: ";
                getline(cin, newAuthor);
                cout << "Enter new price: ";
                cin >> newPrice;
                library.editBook(id, newTitle, newAuthor, newPrice);
                cout << "Book edited successfully." << endl;
                break;
            }
            case 4: {
                int id;
                cout << "Enter ID of the book to show information: ";
                cin >> id;
                library.showBookInfo(id);
                break;
            }
           case 5: {
            vector<Book*> books = library.getBooks();
             cout << "\nBook List:" << endl;
            for (Book* book : books) {
    cout << "Book ID: " << book->getBookID() << ", Title: " << book->getTitle() << ", Author: " << book->getAuthor() << ", Price: " << book->getPrice() << endl;
}
                 break;
               }

            default:
                cout << "Invalid choice." << endl;
            }
            break;
        }
            case 2: {
                cout << "\nStudents Menu:" << endl;
                cout << "1. Add Student" << endl;
                cout << "2. Remove Student" << endl;
                cout << "3. Edit Student" << endl;
                cout << "4. Get Student Info" << endl;
                cout << "5. Exit" << endl;
                cout << "Enter your choice: ";

                int studentChoice;
                cin >> studentChoice;

                switch (studentChoice) {
                    case 1: {
                         std::string name, address, email;
                        int phoneNum;
                        cout << "Enter student name: ";
                        cin.ignore();
                        getline(cin, name);
                        cout << "Enter student address: ";
                        getline(cin, address);
                        cout << "Enter student email: ";
                        cin >> email;
                         cout << "Enter student phone number: ";
                        cin >> phoneNum;
                        library.addStudent(name, address, email, phoneNum);
                        
                        break;
                    }
                    case 2: {
                        int id;
                        cout << "Enter ID of the student to remove: ";
                        cin >> id;
                        if (library.deleteStudent(id)) {
                            cout << "Student with ID " << id << " removed successfully." << endl;
                        } else {
                            cout << "Student with ID " << id << " not found." << endl;
                        }
                        break;
                    }
                    case 3: {
                        int id;
                        string newName, newAddress, newEmail;
                        int newPhoneNum;
                        cout << "Enter ID of the student to edit: ";
                        cin >> id;
                        cout << "Enter new name: ";
                        cin.ignore();
                        getline(cin, newName);
                        cout << "Enter new address: ";
                        getline(cin, newAddress);
                        cout << "Enter new email: ";
                        cin >> newEmail;
                        cout << "Enter new phone number: ";
                        cin >> newPhoneNum;
                        library.editStudent(id, newName, newAddress, newEmail, newPhoneNum);
                        cout << "Student edited successfully." << endl;
                        break;
                    }
                    case 4: {
                        int id;
                        cout << "Enter ID of the student to get information: ";
                        cin >> id;
                        library.showStudentInfo(id);
                        break;
                    }
                    case 5: {
                        cout << "Exiting students menu..." << endl;
                        break;
                    }
                    default:
                        cout << "Invalid choice." << endl;
                }
                break;
            }
        case 3:
            cout << "Exiting..." << endl;
            return 0;
        default:
            cout << "Invalid choice. Please enter a number from 1 to 3." << endl;
        }
    }

    return 0;
}
//OUTPUT:


Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 1

Books Menu:
1. Add Book
2. Delete Book
3. Edit Book
4. Show Book Information
5. Book List
Enter your choice: 1
Enter book title: The Power
Enter book author: James
Enter book price: 23
Book added successfully with ID: 1

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 1

Books Menu:
1. Add Book
2. Delete Book
3. Edit Book
4. Show Book Information
5. Book List
Enter your choice: 1
Enter book title: Psychology
Enter book author: Mike
Enter book price: 21
Book added successfully with ID: 2

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 1

Books Menu:
1. Add Book
2. Delete Book
3. Edit Book
4. Show Book Information
5. Book List
Enter your choice: 5

Book List:
Book ID: 1, Title: The Power, Author: James, Price: 23
Book ID: 2, Title: Physcology, Author: Mike, Price: 21

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 1

Books Menu:
1. Add Book
2. Delete Book
3. Edit Book
4. Show Book Information
5. Book List
Enter your choice: 2
Enter ID of the book to delete: 2
Book with ID 2 deleted successfully.

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 1

Books Menu:
1. Add Book
2. Delete Book
3. Edit Book
4. Show Book Information
5. Book List
Enter your choice: 5

Book List:
Book ID: 1, Title: The Power, Author: James, Price: 23

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 2

Students Menu:
1. Add Student
2. Remove Student
3. Edit Student
4. Get Student Info
5. Exit
Enter your choice: 1
Enter student name: Mohamed
Enter student address: Istanbul
Enter student email: moha@gmail.com
Enter student phone number: 5544
Student added successfully with ID: 1

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 2

Students Menu:
1. Add Student
2. Remove Student
3. Edit Student
4. Get Student Info
5. Exit
Enter your choice: 1
Enter student name: Nasir
Enter student address: Istanbul
Enter student email: nasir@gmail.com
Enter student phone number: 1122
Student added successfully with ID: 2

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 2

Students Menu:
1. Add Student
2. Remove Student
3. Edit Student
4. Get Student Info
5. Exit
Enter your choice: 2
Enter ID of the student to remove: 2
Student with ID 2 removed successfully.

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 2

Students Menu:
1. Add Student
2. Remove Student
3. Edit Student
4. Get Student Info
5. Exit
Enter your choice: 4
Enter ID of the student to get information: 2

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 2

Students Menu:
1. Add Student
2. Remove Student
3. Edit Student
4. Get Student Info
5. Exit
Enter your choice: 4
Enter ID of the student to get information: 1
Student ID: 1
Student Name: Mohamed
Student Address: Istanbul
Student Email: moha@gmail.com
Student Phone Number: 5544

Menu:
1. Manage Books
2. Manage Students
3. Exit
Enter your choice: 3
Exiting...
 
Normal program termination. Exit status: 0
content_copyCOPY