// ENGR 131, Monday 3:30 Recitation (Amanda Rohn) // HW #12 - Inventory Keeper // Solution #include #include #include #include #include #include #include #include "inventory.h" using std::cin; using std::cout; using std::endl; using std::ifstream; using std::ofstream; using std::ostream; using std::vector; using std::string; using aer::GetInteger; string GetStr(string prompt); void OpenIn(ifstream& in, string prompt); void OpenOut(ofstream& out, string prompt); void GetInv(ifstream& in, vector& data); void DispInv(ostream& out, vector& data); void DispInv(ostream& out, Inventory data); Inventory* Find(vector& data, int number); int main() { string outfilename; int choice, number, newinv; ifstream in; ofstream out; vector inv; Inventory* found = NULL; cout << "Welcome to the Inventory Keeper.\n"; OpenIn(in, "Enter the name of the file containing your inventory data"); cout << "\nReading data..."; GetInv(in, inv); cout << "Done.\n"; for(;;) { cout << "\nPlease enter your selection from the following list:" << "\n1. Read new inventory data from a file." << "\n2. Change inventory data." << "\n3. Display data for one product." << "\n4. Display entire inventory list (sorted by product number)." << "\n5. Exit.\n"; choice = GetInteger("selection", 1, 5); switch(choice) { case 1: OpenIn(in, "Enter the name of the file containing your inventory data"); cout << "\nReading data..."; GetInv(in, inv); cout << "Done.\n"; break; case 2: number = GetInteger("What is the number of the product whose inventory you would like to update?"); found = Find(inv, number); if(found == NULL) { cout << "\nThat product was not found in the database.\n"; break; } newinv = GetInteger("Enter the new number of items stocked", 0); found->SetStocked(newinv); break; case 3: number = GetInteger("What is the product number you would like to search for?", 0); found = Find(inv, number); if(found == NULL) { cout << "\nThat product was not found in the database.\n"; break; } choice = GetInteger("Would you like to print to (1) the screen or (2) a file?",1,2); if(choice == 1) { DispInv(cout, *found); } else { OpenOut(out, "Enter the name of the file you'd like to print to"); DispInv(out, *found); } break; case 4: choice = GetInteger("Would you like to print to (1) the screen or (2) a file?",1,2); if(choice == 1) { DispInv(cout,inv); } else { OpenOut(out, "Enter the name of the file you'd like to print to"); DispInv(out, inv); } break; default: break; } if(choice == 5) break; } cout << "\nGoodbye.\n"; return 0; } string GetStr(string prompt) { string input; for(;;) { cout << prompt << ": "; cin >> input; if(cin.fail()) { cout << "\nInvalid input. Try again.\n"; cin.clear(); cin.ignore(1024,'\n'); } else break; } return input; } void OpenIn(ifstream& in, string prompt) { string infilename; for(;;) { infilename = GetStr(prompt); in.open(infilename.c_str()); if(!in.is_open()) { cout << "\nThat file could not be opened. Please try again.\n"; in.clear(); } else break; } } void OpenOut(ofstream& out, string prompt) { string outfilename; for(;;) { outfilename = GetStr(prompt); out.open(outfilename.c_str()); if(!out.is_open()) { cout << "\nThat file could not be opened. Please try again.\n"; out.clear(); } else break; } } void GetInv(ifstream& in, vector& data) { assert(in.is_open()); int idnum, instock; string name; double price; int i; // counter for(;;) { in >> idnum >> name >> price >> instock; if(in.fail()) break; Inventory add(idnum, name, price, instock); data.push_back(Inventory(0,"",0,0)); // place-holder // start at last data-containing item of the vector. If the product number of // data[i] is larger than that of add, add goes before it, so bump it back to // make space. stop when we get to the place where add belongs. for(i = data.size() - 2; i >= 0; i--) { if(data[i].ProdNum() > idnum) data[i+1] = data[i]; else break; } // put add in the proper place data[i+1] = add; } in.clear(); in.ignore(1024); } void DispInv(ostream& out, vector& data) { for(int i = 0; i < data.size(); i++) { out << "\nProduct Number: " << data[i].ProdNum() << "\nProduct Name: " << data[i].Name() << "\nUnit Price: $" << data[i].Price() << "\nNumber in Stock: " << data[i].NumStocked() << endl; } } void DispInv(ostream& out, Inventory data) { out << "\nProduct Number: " << data.ProdNum() << "\nProduct Name: " << data.Name() << "\nUnit Price: $" << data.Price() << "\nNumber in Stock: " << data.NumStocked() << endl; } Inventory* Find(vector& data, int number) { Inventory* found; for(int i = 0; i < data.size(); i++) { if(data[i].ProdNum() == number) { found = &(data[i]); break; } else if(data[i].ProdNum() > number) { found = NULL; break; } } return found; }