- #include <iostream>
- #include <list>
- using namespace std;
- int delete_id;
- // Book Node has 3 attributes: - id, price, and quantity
- class Book
- {
- public:
- int id;
- float price;
- int quantity;
- };
- // Predicate implemented as a function to help in deleting the book with given id
- bool helper (const Book& b) { return (b.id) == delete_id;
- }
- int main()
- {
- list<Book> ls; // STL list to store book info
- // we have to keep asking for all the five options from the user until the user type 'N'
- while (1) {
- // printing menu
- cout << "-----------------------" << endl;
- cout << " :: MENU :: " << endl;
- cout << "1. Add new record" << endl;
- cout << "2. Search record" << endl;
- cout << "3. Display record" << endl;
- cout << "4. Update record" << endl;
- cout << "5. Delete record" << endl;
- cout << "-----------------------" << endl;
- cout << "Enter selection : ";
- int option;
- cin >> option; // option (1-5) to be selected
- if (option == 1) {
- // if user selects option 1
- // he/she wants to add new book
- Book b1;
- cout << "Enter ID : ";
- cin >> b1.id;
- cout << "Enter Price : ";
- cin >> b1.price;
- cout << "Enter Quantity : ";
- cin >> b1.quantity;
- ls.push_back(b1); // push this book at the end of list
- }
- else if (option == 2) {
- // if the user selects option 2
- // he/she wants to search a book from the record
- int temp_id;
- cout << "Enter ID : ";
- cin >> temp_id; // enter id of the book you want to search
- // searching for the book with its id
- list<Book>::iterator it = find_if(ls.begin(),
- ls.end(),
- [&](const Book b) { return temp_id == (b.id); });
- cout << "::Result::" << endl;
- if (it == ls.end()) {
- // entered id not exist in the record
- cout << "Error! Book ID no " << temp_id << " is not exist." << endl;
- }
- else {
- // a book find with the given id
- cout << "Book ID : " << (*it).id << endl;
- cout << "Book Price : " << (*it).price << endl;
- cout << "Quantity Sold : " << (*it).quantity << endl;
- }
- }
- else if (option == 3) {
- // if the user selects option 3
- // Display record
- cout << endl;
- cout << "-----------------------" << endl;
- cout << " :: RECORD :: " << endl;
- cout << "-----------------------" << endl;
- cout << endl;
- int i = 1; // counter of books
- // iterating through the list
- // it is the iterator of the list
- for (list<Book>::iterator it = ls.begin(); it != ls.end(); it++) {
- cout << "Record " << i << ": " << endl;
- cout << "Book ID No : " << (*it).id << endl;
- cout << "Book price : " << (*it).price << endl;
- cout << "Quantity Sold : " << (*it).quantity << endl;
- cout << endl;
- i++;
- }
- cout << "End of record" << endl;
- cout << "No of record in memory : " << ls.size() << endl;
- }
- else if (option == 4) {
- // if user selects option 4
- // Update record
- cout << "Enter ID : ";
- int temp_id;
- cin >> temp_id;
- cout << endl;
- cout << "::Result:: " << endl;
- cout << "-----------------------" << endl;
- cout << "Current record: " << endl;
- // checking if any book exist with the given id
- list<Book>::iterator it = find_if(begin(ls),
- end(ls),
- [&](const Book b) { return temp_id == (b.id); });
- if (it == ls.end()) {
- // book not found with the entered id
- // invalid id
- cout << "Error! Book ID no " << temp_id << " is not exist." << endl;
- }
- else {
- // book with temp_id found
- cout << "Book ID : " << (*it).id << endl;
- cout << "1. Book Price : " << (*it).price << endl;
- cout << "2. Quantity Sold : " << (*it).quantity << endl;
- cout << "Enter choice to update: ";
- int choice;
- cin >> choice;
- cout << "-----------------------" << endl;
- cout << endl;
- if (choice == 1) {
- // update the price of the selected book
- cout << "Enter new value of price: ";
- float temp_price;
- cin >> temp_price;
- (*it).price = temp_price;
- }
- else if (choice == 2) {
- // update the quantity of the selected book
- cout << "Enter new value of quantity: ";
- int temp_quantity;
- cin >> temp_quantity;
- (*it).quantity = temp_quantity;
- }
- cout << "Status: Completed" << endl;
- }
- }
- else if (option == 5) {
- // if the user selects option 5
- // Delete record
- cout << "Enter ID : ";
- int temp_id;
- cin >> temp_id; // enter id of the book that needs to be deleted
- delete_id = temp_id;
- cout << endl;
- cout << "::Result:: " << endl;
- cout << "-----------------------" << endl;
- cout << "Current record: " << endl;
- // checking if any book exist with the given id
- list<Book>::iterator it = find_if(begin(ls),
- end(ls),
- [&](const Book b) { return temp_id == (b.id); });
- if (it == ls.end()) {
- // book not found with the entered id
- // invalid id
- cout << "Error! Book ID no " << temp_id << " is not exist." << endl;
- }
- else {
- // book with temp_id found
- // so we need to delete this book from the record
- cout << "Book ID : " << (*it).id << endl;
- cout << "Book Price : " << (*it).price << endl;
- cout << "Quantity Sold : " << (*it).quantity << endl;
- ls.remove_if(helper); // helper is the predicate function
- cout << endl << "Status: Deleted" << endl; // Deleted successfully
- }
- }
- cout << endl << "Enter Y to continue or X to exit : "; // enter y, if you want to see the menu again
- char ch;
- cin >> ch;
- if (ch != 'Y') {
- // if entered other than 'Y', then exit after saying thank you
- cout << "Thank you" << endl;
- break;
- }
- }
- return 0;
- }
[text] GROUP DATA STRUCT
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: