Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. // Dasalla, Andre
  2. // PA1.cpp
  3. // 10/06/19
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include "ShoppingList.h"
  9. using namespace std;
  10.  
  11.  
  12. int main() {
  13. ifstream infile; // Read in Items
  14. ifstream infile2; // Read in Categories
  15. const char comma = ',';
  16. string itemsFile = "items.txt";
  17. string categoriesFile = "categories.txt";
  18. string line;
  19. string name, price, priority;
  20. string category;
  21. string subcategory;
  22. float convertPrice;
  23. int convertPriority;
  24. ShoppingList list;
  25.  
  26. // Read in Items
  27. infile.open(itemsFile);
  28. if (infile) {
  29. while(getline(infile, name, comma)) {
  30. getline(infile, price, comma);
  31. getline(infile, subcategory, comma);
  32. getline(infile, priority);
  33.  
  34.  
  35. convertPriority = (int)atoi(priority.c_str());
  36. convertPrice = (float)atof(price.c_str());
  37.  
  38. infile2.open(categoriesFile);
  39. if(infile2) {
  40. string parse, search;
  41. size_t found;
  42. int colon;
  43. while(getline(infile2, line)) {
  44. colon = line.find_first_of(":");
  45. parse = line.substr(0,colon);
  46. search = line.substr(colon);
  47. found = search.find(subcategory);
  48. if (found != std::string::npos) {
  49. category = parse;
  50. infile2.close();
  51. }
  52. else {
  53. category = "unknown";
  54. }
  55.  
  56.  
  57. }
  58. }
  59. if (category != "unknown")
  60. list.insert(name, convertPrice, category, subcategory, convertPriority);
  61. else
  62. cout << "Unknown subcategory, Item not inserted." << endl;
  63. }
  64.  
  65. }
  66.  
  67. infile.close();
  68. infile2.close();
  69. list.printList();
  70. cout << "Original Size: " << list.getSize() << endl;
  71. ShoppingList list3 = list.getPriority(5);
  72. list3.printList();
  73. list.remove("banana");
  74. list.printList();
  75. ShoppingList list2 = list;
  76. list2.printList();
  77. cout << "done" << endl;
  78.  
  79. }
  80.  
  81.  
  82. CPP FILE
  83. // shopping.cpp
  84. #include <iostream>
  85. #include "ShoppingList.h"
  86. #include <string>
  87. using namespace std;
  88.  
  89.  
  90. ShoppingList::ShoppingList() { // Default Constructor // DONE
  91. size = 0; // Creates an empty list
  92. }
  93.  
  94. ShoppingList::~ShoppingList() { //DONE
  95. // Deconstructor (Deletes all items from shoppinmgList)
  96. item* temp;
  97. if (head == nullptr)
  98. return;
  99. else {
  100. while(head != nullptr) {
  101. temp = head->next;
  102. delete head;
  103. head = temp;
  104. }
  105. }
  106. cout << "Deconstructor called!" << endl;
  107. }
  108.  
  109.  
  110. ShoppingList::ShoppingList(const ShoppingList& copy) {
  111. item * current = copy.head;
  112. while(current != nullptr) {
  113. insert(current->name, current->price, current->category,
  114. current->subcategory, current->priority);
  115. current = current->next;
  116. }
  117. cout << "COPY CONSTRUCTOR FINISHED" << endl;
  118. }
  119.  
  120. void ShoppingList::insert(string name, float price, string category, //DONE
  121. string subcategory,int priority) { // Insert an item into the shopping list
  122. item* newItem = new item;
  123. newItem->name = name;
  124. newItem->price = price;
  125. newItem->category = category;
  126. newItem->subcategory = subcategory;
  127. newItem->priority = priority;
  128. item* temp;
  129.  
  130. // if ShoppingList size = 0;
  131. if (head == nullptr) {
  132. head = new item;
  133. head = newItem;
  134. size++;
  135. }
  136.  
  137. // If the new item has a higher priority than the head
  138. else if (head->priority >= newItem->priority) {
  139. newItem->next = head;
  140. head = newItem;
  141. size++;
  142. }
  143.  
  144. // else (insert item)
  145. // Items should be ordered by priority, from highest to lowest.
  146. // If a new item has the same priority as an item in the list,
  147. // place it before the existing item.
  148. else {
  149. temp = head;
  150. while (temp->next != nullptr && temp->next->priority < newItem->priority) {
  151. temp = temp->next;
  152. }
  153.  
  154. newItem->next = temp->next;
  155. temp->next = newItem;
  156. size++;
  157. }
  158. }
  159.  
  160. // COPY ASSIGNMENT OPERATOR
  161. ShoppingList& ShoppingList::operator=(const ShoppingList& copy) {
  162. // Copy all nodes from one shopping list to a new shoppinglist
  163. if (this != &copy) {
  164.  
  165. if (copy.head == nullptr)
  166. head = nullptr;
  167.  
  168. else {
  169. item * current = copy.head;
  170. while(current != nullptr) {
  171. insert(current->name, current->price, current->category,
  172. current->subcategory, current->priority);
  173. current = current->next;
  174. }
  175. }
  176. }
  177. cout << "OVERLOADED ASSIGNMENT OPERATOR FINISHED" << endl;
  178. return *this;
  179. }
  180.  
  181.  
  182. void ShoppingList::remove(string name) { // remove an item from the shopping list (by name) // DONE
  183. item* temp;
  184. item* prev;
  185. temp = head;
  186.  
  187. if (temp->name == name) {
  188. head = head->next;
  189. delete temp;
  190. }
  191. else {
  192. while(temp != nullptr && temp->name != name) {
  193. prev = temp;
  194. temp = temp->next;
  195. }
  196.  
  197. if (temp == nullptr)
  198. return;
  199. prev->next = temp->next;
  200. delete temp;
  201. size--;
  202. }
  203. }
  204.  
  205. int ShoppingList::getSize() { // DONE
  206. return size;
  207. }
  208.  
  209. ShoppingList ShoppingList::getPriority(int priority) {
  210. // Returns all items with priority of passed in number
  211. item * temp = head;
  212. ShoppingList newList;
  213. while(temp != nullptr) {
  214. if(temp->priority == priority) {
  215. newList.insert(temp->name, temp->price, temp->category, temp->subcategory, temp->priority);
  216. }
  217. temp = temp->next;
  218. }
  219. return newList;
  220. }
  221.  
  222. ShoppingList ShoppingList::getCategory(string category) {
  223. // Traverse the shopping list and return the items within the given category
  224. ShoppingList newList;
  225. item* temp = head;
  226. while(temp != nullptr) {
  227. if(temp->category == category) {
  228. newList.insert(temp->name, temp->price, temp->category, temp->subcategory, temp->priority);
  229. cout << "Added Node" << endl;
  230. }
  231. temp = temp->next;
  232. }
  233. return newList;
  234. }
  235.  
  236. void ShoppingList::printList() { // DONE
  237. item* temp;
  238. temp = head;
  239. while(temp != nullptr) {
  240. cout << temp->name << " - $" << temp->price << " - " << temp->category <<
  241. " - " << temp->subcategory << endl;
  242. temp = temp->next;
  243. cout << "Printed" << endl;
  244. }
  245. cout << endl;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement