Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Dasalla, Andre
- // PA1.cpp
- // 10/06/19
- #include <iostream>
- #include <string>
- #include <fstream>
- #include "ShoppingList.h"
- using namespace std;
- int main() {
- ifstream infile; // Read in Items
- ifstream infile2; // Read in Categories
- const char comma = ',';
- string itemsFile = "items.txt";
- string categoriesFile = "categories.txt";
- string line;
- string name, price, priority;
- string category;
- string subcategory;
- float convertPrice;
- int convertPriority;
- ShoppingList list;
- // Read in Items
- infile.open(itemsFile);
- if (infile) {
- while(getline(infile, name, comma)) {
- getline(infile, price, comma);
- getline(infile, subcategory, comma);
- getline(infile, priority);
- convertPriority = (int)atoi(priority.c_str());
- convertPrice = (float)atof(price.c_str());
- infile2.open(categoriesFile);
- if(infile2) {
- string parse, search;
- size_t found;
- int colon;
- while(getline(infile2, line)) {
- colon = line.find_first_of(":");
- parse = line.substr(0,colon);
- search = line.substr(colon);
- found = search.find(subcategory);
- if (found != std::string::npos) {
- category = parse;
- infile2.close();
- }
- else {
- category = "unknown";
- }
- }
- }
- if (category != "unknown")
- list.insert(name, convertPrice, category, subcategory, convertPriority);
- else
- cout << "Unknown subcategory, Item not inserted." << endl;
- }
- }
- infile.close();
- infile2.close();
- list.printList();
- cout << "Original Size: " << list.getSize() << endl;
- ShoppingList list3 = list.getPriority(5);
- list3.printList();
- list.remove("banana");
- list.printList();
- ShoppingList list2 = list;
- list2.printList();
- cout << "done" << endl;
- }
- CPP FILE
- // shopping.cpp
- #include <iostream>
- #include "ShoppingList.h"
- #include <string>
- using namespace std;
- ShoppingList::ShoppingList() { // Default Constructor // DONE
- size = 0; // Creates an empty list
- }
- ShoppingList::~ShoppingList() { //DONE
- // Deconstructor (Deletes all items from shoppinmgList)
- item* temp;
- if (head == nullptr)
- return;
- else {
- while(head != nullptr) {
- temp = head->next;
- delete head;
- head = temp;
- }
- }
- cout << "Deconstructor called!" << endl;
- }
- ShoppingList::ShoppingList(const ShoppingList& copy) {
- item * current = copy.head;
- while(current != nullptr) {
- insert(current->name, current->price, current->category,
- current->subcategory, current->priority);
- current = current->next;
- }
- cout << "COPY CONSTRUCTOR FINISHED" << endl;
- }
- void ShoppingList::insert(string name, float price, string category, //DONE
- string subcategory,int priority) { // Insert an item into the shopping list
- item* newItem = new item;
- newItem->name = name;
- newItem->price = price;
- newItem->category = category;
- newItem->subcategory = subcategory;
- newItem->priority = priority;
- item* temp;
- // if ShoppingList size = 0;
- if (head == nullptr) {
- head = new item;
- head = newItem;
- size++;
- }
- // If the new item has a higher priority than the head
- else if (head->priority >= newItem->priority) {
- newItem->next = head;
- head = newItem;
- size++;
- }
- // else (insert item)
- // Items should be ordered by priority, from highest to lowest.
- // If a new item has the same priority as an item in the list,
- // place it before the existing item.
- else {
- temp = head;
- while (temp->next != nullptr && temp->next->priority < newItem->priority) {
- temp = temp->next;
- }
- newItem->next = temp->next;
- temp->next = newItem;
- size++;
- }
- }
- // COPY ASSIGNMENT OPERATOR
- ShoppingList& ShoppingList::operator=(const ShoppingList& copy) {
- // Copy all nodes from one shopping list to a new shoppinglist
- if (this != ©) {
- if (copy.head == nullptr)
- head = nullptr;
- else {
- item * current = copy.head;
- while(current != nullptr) {
- insert(current->name, current->price, current->category,
- current->subcategory, current->priority);
- current = current->next;
- }
- }
- }
- cout << "OVERLOADED ASSIGNMENT OPERATOR FINISHED" << endl;
- return *this;
- }
- void ShoppingList::remove(string name) { // remove an item from the shopping list (by name) // DONE
- item* temp;
- item* prev;
- temp = head;
- if (temp->name == name) {
- head = head->next;
- delete temp;
- }
- else {
- while(temp != nullptr && temp->name != name) {
- prev = temp;
- temp = temp->next;
- }
- if (temp == nullptr)
- return;
- prev->next = temp->next;
- delete temp;
- size--;
- }
- }
- int ShoppingList::getSize() { // DONE
- return size;
- }
- ShoppingList ShoppingList::getPriority(int priority) {
- // Returns all items with priority of passed in number
- item * temp = head;
- ShoppingList newList;
- while(temp != nullptr) {
- if(temp->priority == priority) {
- newList.insert(temp->name, temp->price, temp->category, temp->subcategory, temp->priority);
- }
- temp = temp->next;
- }
- return newList;
- }
- ShoppingList ShoppingList::getCategory(string category) {
- // Traverse the shopping list and return the items within the given category
- ShoppingList newList;
- item* temp = head;
- while(temp != nullptr) {
- if(temp->category == category) {
- newList.insert(temp->name, temp->price, temp->category, temp->subcategory, temp->priority);
- cout << "Added Node" << endl;
- }
- temp = temp->next;
- }
- return newList;
- }
- void ShoppingList::printList() { // DONE
- item* temp;
- temp = head;
- while(temp != nullptr) {
- cout << temp->name << " - $" << temp->price << " - " << temp->category <<
- " - " << temp->subcategory << endl;
- temp = temp->next;
- cout << "Printed" << endl;
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement