sidrs

OOP Ass 6B - Personal Record (STL Vectors)

Sep 20th, 2024
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. class Item {
  7. public:
  8.     string name;
  9.     string dob;
  10.     string phone;
  11.     int roll;
  12. public:
  13.     bool operator == (const Item& obj) {
  14.         if (roll == obj.roll) return 1;
  15.         return 0;
  16.     }
  17.  
  18.     bool operator < (const Item& obj) {
  19.         if (roll < obj.roll) return 1;
  20.         return 0;
  21.     }
  22. };
  23.  
  24. bool comp(const Item& obj1, const Item& obj2) {
  25.     return obj1.roll< obj2.roll;
  26. }
  27.  
  28. vector<Item> items;
  29.  
  30. void display();
  31. void insert();
  32. void search();
  33. void remove();
  34. void print(Item &obj);
  35.  
  36. int main() {
  37.  
  38.     int choice = 0;
  39.     while (true) {
  40.         cout << endl;
  41.         cout << "------------- MENU -------------" << endl;
  42.         cout << "1. Add Student" << endl;
  43.         cout << "2. Search for a Student" << endl;
  44.         cout << "3. Display all Records" << endl;
  45.         cout << "4. Display sorted records (by Roll No.)" << endl;
  46.         cout << "5. Remove a Student" << endl;
  47.         cout << "6. Exit" << endl;
  48.         cout << "--------------------------------" << endl;
  49.         cout << "Enter your choice: "; cin >> choice;
  50.         cout << "--------------------------------" << endl;
  51.         cout << endl;
  52.  
  53.         if (choice == 1) insert();
  54.         else if (choice == 2) search();
  55.         else if (choice == 3) display();
  56.         else if (choice == 4) {
  57.             sort(items.begin(), items.end(), comp);
  58.             display();
  59.         }
  60.         else if (choice == 5) remove();
  61.         else if (choice == 6) {
  62.             cout << "\n\n" << endl;
  63.             cout << "Terminating the program..." << endl;
  64.             break;
  65.         }
  66.         else cout << "ERROR! Enter a Valid Choice." << endl;
  67.        
  68.     }
  69.  
  70.     return 0;
  71. }
  72.  
  73. void insert() {
  74.     Item item;
  75.     cout << "--------- Add Student ----------" << endl;
  76.     cout << "Enter Student Roll: "; cin >> item.roll;
  77.     cout << "Enter Student Name: "; cin >> item.name;
  78.     cout << "Enter Phone No.: "; cin >> item.phone;
  79.     cout << "Enter DOB: "; cin >> item.dob;
  80.     cout << "--------------------------------" << endl;
  81.     cout << "Record successfully added." << endl;
  82.     cout << "--------------------------------" << endl;
  83.     items.push_back(item);
  84. }
  85.  
  86. void search() {
  87.     vector<Item>::iterator it;
  88.     Item item;
  89.     cout << "-------- Student Lookup --------" << endl;
  90.     cout << "Enter Student Roll No: "; cin >> item.code;
  91.     cout << "Searching for the required Student... " << endl;
  92.     it = find(items.begin(), items.end(), item);
  93.     cout << "--------------------------------" << endl;
  94.     if (it == items.end()) {
  95.         cout << "Student Not found" << endl;
  96.     } else {
  97.         cout << "Student Found" << endl;
  98.     }
  99.     cout << "--------------------------------" << endl;
  100. }
  101.  
  102. void remove() {
  103.     vector<Item>::iterator it;
  104.     Item item;
  105.     cout << "--------- Record Removal ----------" << endl;
  106.     cout << "Enter Roll No.: "; cin >> item.code;
  107.     it = find(items.begin(), items.end(), item);
  108.     cout << "--------------------------------" << endl;
  109.     if (it == items.end()) {
  110.         cout << "ERROR! Record Not Found" << endl;
  111.     } else {
  112.         items.erase(it);
  113.         cout << "Record has been Removed" << endl;
  114.     }
  115.     cout << "--------------------------------" << endl;
  116. }
  117.  
  118. void print(Item &obj){
  119.     cout << endl;
  120.     cout << "--------------------------------" << endl;
  121.     cout << "Item Code: " << obj.code << endl;
  122.     cout << "Item Name: " << obj.name << endl;
  123.     cout << "Item Cost: " << obj.cost << endl;
  124.     cout << "Item Quantity: " << obj.quantity << endl;
  125.     cout << "--------------------------------" << endl;
  126.     cout << endl;
  127. }
  128.  
  129. void display() {
  130.     cout << endl;
  131.     cout << "--------- Item Display ---------" << endl;
  132.     for_each(items.begin(), items.end(), print);
  133. }
  134.  
  135.  
  136.  
Advertisement
Add Comment
Please, Sign In to add comment