Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include "CSVReader2.h"
- #include "AVLTreeNode.h"
- #include "AVLTreeNode.cpp"
- #include <string>
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- int main(int argc, char** argv) {
- int answer;
- string name;
- int pID;
- int quantity;
- int price;
- int Count=0;
- int TotalQuantityCheck = 0;
- int TotalPriceCheck = 0;
- bool emptyTree = true;
- int IDwMax;
- int IDwMin = -1;
- //Step 1: load the entire StoreInventoryStock.csv into rd
- CSVReader *rd = new CSVReader("Inventory.csv",'|'); //maybe i should change this back to a ','
- AVLTree *tree = new AVLTree();
- for(int i=0; i<rd->getRowsCount(); i++)
- {
- tree->Insert(rd->getCellAsString(i,0), //name
- rd->getCellAsDouble(i,1), //price
- rd->getCellAsInt(i,2), //quantity
- rd->getCellAsInt(i,3)); //pID
- TotalQuantityCheck = TotalQuantityCheck + rd->getCellAsInt(i,2);
- TotalPriceCheck = TotalPriceCheck + (rd->getCellAsDouble(i,1) * rd->getCellAsInt(i,2));
- if(emptyTree) //shouldn't happen because tree is never empty due to items in CSV
- {
- IDwMin = rd->getCellAsInt(i,3);
- IDwMax = rd->getCellAsInt(i,3);
- }//if
- else{ //if kills of player just inserted is < current lowest kills
- if(rd->getCellAsInt(i,1) < tree->Find(IDwMin).price) //then set the current lowest kills as the lowest kills
- { //and save the current lowest's kill for later
- IDwMin = rd->getCellAsInt(i,3);
- }
- if(rd->getCellAsInt(i,1) > tree->Find(IDwMax).price) //if kills of player just inserted is > current highest
- { //then set the current highest kills as the highest kills of split
- IDwMax = rd->getCellAsInt(i,3); //and save the current highest kills for later
- }
- }
- emptyTree = false;
- }//for loop
- do{
- cout<<"**********************************************************"<<endl;
- cout<<"INVENTORY MANAGER"<<endl;
- cout<<"Choose an option:"<<endl;
- cout<<"[1] Add a new item to the inventory"<<endl;
- cout<<"[2] Display the information for an item (given the pID)"<<endl;
- cout<<"[3] Display the inventory"<<endl;
- cout<<"[4] Display inventory statistics"<<endl;
- cout<<"[5] Quit"<<endl;
- cin>>answer;
- switch(answer)
- {
- case 1:{
- cout<<"\nEnter the name of the Item: ";
- cin>>name;
- cout<<"\nEnter the price: ";
- cin>>price;
- cout<<"\nEnter the item's quantity: ";
- cin>>quantity;
- cout<<"\nEnter the item's ID: ";
- cin>>pID;
- if(pID <= 0 || price <= 0 || quantity <= 0)
- cout<<"***Error: Please enter a number greater than 0 for price, quantity and ID."<<endl;
- else {
- tree->Insert(name, price, quantity, pID);
- Count++;
- if(emptyTree)
- {
- IDwMin = pID;
- IDwMax = pID;
- }
- else{
- if(price < tree->Find(IDwMin).price) //
- {
- IDwMin = pID;
- }
- if(price > tree->Find(IDwMax).price)
- {
- IDwMax = pID;
- }
- }//else
- emptyTree = false;
- // TotalQuantityCheck + TotalQuantityCheck + rd->getCellAsInt(i,2);
- // TotalPriceCheck = TotalPriceCheck + (rd->getCellAsDouble(i,1) * rd->getCellAsInt(i,2));
- cout<<"\nThank you. Item added sucessfully.";
- ofstream myfile;
- myfile.open("Inventory.csv");
- myfile <<name<<'|'<<price<<'|'<<quantity<<'|'<<pID<<endl;
- myfile.close();
- }//else
- } break;
- case 2: {
- cout<<"\nEnter the item ID of the item to be displayed: ";
- cin>>pID;
- AVLTreeNode foundNode = tree->Find(pID);
- if (foundNode.height == -1){
- cout<<"***Error: Item with the ID = "<<pID<<" is not in the database."<<endl;
- }
- else{
- cout<<"\nItem name: "<<foundNode.Name<<", Item price: $"<<foundNode.price
- <<", Quantity: "<<foundNode.quantity<<", Item ID = "<<foundNode.PID<<endl;
- }
- } break;
- case 3: {
- cout<<"Here is the Inventory database: "<<endl;
- tree->PrintTreeInOrder();
- } break;
- case 4: {
- cout<<"Database Statistics:"<<endl;
- cout<<"\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
- /* cout<<"\tNumber of items: "<<Count+rd->getRowsCount()<<endl;
- //this should track all user inputted items + ones in csv file
- cout<<"\tTotal Number of items: "<<TotalQuantityCheck<<endl; //+ rd->getCellAsInt(i,3)<<endl;
- //this should track all user inputted prices + ones in csv file then multiply by the the quantities
- cout<<"\tTotal Inventory Cost: $"<<TotalPriceCheck<<endl;// + (rd->getCellAsDouble(i,2) * rd->getCellAsInt(i,3))<<endl;
- //Name, price, quantity and ID are displayed but using a function from earlier in main
- cout<<"\tCheapest Item: "<<endl;
- // cout<<"\n\t\tname: "<<tree->Find(IDwMin).description<<", SKU: "<<tree->Find(skuOfMin).sku
- // <<", Quantity: "<<tree->Find(skuOfMin).quantity<<", Price = $"<<tree->Find(skuOfMin).price<<endl;
- // cout<<"\nMost Expensive Item: "<<endl;
- // cout<<"\n\t\tDescription: "<<tree->Find(skuOfMax).description<<", SKU: "<<tree->Find(skuOfMax).sku
- // <<", Quantity: "<<tree->Find(skuOfMax).quantity<<", Price = $"<<tree->Find(skuOfMax).price<<endl; */
- } break;
- case 5: break;
- default: cout<<"Invalid option - please try again \n"; break;
- }//switch
- }//do
- while(answer!=5);
- ofstream myfile;
- tree->SaveTreeInOrder(myfile);
- delete tree; //just putting tree specifies that the tree is getting deleted
- //delete automatically calls the deconstructor
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement