#include #include #include typedef char string[100]; struct node { char StockNbr[10]; int QtyInStock; int ReOrderLevel; node* Next; }; typedef node* ptrType; ptrType ptrHead = NULL; ptrType ptrTail = NULL; ptrType ptrPre = NULL; ptrType ptrCur = NULL; ptrType ptrNew = NULL; void PrintHelp() { cout << "H (help) Provides a summary of available commands\n"; cout << "R (read) Read the inventory from the specified file\n"; cout << "W (write) Write the inventory to the specified file\n"; cout << "L (list) List the entire inventory in alphabetical order\n"; cout << "I (inquire) Display the inventory information for the specified item\n"; cout << "N (new) Adds the specified item to the inventory\n"; cout << "D (delete) Deletes the specified item from the inventory\n"; cout << "B (buy) Adds to the quantity in stock for the specified item\n"; cout << "S (sell) Subtract from the quantity in stock for the specified item\n"; cout << "Q (quit) Exits the program\n"; } void AddItem(string strInput) { ptrNew = new node; strcpy(ptrNew->StockNbr, strInput); ptrNew->QtyInStock = 0; cout << "Enter re-order level: "; cin >> ptrNew->ReOrderLevel; if ( ptrHead == NULL ) { ptrNew->Next = ptrHead; ptrHead = ptrNew; } else { for (ptrPre=NULL, ptrCur=ptrHead; (ptrCur!=NULL) && strcmp(strInput, ptrCur->StockNbr) > 0; ptrPre=ptrCur, ptrCur=ptrCur->Next ); ptrNew->Next = ptrCur; ptrPre->Next = ptrNew; } cout << ptrNew->StockNbr << " has been added to the inventory\n"; } void ListInventory() { if ( ptrHead == NULL ) cout << "ListInventory Error: File Not Read!!\n"; else { for (ptrCur=ptrHead; ptrCur!=NULL; ptrCur = ptrCur->Next) { cout << ptrCur->StockNbr << " " << ptrCur->QtyInStock << " " << ptrCur->ReOrderLevel << "\n"; } } } void ItemInfo(string strInput) { if ( ptrHead == NULL ) cout << "ItemInfo Error: File Not Read!!\n"; else { for (ptrCur=ptrHead; (ptrCur!=NULL) && (strcmp(ptrCur->StockNbr, strInput)!=0); ptrCur = ptrCur->Next); if (ptrCur!=NULL) cout << ptrCur->StockNbr << " " << ptrCur->QtyInStock << " " << ptrCur->ReOrderLevel << "\n"; } } void BuyItem(string strInput) { if ( ptrHead == NULL ) cout << "BuyItem Error: File Not Read!!\n"; else { for (ptrCur=ptrHead; (ptrCur!=NULL) && (strcmp(ptrCur->StockNbr, strInput)!=0); ptrCur = ptrCur->Next); if (ptrCur!=NULL) { int amtBuy = 0; cout << "Enter amount bought: "; cin >> amtBuy; ptrCur->QtyInStock += amtBuy; } else cout << strInput << " not found.\n"; } } void SellItem(string strInput) { if ( ptrHead == NULL ) cout << "SellItem Error: File Not Read!!\n"; else { for (ptrCur=ptrHead; (ptrCur!=NULL) && (strcmp(ptrCur->StockNbr, strInput)!=0); ptrCur = ptrCur->Next); if (ptrCur!=NULL) { int amtSold = 0; cout << "Enter amount sold: "; cin >> amtSold; if ( amtSold > ptrCur->QtyInStock ) cout << "Can't sell more than you have!\n"; else ptrCur->QtyInStock -= amtSold; } else cout << strInput << " not found.\n"; } } void WriteFile(string strInput) { if ( ptrHead == NULL ) cout << "WriteFile Error: File Not Read!!\n"; else { ofstream OutFile(strInput); for (ptrCur=ptrHead; ptrCur!=NULL; ptrCur = ptrCur->Next) { OutFile << ptrCur->StockNbr << " " << ptrCur->QtyInStock << " " << ptrCur->ReOrderLevel << "\n"; } OutFile.close(); cout << "Records written to " << strInput << "\n"; } } void ReadFile(string strInput) { if ( ptrHead != NULL ) cout << "ReadFile Error: File Already Read!!\n"; else { ifstream InFile(strInput); while ( !InFile.eof() ) { ptrNew = new node; InFile >> ptrNew->StockNbr; InFile >> ptrNew->QtyInStock; InFile >> ptrNew->ReOrderLevel; if ( ptrNew->QtyInStock >= 0 ) { if ( ptrHead == NULL ) { ptrNew->Next = ptrHead; ptrHead = ptrNew; } else { for (ptrPre=NULL, ptrCur=ptrHead; (ptrCur!=NULL) && strcmp(strInput, ptrCur->StockNbr) > 0; ptrPre=ptrCur, ptrCur=ptrCur->Next ); ptrNew->Next = ptrCur; ptrPre->Next = ptrNew; } } } InFile.close(); } } void DelItem(string strInput) { if ( ptrHead == NULL ) cout << "DelItem Error: No Inventory Items!!\n"; else { for (ptrPre=NULL, ptrCur=ptrHead; (ptrCur!=NULL) && strcmp(strInput, ptrCur->StockNbr) > 0; ptrPre=ptrCur, ptrCur=ptrCur->Next ); if ( strcmp(strInput, ptrCur->StockNbr) == 0 ) { if ( ptrPre == NULL ) { ptrHead = ptrHead->Next; ptrCur->Next = NULL; delete ptrCur; ptrCur = NULL; } else if (ptrCur->Next == NULL ) { ptrPre->Next = NULL; ptrCur->Next = NULL; delete ptrCur; ptrCur = NULL; } else { ptrPre->Next = ptrCur->Next; ptrCur->Next = NULL; delete ptrCur; ptrCur = NULL; } cout << ptrNew->StockNbr << " has been removed from the inventory\n"; } else cout << strInput << " not found.\n"; } } void main() { string strInput; string strParm; char *token; do { cout << "\nPlease Enter a Command: "; cin.getline(strInput, 100); if ( strlen(strInput) > 1 ) { token = strtok( strInput, " " ); token = strtok( NULL, " " ); strcpy(strParm, token); } switch ( strInput[0]) { case 'H': // Help: Provides a summary of available commands case 'h': PrintHelp(); break; case 'N': // New: Adds the specified StockNbr to the inventory. Prompt for ReOrderLevel. case 'n': AddItem(strParm); break; case 'L': // List: List the entire inventory in alphabetical order case 'l': ListInventory(); break; case 'I': // Inquire: Display the inventory information for the specified StockNbr case 'i': ItemInfo(strParm); break; case 'B': // Buy: Adds to the quantity in stock for the specified StockNbr. Prompt for quantity. case 'b': BuyItem(strParm); break; case 'S': // Sell: Subtract from the quantity in stock for the specified StockNbr. Prompt for quantity. case 's': SellItem(strParm); break; case 'W': // Write: Write the inventory to the specified file case 'w': WriteFile(strParm); break; case 'R': // Read: Read the inventory from the specified file case 'r': ReadFile(strParm); break; case 'D': // Delete: Deletes the specified StockNbr from the inventory. case 'd': DelItem(strParm); break; case 'Q': case 'q': break; default: cout << "\nInvalid Command"; } } while ( strInput[0] != 'Q' && strInput[0] != 'q' ); }