Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- class Item {
- public:
- string name;
- string dob;
- string phone;
- int roll;
- public:
- bool operator == (const Item& obj) {
- if (roll == obj.roll) return 1;
- return 0;
- }
- bool operator < (const Item& obj) {
- if (roll < obj.roll) return 1;
- return 0;
- }
- };
- bool comp(const Item& obj1, const Item& obj2) {
- return obj1.roll< obj2.roll;
- }
- vector<Item> items;
- void display();
- void insert();
- void search();
- void remove();
- void print(Item &obj);
- int main() {
- int choice = 0;
- while (true) {
- cout << endl;
- cout << "------------- MENU -------------" << endl;
- cout << "1. Add Student" << endl;
- cout << "2. Search for a Student" << endl;
- cout << "3. Display all Records" << endl;
- cout << "4. Display sorted records (by Roll No.)" << endl;
- cout << "5. Remove a Student" << endl;
- cout << "6. Exit" << endl;
- cout << "--------------------------------" << endl;
- cout << "Enter your choice: "; cin >> choice;
- cout << "--------------------------------" << endl;
- cout << endl;
- if (choice == 1) insert();
- else if (choice == 2) search();
- else if (choice == 3) display();
- else if (choice == 4) {
- sort(items.begin(), items.end(), comp);
- display();
- }
- else if (choice == 5) remove();
- else if (choice == 6) {
- cout << "\n\n" << endl;
- cout << "Terminating the program..." << endl;
- break;
- }
- else cout << "ERROR! Enter a Valid Choice." << endl;
- }
- return 0;
- }
- void insert() {
- Item item;
- cout << "--------- Add Student ----------" << endl;
- cout << "Enter Student Roll: "; cin >> item.roll;
- cout << "Enter Student Name: "; cin >> item.name;
- cout << "Enter Phone No.: "; cin >> item.phone;
- cout << "Enter DOB: "; cin >> item.dob;
- cout << "--------------------------------" << endl;
- cout << "Record successfully added." << endl;
- cout << "--------------------------------" << endl;
- items.push_back(item);
- }
- void search() {
- vector<Item>::iterator it;
- Item item;
- cout << "-------- Student Lookup --------" << endl;
- cout << "Enter Student Roll No: "; cin >> item.code;
- cout << "Searching for the required Student... " << endl;
- it = find(items.begin(), items.end(), item);
- cout << "--------------------------------" << endl;
- if (it == items.end()) {
- cout << "Student Not found" << endl;
- } else {
- cout << "Student Found" << endl;
- }
- cout << "--------------------------------" << endl;
- }
- void remove() {
- vector<Item>::iterator it;
- Item item;
- cout << "--------- Record Removal ----------" << endl;
- cout << "Enter Roll No.: "; cin >> item.code;
- it = find(items.begin(), items.end(), item);
- cout << "--------------------------------" << endl;
- if (it == items.end()) {
- cout << "ERROR! Record Not Found" << endl;
- } else {
- items.erase(it);
- cout << "Record has been Removed" << endl;
- }
- cout << "--------------------------------" << endl;
- }
- void print(Item &obj){
- cout << endl;
- cout << "--------------------------------" << endl;
- cout << "Item Code: " << obj.code << endl;
- cout << "Item Name: " << obj.name << endl;
- cout << "Item Cost: " << obj.cost << endl;
- cout << "Item Quantity: " << obj.quantity << endl;
- cout << "--------------------------------" << endl;
- cout << endl;
- }
- void display() {
- cout << endl;
- cout << "--------- Item Display ---------" << endl;
- for_each(items.begin(), items.end(), print);
- }
Advertisement
Add Comment
Please, Sign In to add comment