Advertisement
Guest User

Untitled

a guest
May 6th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. //
  2. // Admin.cpp
  3. // Project4
  4. //
  5. // Created by Hisham Alsamarrai on 5/6/19.
  6. // Copyright © 2019 Hisham Alsamarrai. All rights reserved.
  7. //
  8.  
  9. #include "Admin.h"
  10. #include <iostream>
  11. using namespace std;
  12.  
  13. void Admin::validateUserPass(string User, string Pass)
  14. {
  15. USERNAME = User;
  16. PASSWORD = Pass;
  17.  
  18. if ((User == "admin" && Pass == "admin") || (User == "ADMIN" && Pass == "ADMIN"))
  19. {
  20. cout << "Welcome ADMIN!" << endl;
  21. }
  22. else while (User != "admin" || Pass != "admin" || User != "ADMIN" || Pass != "ADMIN")
  23. {
  24. cout << "The username or password you entered is invalid! Try again!" << endl;
  25.  
  26. cout << "Username: ";
  27. cin >> User;
  28.  
  29. cout << "Password: ";
  30. cin >> Pass;
  31.  
  32. if ((User == "admin" && Pass == "admin") || (User == "ADMIN" && Pass == "ADMIN"))
  33. {
  34. cout << "Welcome ADMIN!" << endl;
  35. break;
  36. }
  37. }
  38. }
  39.  
  40. // NEWPRODUCT Function -- Creates a new node at the end of the linked list
  41. void Admin::newProduct(class LinkedList *readList)
  42. {
  43. int Number;
  44. int Name;
  45. double Cost;
  46. int Quantity;
  47.  
  48. Inventory *head = readList->getHead();; // Create a temporary node to keep track of the orginal head node
  49. Inventory *current = NULL; // Create a temporary node to use to transverse through the linked list
  50. Inventory* newNode = new Inventory(); // Create a new node to add to the end of the list
  51.  
  52. cout << "Item NUMBER for the new product: "; // Prompts user for item number
  53. cin >> Number;
  54.  
  55. cout << "Item NAME for the new product: "; // Prompts user for item name
  56. cin >> Name;
  57.  
  58. cout << "Item COST for the new product: $"; // Prompts user for item cost
  59. cin >> Cost;
  60.  
  61. cout << "Item QUANTITY for the new product: "; // Prompts user for quantity
  62. cin >> Quantity;
  63.  
  64. newNode->setItemNumber(Number); // Set the Node Data
  65. string NameAsString = to_string(Name); // Change to string
  66. newNode->setItemName(NameAsString); // Set the Node Data
  67. newNode->setItemCost(Cost); // Set the Node Data
  68. newNode->setQuantity(Quantity); // Set the Node Data
  69. newNode->next = NULL;
  70.  
  71. if (!head) { // Check to see if linked list is empty
  72. head = newNode; // If it is append the new node onto the empty list
  73. }
  74. else
  75. {
  76. current = head; // Set the current node to the head node
  77.  
  78. while (current->next) // Traverse the linked list until you reach the end
  79. {
  80. current = current->next;
  81. }
  82. current->next = newNode; // At the end set the current to point to the new node.
  83. }
  84.  
  85. readList->getHead(); // Return the new linked list to the calling function
  86. cout << endl;
  87. }
  88.  
  89. // LENGTH Function -- Goes through and counts the length of the list
  90. int Admin::length(class LinkedList *readList)
  91. {
  92. Inventory *current = readList->getHead(); // Create a temporary node to keep track of the current node
  93. int count = 0;
  94.  
  95. while (current != NULL) { // Loop through the list until we find the end
  96. count++;
  97. current = current->next;
  98. }
  99. return count;
  100. }
  101.  
  102. // DELETEPRODUCT Function -- Deletes a node in the inventoryFile
  103. void Admin::deleteProduct(class LinkedList *readList, int position)
  104. {
  105. int itemNumber; // Declares int for itemNumber
  106. cout << "Which node do you want to delete?" << endl; // Prompts user
  107. cout << "Give an item number: "; // Prompts user
  108. cin >> itemNumber;
  109.  
  110. int count = 0; // Count is used for length
  111. Inventory *current = readList->getHead(); // Creates a temporary node to traverse through the list
  112. Inventory *prev = readList->getHead(); // Creates a temporary node to go back in the list
  113.  
  114. // For invalid positions
  115. if ((position < 0 || position > length(readList)) && itemNumber != current->getItemNumber())
  116. {
  117. cout << "Invalid index!" << endl;
  118. return;
  119. }
  120. else if (position == 0) // If position is at 0
  121. {
  122. current = current->next;
  123. delete prev;
  124. readList->setHead(current);
  125. return;
  126. }
  127. else if (position == length(readList) - 1 && itemNumber == current->getItemNumber()) // If itemNumber is found within the list
  128. {
  129. while (current->next != NULL)
  130. {
  131. prev = current;
  132. current = current->next;
  133. }
  134. prev->next = NULL;
  135. delete current;
  136. return;
  137. }
  138. else // Deletes node otherwise
  139. {
  140. while (current != NULL)
  141. {
  142. if (count == position)
  143. {
  144. prev->next = current->next;
  145. delete current;
  146. return;
  147. }
  148. prev = current;
  149. current = current->next;
  150. count++;
  151. }
  152. }
  153.  
  154. cout << endl; // Creates a new line
  155. }
  156.  
  157. // UPDATEQuantity Function -- updates the quantity for an itemNumber
  158. void Admin::updateQuantity(class LinkedList *readList, class LinkedList *shoppingCart, int newQuantity, int itemNumber)
  159. {
  160. Inventory *current = readList->getHead(); // Creates a temporary node that traverses the linked list
  161.  
  162. cout << "Which product quantity would you like to update?" << endl; // Prompts user
  163. cout << "Type a product here: "; // Prompts user
  164. cin >> itemNumber; // Takes in input
  165.  
  166. // While loop that traverses checking if valid input was found
  167. while (current != NULL)
  168. {
  169. // If the input is valid
  170. if (itemNumber == current->getItemNumber())
  171. {
  172. cout << "The new quantity: "; // Prompts user for new quantity
  173. cin >> newQuantity; // Takes input
  174.  
  175. current->setQuantity(newQuantity); // Stores new quantity
  176. }
  177.  
  178. current = current->next; // Goes to the next node
  179. }
  180. cout << endl; // Creates newline
  181. }
  182.  
  183. // UPDATECost Function -- updates the cost for an itemNumber
  184. void Admin::updateCost(class LinkedList *readList, class LinkedList *shoppingCart, int newCost, int itemNumber)
  185. {
  186. Inventory *current = readList->getHead(); // Creates a temporary node that traverses the linked list
  187.  
  188. cout << "Which product quantity would you like to update?" << endl; // Prompts user
  189. cout << "Type a product here: "; // Prompts user
  190. cin >> itemNumber; // Takes in input
  191.  
  192. // While loop that traverses checking if valid input was found
  193. while (current != NULL)
  194. {
  195. // If the input is valid
  196. if (itemNumber == current->getItemNumber())
  197. {
  198. cout << "The new quantity: "; // Prompts user for new quantity
  199. cin >> newCost; // Takes input
  200.  
  201. current->setItemCost(newCost); // Stores new quantity
  202. }
  203.  
  204. current = current->next; // Goes to the next node
  205. }
  206. cout << endl; // Creates newline
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement