sidrs

OOP Ass 6A - Inventory (STL Vector Containers)

Sep 20th, 2024
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.81 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.     int code;
  10.     int cost;
  11.     int quantity;
  12. public:
  13.     bool operator == (const Item& obj) {
  14.         if (code == obj.code) return 1;
  15.         return 0;
  16.     }
  17.  
  18.     bool operator < (const Item& obj) {
  19.         if (code < obj.code) return 1;
  20.         return 0;
  21.     }
  22. };
  23.  
  24. bool comp(const Item& obj1, const Item& obj2) {
  25.     return obj1.cost < obj2.cost;
  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. Insert Item" << endl;
  43.         cout << "2. Search for an Item" << endl;
  44.         cout << "3. Display All Records" << endl;
  45.         cout << "4. Display Sorted Records (By Cost)" << endl;
  46.         cout << "5. Remove an Item" << 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 << "--------- Insert Item ----------" << endl;
  76.     cout << "Enter Item Code: "; cin >> item.code;
  77.     cout << "Enter Item Name: "; cin >> item.name;
  78.     cout << "Enter Item Quantity: "; cin >> item.quantity;
  79.     cout << "Enter Item Cost: "; cin >> item.cost;
  80.     cout << "--------------------------------" << endl;
  81.     cout << "Item 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 << "--------- Item Lookup ----------" << endl;
  90.     cout << "Enter Item Code: "; cin >> item.code;
  91.     cout << "Searching for the required Item... " << endl;
  92.     it = find(items.begin(), items.end(), item);
  93.     cout << "--------------------------------" << endl;
  94.     if (it == items.end()) {
  95.         cout << "Item Not found" << endl;
  96.     } else {
  97.         cout << "Item Found" << endl;
  98.     }
  99.     cout << "--------------------------------" << endl;
  100. }
  101.  
  102. void remove() {
  103.     vector<Item>::iterator it;
  104.     Item item;
  105.     cout << "--------- Item Removal ----------" << endl;
  106.     cout << "Enter Item Code: "; cin >> item.code;
  107.     it = find(items.begin(), items.end(), item);
  108.     cout << "--------------------------------" << endl;
  109.     if (it == items.end()) {
  110.         cout << "ERROR! Item Not Found" << endl;
  111.     } else {
  112.         items.erase(it);
  113.         cout << "Item 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. /* OUTPUT
  137.  
  138. ------------- MENU -------------
  139. 1. Insert Item
  140. 2. Search for an Item
  141. 3. Display All Records
  142. 4. Display Sorted Records
  143. 5. Remove an Item
  144. 6. Exit
  145. --------------------------------
  146. Enter your choice: 1
  147. --------------------------------
  148.  
  149. --------- Insert Item ----------
  150. Enter Item Code: 1
  151. Enter Item Name: Monitor
  152. Enter Item Quantity: 15
  153. Enter Item Cost: 89
  154. --------------------------------
  155. Item successfully added.
  156. --------------------------------
  157.  
  158. ------------- MENU -------------
  159. 1. Insert Item
  160. 2. Search for an Item
  161. 3. Display All Records
  162. 4. Display Sorted Records
  163. 5. Remove an Item
  164. 6. Exit
  165. --------------------------------
  166. Enter your choice: 1
  167. --------------------------------
  168.  
  169. --------- Insert Item ----------
  170. Enter Item Code: 2
  171. Enter Item Name: CPU              
  172. Enter Item Quantity: 10
  173. Enter Item Cost: 125
  174. --------------------------------
  175. Item successfully added.
  176. --------------------------------
  177.  
  178. ------------- MENU -------------
  179. 1. Insert Item
  180. 2. Search for an Item
  181. 3. Display All Records
  182. 4. Display Sorted Records
  183. 5. Remove an Item
  184. 6. Exit
  185. --------------------------------
  186. Enter your choice: 1
  187. --------------------------------
  188.  
  189. --------- Insert Item ----------
  190. Enter Item Code: 3
  191. Enter Item Name: Keyboard
  192. Enter Item Quantity: 25
  193. Enter Item Cost: 35
  194. --------------------------------
  195. Item successfully added.
  196. --------------------------------
  197.  
  198. ------------- MENU -------------
  199. 1. Insert Item
  200. 2. Search for an Item
  201. 3. Display All Records
  202. 4. Display Sorted Records
  203. 5. Remove an Item
  204. 6. Exit
  205. --------------------------------
  206. Enter your choice: 1
  207. --------------------------------
  208.  
  209. --------- Insert Item ----------
  210. Enter Item Code: 5
  211. Enter Item Name: Mouse
  212. Enter Item Quantity: 40
  213. Enter Item Cost: 20
  214. --------------------------------
  215. Item successfully added.
  216. --------------------------------
  217.  
  218. ------------- MENU -------------
  219. 1. Insert Item
  220. 2. Search for an Item
  221. 3. Display All Records
  222. 4. Display Sorted Records
  223. 5. Remove an Item
  224. 6. Exit
  225. --------------------------------
  226. Enter your choice: 3
  227. --------------------------------
  228.  
  229.  
  230. --------- Item Display ---------
  231.  
  232. --------------------------------
  233. Item Code: 1
  234. Item Name: Monitor
  235. Item Cost: 89
  236. Item Quantity: 15
  237. --------------------------------
  238.  
  239.  
  240. --------------------------------
  241. Item Code: 2
  242. Item Name: CPU
  243. Item Cost: 125
  244. Item Quantity: 10
  245. --------------------------------
  246.  
  247.  
  248. --------------------------------
  249. Item Code: 3
  250. Item Name: Keyboard
  251. Item Cost: 35
  252. Item Quantity: 25
  253. --------------------------------
  254.  
  255.  
  256. --------------------------------
  257. Item Code: 5
  258. Item Name: Mouse
  259. Item Cost: 20
  260. Item Quantity: 40
  261. --------------------------------
  262.  
  263.  
  264. ------------- MENU -------------
  265. 1. Insert Item
  266. 2. Search for an Item
  267. 3. Display All Records
  268. 4. Display Sorted Records
  269. 5. Remove an Item
  270. 6. Exit
  271. --------------------------------
  272. Enter your choice: 5
  273. --------------------------------
  274.  
  275. --------- Item Removal ----------
  276. Enter Item Code: 5
  277. --------------------------------
  278. Item has been Removed
  279. --------------------------------
  280.  
  281. ------------- MENU -------------
  282. 1. Insert Item
  283. 2. Search for an Item
  284. 3. Display All Records
  285. 4. Display Sorted Records
  286. 5. Remove an Item
  287. 6. Exit
  288. --------------------------------
  289. Enter your choice: 2
  290. --------------------------------
  291.  
  292. --------- Item Lookup ----------
  293. Enter Item Code: 5
  294. Searching for the required Item...
  295. --------------------------------
  296. Item Not found
  297. --------------------------------
  298.  
  299. ------------- MENU -------------
  300. 1. Insert Item
  301. 2. Search for an Item
  302. 3. Display All Records
  303. 4. Display Sorted Records
  304. 5. Remove an Item
  305. 6. Exit
  306. --------------------------------
  307. Enter your choice: 4
  308. --------------------------------
  309.  
  310.  
  311. --------- Item Display ---------
  312.  
  313. --------------------------------
  314. Item Code: 3
  315. Item Name: Keyboard
  316. Item Cost: 35
  317. Item Quantity: 25
  318. --------------------------------
  319.  
  320.  
  321. --------------------------------
  322. Item Code: 1
  323. Item Name: Monitor
  324. Item Cost: 89
  325. Item Quantity: 15
  326. --------------------------------
  327.  
  328.  
  329. --------------------------------
  330. Item Code: 2
  331. Item Name: CPU
  332. Item Cost: 125
  333. Item Quantity: 10
  334. --------------------------------
  335.  
  336.  
  337. ------------- MENU -------------
  338. 1. Insert Item
  339. 2. Search for an Item
  340. 3. Display All Records
  341. 4. Display Sorted Records
  342. 5. Remove an Item
  343. 6. Exit
  344. --------------------------------
  345. Enter your choice: 6
  346. --------------------------------
  347.  
  348. Terminating the program...
  349.  
  350. */
  351.  
Advertisement
Add Comment
Please, Sign In to add comment