[text] GROUP DATA STRUCT

Viewer

copydownloadembedprintName: GROUP DATA STRUCT
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int delete_id;
  7.  
  8. // Book Node has 3 attributes: - id, price, and quantity 
  9. class Book
  10. {
  11. public:
  12.     int id;
  13.     float price;
  14.     int quantity;
  15. };
  16.  
  17. // Predicate implemented as a function to help in deleting the book with given id 
  18. bool helper (const Book& b) { return (b.id) == delete_id;
  19. }
  20.  
  21. int main()
  22. {
  23.  
  24.     list<Book> ls; // STL list to store book info   
  25.     // we have to keep asking for all the five options from the user until the user type 'N'
  26.  
  27.     while (1) {
  28.         // printing menu          
  29.         cout << "-----------------------" << endl;
  30.         cout << "    ::   MENU   ::    " << endl;
  31.         cout << "1. Add new record" << endl;
  32.         cout << "2. Search record" << endl;
  33.         cout << "3. Display record" << endl;
  34.         cout << "4. Update record" << endl;
  35.         cout << "5. Delete record" << endl;
  36.         cout << "-----------------------" << endl;
  37.         cout << "Enter selection : ";
  38.         int option;
  39.         cin >> option; // option (1-5) to be selected  
  40.  
  41.         if (option == 1) {
  42.             // if user selects option 1             
  43.             // he/she wants to add new book             
  44.             Book b1;
  45.             cout << "Enter ID       : ";
  46.             cin >> b1.id;
  47.             cout << "Enter Price    : ";
  48.             cin >> b1.price;
  49.             cout << "Enter Quantity : ";
  50.             cin >> b1.quantity;
  51.  
  52.             ls.push_back(b1); // push this book at the end of list         
  53.         }
  54.         else if (option == 2) {
  55.             // if the user selects option 2             
  56.             // he/she wants to search a book from the record 
  57.  
  58.             int temp_id;
  59.             cout << "Enter ID        : ";
  60.             cin >> temp_id; // enter id of the book you want to search 
  61.  
  62.             // searching for the book with its id             
  63.             list<Book>::iterator it = find_if(ls.begin(), 
  64.                             ls.end(),
  65.                             [&](const Book b) { return temp_id == (b.id); });
  66.             cout << "::Result::" << endl;
  67.             if (it == ls.end()) {
  68.                 // entered id not exist in the record                 
  69.                 cout << "Error! Book ID no " << temp_id << " is not exist." << endl;
  70.             }
  71.             else {
  72.                 // a book find with the given id                 
  73.                 cout << "Book ID         : " << (*it).id << endl;
  74.                 cout << "Book Price      : " << (*it).price << endl;                     
  75.                 cout << "Quantity Sold   : " << (*it).quantity << endl;
  76.             }
  77.         }
  78.         else if (option == 3) {
  79.             // if the user selects option 3             
  80.             // Display record             
  81.             cout << endl;
  82.             cout << "-----------------------" << endl;
  83.             cout << "      :: RECORD ::    " << endl;
  84.             cout << "-----------------------" << endl;
  85.             cout << endl;
  86.  
  87.             int i = 1; // counter of books                      
  88.  
  89.             // iterating through the list              
  90.             // it is the iterator of the list             
  91.             for (list<Book>::iterator it = ls.begin(); it != ls.end(); it++) {
  92.                 cout << "Record " << i << ": " << endl;
  93.                 cout << "Book ID No      : " << (*it).id << endl;
  94.                 cout << "Book price      : " << (*it).price << endl;
  95.                 cout << "Quantity Sold   : " << (*it).quantity << endl;                
  96.                 cout << endl;
  97.                 i++;
  98.             }
  99.             cout << "End of record" << endl;
  100.             cout << "No of record in memory : " << ls.size() << endl;
  101.         }
  102.         else if (option == 4) {
  103.             // if user selects option 4             
  104.             // Update record             
  105.             cout << "Enter ID        : ";
  106.             int temp_id;
  107.             cin >> temp_id;
  108.  
  109.             cout << endl;
  110.             cout << "::Result:: " << endl;
  111.             cout << "-----------------------" << endl;
  112.             cout << "Current record: " << endl;
  113.  
  114.             // checking if any book exist with the given id  
  115.             list<Book>::iterator it = find_if(begin(ls), 
  116.                             end(ls),
  117.                             [&](const Book b) { return temp_id == (b.id); });
  118.  
  119.             if (it == ls.end()) {
  120.                 // book not found with the entered id                 
  121.                 // invalid id                 
  122.                 cout << "Error! Book ID no " << temp_id << " is not exist." << endl;
  123.             }
  124.             else {
  125.                 // book with temp_id found                 
  126.                 cout << "Book ID          : " << (*it).id << endl;
  127.                 cout << "1. Book Price    : " << (*it).price << endl;
  128.                 cout << "2. Quantity Sold : " << (*it).quantity << endl;
  129.  
  130.                 cout << "Enter choice to update: ";
  131.                 int choice;
  132.                 cin >> choice;
  133.                 cout << "-----------------------" << endl;
  134.                 cout << endl;
  135.  
  136.                 if (choice == 1) {
  137.                     // update the price of the selected book                     
  138.                     cout << "Enter new value of price: ";
  139.                     float temp_price;
  140.                     cin >> temp_price;
  141.                     (*it).price = temp_price;
  142.                 }
  143.  
  144.                 else if (choice == 2) {
  145.                     // update the quantity of the selected book                     
  146.                     cout << "Enter new value of quantity: ";                     
  147.                     int temp_quantity;
  148.                     cin >> temp_quantity;
  149.                     (*it).quantity = temp_quantity;
  150.                 }
  151.                 cout << "Status: Completed" << endl;
  152.             }
  153.         }
  154.         else if (option == 5) {
  155.             // if the user selects option 5             
  156.             // Delete record             
  157.             cout << "Enter ID        : ";
  158.             int temp_id;
  159.             cin >> temp_id; // enter id of the book that needs to be deleted             
  160.             delete_id = temp_id;                          
  161.  
  162.             cout << endl;
  163.             cout << "::Result:: " << endl;
  164.             cout << "-----------------------" << endl;
  165.             cout << "Current record: " << endl;
  166.  
  167.             // checking if any book exist with the given id             
  168.             list<Book>::iterator it = find_if(begin(ls), 
  169.                             end(ls),
  170.                             [&](const Book b) { return temp_id == (b.id); });
  171.  
  172.             if (it == ls.end()) {
  173.                 // book not found with the entered id                 
  174.                 // invalid id                 
  175.                 cout << "Error! Book ID no " << temp_id << " is not exist." << endl;
  176.             }
  177.             else {
  178.                 // book with temp_id found                 
  179.                 // so we need to delete this book from the record                 
  180.                 cout << "Book ID       : " << (*it).id << endl;
  181.                 cout << "Book Price    : " << (*it).price << endl;
  182.                 cout << "Quantity Sold : " << (*it).quantity << endl;                 
  183.                 ls.remove_if(helper); // helper is the predicate function                  
  184.                 cout << endl << "Status: Deleted" << endl; // Deleted successfully              
  185.                 }                                   
  186.             }
  187.  
  188.             cout << endl << "Enter Y to continue or X to exit : "; // enter y, if you want to see the menu again         
  189.             char ch;
  190.             cin >> ch;
  191.             if (ch != 'Y') {
  192.                 // if entered other than 'Y', then exit after saying thank you             
  193.                 cout << "Thank you" << endl;             
  194.                 break;
  195.             }
  196.  
  197.         }     
  198.         return 0;
  199.     }
  200.  
  201.  

Editor

You can edit this paste and save as new:


File Description
  • GROUP DATA STRUCT
  • Paste Code
  • 01 Dec-2022
  • 7.91 Kb
You can Share it: