midoking

Final working Library system

Aug 14th, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <vector>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9. void SearchByID(int y);
  10. void DeleteBook(int id);
  11. void SearchByName(char y[]);
  12. void UpdateBook(int id, int ch);
  13.  
  14.  
  15. struct Book
  16. {
  17.     char name[50];
  18.     int ID;
  19.     char author[50];
  20.     double price;
  21.     int copies;
  22. };
  23.  
  24. struct Library
  25. {
  26.     string idd, name, author, price, copies;
  27.     vector<Book> books;
  28. };
  29.  
  30. string toString(Book & b)
  31. {
  32.     ostringstream OLib;
  33.     OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
  34.     return OLib.str();
  35. }
  36.  
  37. string toString(Library & l)
  38. {
  39.     ostringstream OLib;
  40.     OLib << left << setw(4) << l.idd << setw(15) << l.name << setw(15) << l.author << setw(8) << l.price << setw(8) << l.copies << endl;
  41.     OLib << "=================================================" << endl;
  42.     for (Book & b : l.books) {
  43.         OLib << toString(b);
  44.         OLib << "=================================================" << endl;
  45.     }
  46.     return OLib.str();
  47. }
  48.  
  49. void WriteFile(Library & library, char x[50])
  50. {
  51.     ofstream OLib(x);
  52.     OLib << left << setw(4) << library.idd << setw(15) << library.name << setw(15) << library.author << setw(8) << library.price << setw(8) << library.copies << endl;
  53.     for (Book &b : library.books)
  54.     {
  55.         OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
  56.     }
  57. }
  58.  
  59. Library ReadFile(char x[50])
  60. {
  61.     ifstream iLib(x, ios::in);
  62.     Library library;
  63.     Book b;
  64.     iLib >> library.idd >> library.name >> library.author >> library.price >> library.copies;
  65.     while (iLib >> b.ID >> b.name >> b.author >> b.price >> b.copies) {
  66.         library.books.push_back(b);
  67.     }
  68.     return library;
  69. }
  70.  
  71. void BorrowBook(int id)
  72. {
  73.     Library library = ReadFile("library.txt");
  74.     for (Book & b : library.books)
  75.     {
  76.         if (b.ID == id)
  77.             b.copies--;
  78.     }
  79.     WriteFile(library,"library.txt");
  80. }
  81.  
  82. int main()
  83. {
  84.     ofstream OLib("library.txt", ios::app);
  85.     int choice, id;
  86.     Book book;
  87.     cout << setw(310) << "Library system by MiDo\n" << endl;
  88.     cout << "Choose one of the following:- " << endl;
  89.     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;
  90.     cin >> choice;
  91.     while (choice >= 0 && choice < 8) {
  92.         if (choice == 1) {
  93.             cout << "Enter the name of the book you want to add: " << endl;
  94.             getchar();
  95.             cin.get(book.name, 50);
  96.             cout << "Enter the ID of the book you want to add: " << endl;
  97.             cin >> book.ID;
  98.             cout << "Enter the author of the book you want to add: " << endl;
  99.             getchar();
  100.             cin.get(book.author, 50);
  101.             cout << "Enter the price of the book you want to add: " << endl;
  102.             cin >> book.price;
  103.             cout << "Enter the copies of the book you want to add: " << endl;
  104.             cin >> book.copies;
  105.             OLib << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
  106.             cout << "Book successfully added!" << endl;
  107.             choice = 0;
  108.         }
  109.         else if (choice == 2) {
  110.             int ch;
  111.             cout << "Enter the ID of the book you want to update." << endl;
  112.             cin >> id;
  113.             cout << "Enter what you want to update :-\n1. Name.\n2. Author.\n3. Price.\n4. No. of copies." << endl;
  114.             cin >> ch;
  115.             UpdateBook(id, ch);
  116.         }
  117.         else if (choice == 3) {
  118.             cout << "Enter the ID of the book you want to delete." << endl;
  119.             cin >> id;
  120.             DeleteBook(id); // Delete a book
  121.             cout << "Book has been deleted! \n" << endl;
  122.         }
  123.         else if (choice == 4) { // show all books
  124.             Library l = ReadFile("library.txt");
  125.             cout << toString(l) << endl;
  126.         }
  127.         else if (choice == 5) {
  128.             int x;
  129.             cout << "Enter the ID you want to search for" << endl;
  130.             cin >> x;
  131.             SearchByID(x);
  132.             choice = 0;
  133.         }
  134.         else if (choice == 6) {
  135.             char x[50];
  136.             cout << "Enter the book name you want to search for" << endl;
  137.             cin >> x;
  138.             SearchByName(x);
  139.             choice = 0;
  140.         }
  141.         else if (choice == 7) {
  142.             int x;
  143.             cout << "Enter the ID of the book you want to borrow" << endl;
  144.             cin >> x;
  145.             BorrowBook(x);
  146.             cout << "Book has been borrowed, Get it back when you finish.\n" << endl;
  147.         }
  148.         else if (choice == 8) {
  149.             return 0;
  150.         }
  151.         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;
  152.         cin >> choice;
  153.     }
  154.     cout << "Program ended!" << endl;
  155.     system("pause");
  156.     return 0;
  157. }
  158.  
  159. void SearchByID(int y)
  160. {
  161.     bool found = false;
  162.     Library library = ReadFile("library.txt");
  163.     for (Book &book : library.books) {
  164.         if (book.ID == y) {
  165.             cout << "\nBook is found:\n" << endl;
  166.             cout << left << setw(4) << library.idd << setw(15) << library.name << setw(15) << library.author << setw(8) << library.price << setw(8) << library.copies << endl;
  167.             cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
  168.             cout << "================================================\n" << endl;
  169.             found = true;
  170.             break;
  171.         }
  172.     }
  173.     if (found == false)
  174.         cout << "Book is not found" << endl;
  175. }
  176.  
  177. void SearchByName(char y[])
  178. {
  179.     bool found = false;
  180.     Library lib = ReadFile("library.txt");
  181.     for (Book &book : lib.books) {
  182.         if (strcmp(y, book.name) == 0) {
  183.             cout << "\nBook is found:\n" << endl;
  184.             cout << left << setw(4) << lib.idd << setw(15) << lib.name << setw(15) << lib.author << setw(8) << lib.price << setw(8) << lib.copies << endl;
  185.             cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
  186.             cout << "================================================\n" << endl;
  187.             found = true;
  188.             break;
  189.         }
  190.     }
  191.     if (found == false)
  192.         cout << "Book is not found" << endl;
  193. }
  194.  
  195. void DeleteBook(int id)
  196. {
  197.     Library library = ReadFile("library.txt");
  198.     for (auto i = library.books.begin(); i != library.books.end(); i++)
  199.     {
  200.         if (i->ID != id)
  201.             continue;
  202.         library.books.erase(i);
  203.         break;
  204.     }
  205.     WriteFile(library, "library.txt");
  206. }
  207.  
  208. void UpdateBook(int id, int ch)
  209. {
  210.     bool found = false;
  211.     Library library = ReadFile("library.txt");
  212.     ofstream OLib("LibraryTemp.txt");
  213.     char update[50]; int price, copies;
  214.     OLib << left << setw(4) << library.idd << setw(15) << library.name << setw(15) << library.author << setw(8) << library.price << setw(8) << library.copies << endl;
  215.     for (Book &b : library.books)
  216.     {
  217.         if (b.ID == id) {
  218.             found = true;
  219.             if (ch == 1) {
  220.                 cout << "Enter the new name you want to update" << endl;
  221.                 cin >> update;
  222.                 OLib << left << setw(4) << b.ID << setw(15) << update << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
  223.                 cout << "Book has been updated! \n" << endl;
  224.             }
  225.             else if (ch == 2) {
  226.                 cout << "Enter the new author you want to update" << endl;
  227.                 cin >> update;
  228.                 OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << update << setw(8) << b.price << setw(8) << b.copies << endl;
  229.                 cout << "Book has been updated! \n" << endl;
  230.             }
  231.             else if (ch == 3) {
  232.                 cout << "Enter the new price you want to edit" << endl;
  233.                 cin >> price;
  234.                 OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << price << setw(8) << b.copies << endl;
  235.                 cout << "Book has been updated!\n" << endl;
  236.             }
  237.             else if (ch == 4) {
  238.                 cout << "Enter the new copies you want to update" << endl;
  239.                 cin >> copies;
  240.                 OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << copies << endl;
  241.                 cout << "Book has been updated! \n" << endl;
  242.             }
  243.  
  244.         }
  245.         else
  246.             OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
  247.     }
  248.     if (found == false)
  249.         cout << "File is not found" << endl;
  250.     library = ReadFile("LibraryTemp.txt");
  251.     WriteFile(library, "library.txt");
  252. }
Advertisement
Add Comment
Please, Sign In to add comment