Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <vector>
- #include <sstream>
- using namespace std;
- void SearchByID(int y);
- void DeleteBook(int id);
- void SearchByName(char y[]);
- void UpdateBook(int id, int ch);
- struct Book
- {
- char name[50];
- int ID;
- char author[50];
- double price;
- int copies;
- };
- struct Library
- {
- string idd, name, author, price, copies;
- vector<Book> books;
- };
- string toString(Book & b)
- {
- ostringstream OLib;
- OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
- return OLib.str();
- }
- string toString(Library & l)
- {
- ostringstream OLib;
- OLib << left << setw(4) << l.idd << setw(15) << l.name << setw(15) << l.author << setw(8) << l.price << setw(8) << l.copies << endl;
- OLib << "=================================================" << endl;
- for (Book & b : l.books) {
- OLib << toString(b);
- OLib << "=================================================" << endl;
- }
- return OLib.str();
- }
- void WriteFile(Library & library, char x[50])
- {
- ofstream OLib(x);
- OLib << left << setw(4) << library.idd << setw(15) << library.name << setw(15) << library.author << setw(8) << library.price << setw(8) << library.copies << endl;
- for (Book &b : library.books)
- {
- OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
- }
- }
- Library ReadFile(char x[50])
- {
- ifstream iLib(x, ios::in);
- Library library;
- Book b;
- iLib >> library.idd >> library.name >> library.author >> library.price >> library.copies;
- while (iLib >> b.ID >> b.name >> b.author >> b.price >> b.copies) {
- library.books.push_back(b);
- }
- return library;
- }
- void BorrowBook(int id)
- {
- Library library = ReadFile("library.txt");
- for (Book & b : library.books)
- {
- if (b.ID == id)
- b.copies--;
- }
- WriteFile(library,"library.txt");
- }
- int main()
- {
- ofstream OLib("library.txt", ios::app);
- int choice, id;
- Book book;
- cout << setw(310) << "Library system by MiDo\n" << endl;
- cout << "Choose one of the following:- " << endl;
- cout << "1. Add a new book.\n2. Update a book.\n3. Delete a book.\n4. Show all books.\n5. Search book by ID.\n6. Search book by Name.\n7. Borrow a book.\n8. Exit." << endl;
- cin >> choice;
- while (choice >= 0 && choice < 8) {
- if (choice == 1) {
- cout << "Enter the name of the book you want to add: " << endl;
- getchar();
- cin.get(book.name, 50);
- cout << "Enter the ID of the book you want to add: " << endl;
- cin >> book.ID;
- cout << "Enter the author of the book you want to add: " << endl;
- getchar();
- cin.get(book.author, 50);
- cout << "Enter the price of the book you want to add: " << endl;
- cin >> book.price;
- cout << "Enter the copies of the book you want to add: " << endl;
- cin >> book.copies;
- OLib << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
- cout << "Book successfully added!" << endl;
- choice = 0;
- }
- else if (choice == 2) {
- int ch;
- cout << "Enter the ID of the book you want to update." << endl;
- cin >> id;
- cout << "Enter what you want to update :-\n1. Name.\n2. Author.\n3. Price.\n4. No. of copies." << endl;
- cin >> ch;
- UpdateBook(id, ch);
- }
- else if (choice == 3) {
- cout << "Enter the ID of the book you want to delete." << endl;
- cin >> id;
- DeleteBook(id); // Delete a book
- cout << "Book has been deleted! \n" << endl;
- }
- else if (choice == 4) { // show all books
- Library l = ReadFile("library.txt");
- cout << toString(l) << endl;
- }
- else if (choice == 5) {
- int x;
- cout << "Enter the ID you want to search for" << endl;
- cin >> x;
- SearchByID(x);
- choice = 0;
- }
- else if (choice == 6) {
- char x[50];
- cout << "Enter the book name you want to search for" << endl;
- cin >> x;
- SearchByName(x);
- choice = 0;
- }
- else if (choice == 7) {
- int x;
- cout << "Enter the ID of the book you want to borrow" << endl;
- cin >> x;
- BorrowBook(x);
- cout << "Book has been borrowed, Get it back when you finish.\n" << endl;
- }
- else if (choice == 8) {
- return 0;
- }
- cout << "1. Add a new book.\n2. Update a book.\n3. Delete a book.\n4. Show all books.\n5. Search book by ID.\n6. Search book by Name.\n7. Borrow a book.\n8. exit." << endl;
- cin >> choice;
- }
- cout << "Program ended!" << endl;
- system("pause");
- return 0;
- }
- void SearchByID(int y)
- {
- bool found = false;
- Library library = ReadFile("library.txt");
- for (Book &book : library.books) {
- if (book.ID == y) {
- cout << "\nBook is found:\n" << endl;
- cout << left << setw(4) << library.idd << setw(15) << library.name << setw(15) << library.author << setw(8) << library.price << setw(8) << library.copies << endl;
- cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
- cout << "================================================\n" << endl;
- found = true;
- break;
- }
- }
- if (found == false)
- cout << "Book is not found" << endl;
- }
- void SearchByName(char y[])
- {
- bool found = false;
- Library lib = ReadFile("library.txt");
- for (Book &book : lib.books) {
- if (strcmp(y, book.name) == 0) {
- cout << "\nBook is found:\n" << endl;
- cout << left << setw(4) << lib.idd << setw(15) << lib.name << setw(15) << lib.author << setw(8) << lib.price << setw(8) << lib.copies << endl;
- cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
- cout << "================================================\n" << endl;
- found = true;
- break;
- }
- }
- if (found == false)
- cout << "Book is not found" << endl;
- }
- void DeleteBook(int id)
- {
- Library library = ReadFile("library.txt");
- for (auto i = library.books.begin(); i != library.books.end(); i++)
- {
- if (i->ID != id)
- continue;
- library.books.erase(i);
- break;
- }
- WriteFile(library, "library.txt");
- }
- void UpdateBook(int id, int ch)
- {
- bool found = false;
- Library library = ReadFile("library.txt");
- ofstream OLib("LibraryTemp.txt");
- char update[50]; int price, copies;
- OLib << left << setw(4) << library.idd << setw(15) << library.name << setw(15) << library.author << setw(8) << library.price << setw(8) << library.copies << endl;
- for (Book &b : library.books)
- {
- if (b.ID == id) {
- found = true;
- if (ch == 1) {
- cout << "Enter the new name you want to update" << endl;
- cin >> update;
- OLib << left << setw(4) << b.ID << setw(15) << update << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
- cout << "Book has been updated! \n" << endl;
- }
- else if (ch == 2) {
- cout << "Enter the new author you want to update" << endl;
- cin >> update;
- OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << update << setw(8) << b.price << setw(8) << b.copies << endl;
- cout << "Book has been updated! \n" << endl;
- }
- else if (ch == 3) {
- cout << "Enter the new price you want to edit" << endl;
- cin >> price;
- OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << price << setw(8) << b.copies << endl;
- cout << "Book has been updated!\n" << endl;
- }
- else if (ch == 4) {
- cout << "Enter the new copies you want to update" << endl;
- cin >> copies;
- OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << copies << endl;
- cout << "Book has been updated! \n" << endl;
- }
- }
- else
- OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
- }
- if (found == false)
- cout << "File is not found" << endl;
- library = ReadFile("LibraryTemp.txt");
- WriteFile(library, "library.txt");
- }
Advertisement
Add Comment
Please, Sign In to add comment