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;
- int code;
- int cost;
- int quantity;
- public:
- bool operator == (const Item& obj) {
- if (code == obj.code) return 1;
- return 0;
- }
- bool operator < (const Item& obj) {
- if (code < obj.code) return 1;
- return 0;
- }
- };
- bool comp(const Item& obj1, const Item& obj2) {
- return obj1.cost < obj2.cost;
- }
- 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. Insert Item" << endl;
- cout << "2. Search for an Item" << endl;
- cout << "3. Display All Records" << endl;
- cout << "4. Display Sorted Records (By Cost)" << endl;
- cout << "5. Remove an Item" << 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 << "--------- Insert Item ----------" << endl;
- cout << "Enter Item Code: "; cin >> item.code;
- cout << "Enter Item Name: "; cin >> item.name;
- cout << "Enter Item Quantity: "; cin >> item.quantity;
- cout << "Enter Item Cost: "; cin >> item.cost;
- cout << "--------------------------------" << endl;
- cout << "Item successfully added." << endl;
- cout << "--------------------------------" << endl;
- items.push_back(item);
- }
- void search() {
- vector<Item>::iterator it;
- Item item;
- cout << "--------- Item Lookup ----------" << endl;
- cout << "Enter Item Code: "; cin >> item.code;
- cout << "Searching for the required Item... " << endl;
- it = find(items.begin(), items.end(), item);
- cout << "--------------------------------" << endl;
- if (it == items.end()) {
- cout << "Item Not found" << endl;
- } else {
- cout << "Item Found" << endl;
- }
- cout << "--------------------------------" << endl;
- }
- void remove() {
- vector<Item>::iterator it;
- Item item;
- cout << "--------- Item Removal ----------" << endl;
- cout << "Enter Item Code: "; cin >> item.code;
- it = find(items.begin(), items.end(), item);
- cout << "--------------------------------" << endl;
- if (it == items.end()) {
- cout << "ERROR! Item Not Found" << endl;
- } else {
- items.erase(it);
- cout << "Item 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);
- }
- /* OUTPUT
- ------------- MENU -------------
- 1. Insert Item
- 2. Search for an Item
- 3. Display All Records
- 4. Display Sorted Records
- 5. Remove an Item
- 6. Exit
- --------------------------------
- Enter your choice: 1
- --------------------------------
- --------- Insert Item ----------
- Enter Item Code: 1
- Enter Item Name: Monitor
- Enter Item Quantity: 15
- Enter Item Cost: 89
- --------------------------------
- Item successfully added.
- --------------------------------
- ------------- MENU -------------
- 1. Insert Item
- 2. Search for an Item
- 3. Display All Records
- 4. Display Sorted Records
- 5. Remove an Item
- 6. Exit
- --------------------------------
- Enter your choice: 1
- --------------------------------
- --------- Insert Item ----------
- Enter Item Code: 2
- Enter Item Name: CPU
- Enter Item Quantity: 10
- Enter Item Cost: 125
- --------------------------------
- Item successfully added.
- --------------------------------
- ------------- MENU -------------
- 1. Insert Item
- 2. Search for an Item
- 3. Display All Records
- 4. Display Sorted Records
- 5. Remove an Item
- 6. Exit
- --------------------------------
- Enter your choice: 1
- --------------------------------
- --------- Insert Item ----------
- Enter Item Code: 3
- Enter Item Name: Keyboard
- Enter Item Quantity: 25
- Enter Item Cost: 35
- --------------------------------
- Item successfully added.
- --------------------------------
- ------------- MENU -------------
- 1. Insert Item
- 2. Search for an Item
- 3. Display All Records
- 4. Display Sorted Records
- 5. Remove an Item
- 6. Exit
- --------------------------------
- Enter your choice: 1
- --------------------------------
- --------- Insert Item ----------
- Enter Item Code: 5
- Enter Item Name: Mouse
- Enter Item Quantity: 40
- Enter Item Cost: 20
- --------------------------------
- Item successfully added.
- --------------------------------
- ------------- MENU -------------
- 1. Insert Item
- 2. Search for an Item
- 3. Display All Records
- 4. Display Sorted Records
- 5. Remove an Item
- 6. Exit
- --------------------------------
- Enter your choice: 3
- --------------------------------
- --------- Item Display ---------
- --------------------------------
- Item Code: 1
- Item Name: Monitor
- Item Cost: 89
- Item Quantity: 15
- --------------------------------
- --------------------------------
- Item Code: 2
- Item Name: CPU
- Item Cost: 125
- Item Quantity: 10
- --------------------------------
- --------------------------------
- Item Code: 3
- Item Name: Keyboard
- Item Cost: 35
- Item Quantity: 25
- --------------------------------
- --------------------------------
- Item Code: 5
- Item Name: Mouse
- Item Cost: 20
- Item Quantity: 40
- --------------------------------
- ------------- MENU -------------
- 1. Insert Item
- 2. Search for an Item
- 3. Display All Records
- 4. Display Sorted Records
- 5. Remove an Item
- 6. Exit
- --------------------------------
- Enter your choice: 5
- --------------------------------
- --------- Item Removal ----------
- Enter Item Code: 5
- --------------------------------
- Item has been Removed
- --------------------------------
- ------------- MENU -------------
- 1. Insert Item
- 2. Search for an Item
- 3. Display All Records
- 4. Display Sorted Records
- 5. Remove an Item
- 6. Exit
- --------------------------------
- Enter your choice: 2
- --------------------------------
- --------- Item Lookup ----------
- Enter Item Code: 5
- Searching for the required Item...
- --------------------------------
- Item Not found
- --------------------------------
- ------------- MENU -------------
- 1. Insert Item
- 2. Search for an Item
- 3. Display All Records
- 4. Display Sorted Records
- 5. Remove an Item
- 6. Exit
- --------------------------------
- Enter your choice: 4
- --------------------------------
- --------- Item Display ---------
- --------------------------------
- Item Code: 3
- Item Name: Keyboard
- Item Cost: 35
- Item Quantity: 25
- --------------------------------
- --------------------------------
- Item Code: 1
- Item Name: Monitor
- Item Cost: 89
- Item Quantity: 15
- --------------------------------
- --------------------------------
- Item Code: 2
- Item Name: CPU
- Item Cost: 125
- Item Quantity: 10
- --------------------------------
- ------------- MENU -------------
- 1. Insert Item
- 2. Search for an Item
- 3. Display All Records
- 4. Display Sorted Records
- 5. Remove an Item
- 6. Exit
- --------------------------------
- Enter your choice: 6
- --------------------------------
- Terminating the program...
- */
Advertisement
Add Comment
Please, Sign In to add comment