midoking

Library system

Aug 12th, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. struct Book
  8. {
  9.     char name[50];
  10.     int ID;
  11.     char author[50];
  12.     double price;
  13.     int copies;
  14. };
  15.  
  16. ofstream OLib("Library.txt", ios::app);
  17. ifstream iLib("Library.txt", ios::in);
  18.  
  19. void SearchByID(int y);
  20. void SearchByName(char y[]);
  21.  
  22. int main()
  23. {
  24.     string search, line;
  25.     int choice;
  26.     Book book;
  27.     cout << setw(310) << "Library system by MiDo\n" << endl;
  28.     cout << "Choose one of the following:- " << endl;
  29.     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;
  30.     cin >> choice;
  31.     while (choice >= 0 && choice <8) {
  32.         if (choice == 1) {
  33.             cout << "Enter the name of the book you want to add: " << endl;
  34.             getchar();
  35.             cin.get(book.name, 50);
  36.             cout << "Enter the ID of the book you want to add: " << endl;
  37.             cin >> book.ID;
  38.             cout << "Enter the author of the book you want to add: " << endl;
  39.             getchar();
  40.             cin.get(book.author, 50);
  41.             cout << "Enter the price of the book you want to add: " << endl;
  42.             cin >> book.price;
  43.             cout << "Enter the copies of the book you want to add: " << endl;
  44.             cin >> book.copies;
  45.             OLib << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
  46.             cout << "Book successfully added!" << endl;
  47.             choice = 0;
  48.         }
  49.         else if (choice == 2) {
  50.             cout << "Enter the ID of the book you want to update." << endl;
  51.             cin >> search;
  52.             while (getline(iLib, line)) {
  53.                 if (line.find(search)) {
  54.                     line.replace(line.find(search), search.length(), "");
  55.                     cout << "Enter the new ID, name, author, price, copies" << endl;
  56.                     cin >> book.ID >> book.name >> book.author >> book.price >> book.copies;
  57.                     OLib << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
  58.                     cout << "Book successfully edited!" << endl;
  59.                 }
  60.             }
  61.         }
  62.         else if (choice == 3) {
  63.         }
  64.         else if (choice == 4) {
  65.             string id, name, author, price, copies;
  66.             iLib >> id >> name >> author >> price >> copies;
  67.             cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
  68.             while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
  69.                 cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
  70.                 cout << "================================================" << endl;
  71.             }
  72.             choice = 0;
  73.         }
  74.         else if (choice == 5) {
  75.             int x;
  76.             cout << "Enter the ID you want to search for" << endl;
  77.             cin >> x;
  78.             SearchByID(x);
  79.             choice = 0;
  80.         }
  81.         else if (choice == 6) {
  82.             char x[50];
  83.             cout << "Enter the book name you want to search for" << endl;
  84.             cin >> x;
  85.             SearchByName(x);
  86.             choice = 0;
  87.         }
  88.         else if (choice == 7) {
  89.             int x;
  90.             cout << "Enter the ID of the book you want to borrow" << endl;
  91.             cin >> x;
  92.  
  93.         }
  94.         else if (choice == 8) {
  95.             return 0;
  96.         }
  97.         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;
  98.         cin >> choice;
  99.     }
  100.     cout << "Program ended!" << endl;
  101.     system("pause");
  102.     return 0;
  103. }
  104.  
  105. void SearchByID(int y)
  106. {
  107.     bool found = false;
  108.     Book book;
  109.     string id, name, author, price, copies;
  110.     iLib >> id >> name >> author >> price >> copies;
  111.     while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
  112.         if (book.ID == y) {
  113.             cout << "Book is found:" << endl;
  114.             cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
  115.             cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
  116.             found = true;
  117.             break;
  118.         }
  119.     }
  120.     if (found == false)
  121.         cout << "Book is not found" << endl;
  122. }
  123.  
  124. void SearchByName(char y[])
  125. {
  126.     bool found = false;
  127.     Book book;
  128.     string id, name, author, price, copies;
  129.     iLib >> id >> name >> author >> price >> copies;
  130.     while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
  131.         if (strcmp(y,book.name) == 0) {
  132.             cout << "Book is found:" << endl;
  133.             cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
  134.             cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
  135.             found = true;
  136.             break;
  137.         }
  138.     }
  139.     if (found == false)
  140.         cout << "Book is not found" << endl;
  141. }
  142.  
  143. void Borrow(int x)
  144. {
  145.     bool found = false;
  146.     Book book;
  147.     int b;
  148.     string id, name, author, price, copies;
  149.     iLib >> id >> name >> author >> price >> copies;
  150.     while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
  151.         if (book.ID == x) {
  152.             b = book.copies;
  153.             cout << "Book is borrowed:" << endl;
  154.             cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
  155.             cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
  156.             found = true;
  157.             break;
  158.         }
  159.     }
  160.     if (found == false)
  161.         cout << "Book is not found" << endl;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment