// SYSC 2002 Winter 2011 Assignment #2 #include "stdstuff.h" // global constants const int MAXOPT=7, MAXCUST=100; // Hint: make MAXCUST much smaller to test filling the database // "customer" is the struct containing the customer information // it is made up of id (an integer) and total (a double) struct customer{ int id; double total; }; // Complete the functions here. Note that the students can be stored in any order. // add a new customer to the database; returns true if successful, false otherwise // (database full or customer already exists) bool addCustomer(int id, double total, customer db[], int &num) { if(num==MAXCUST) return false; // database is full for(int i=0;imaxtotal) maxtotal=db[i].total; } return maxtotal; } // mainMenu returns an integer representing the menu option chosen // Valid values are from 1 to "MAXOPT" (global constant). int mainMenu(){ int returnVal; for(;;){ cout << endl; cout << "1: Add New Customer" << endl; cout << "2: Remove Existing Customer" << endl; cout << "3: Print Database Information" << endl; cout << "4: Change Existing Customer ID" << endl; cout << "5: Change Existing Customer Total" << endl; cout << "6: Find Best Customer Total" << endl; cout << "7: End Program\n\n"; // always use "max" for ending program cout << "Please enter the desired operation [1-" << MAXOPT << "]: "; cin >> returnVal; //return if valid option, otherwise output error message if(returnVal>=1&&returnVal<=MAXOPT) return returnVal; cout << "Please enter a valid integer option!" << endl; ; } } void testFunction(customer db[], int &num) { bool added = false; int temp =0, id=0; double total=0; //TEST addCustomer() cout << "[TEST 1]: addCustomer()\n"; added = addCustomer(id=5,total=5,db,num); added = addCustomer(id=3,total=3,db,num); added = addCustomer(id=10,total=10,db,num); added = addCustomer(id=7,total=7,db,num); added = addCustomer(id=4,total=4,db,num); added = addCustomer(id=2,total=2,db,num); added = addCustomer(id=12,total=12,db,num); if (added) cout<< "CORRECT: Customers No.: 5,3,10,7,4,2,12 added. " << num << " Customers added. (Should be 7) \n"; else cout<< "INCORRECT: addCustomer() should not have returned false. \n"; if(addCustomer(id=12,total=12,db,num)) cout << "INCORRECT: Customer 12 added. Customer should not be added because already exists.\n"; else cout << "CORRECT: Customer No. 12 could not be added: already in database.\n"; cout << "\nTesting addCustomer() when DB is full: \n"; if(addCustomer(id=6,total=12,db,temp=100)) cout << "INCORRECT: Customer 6 added. Customer should not be added because database is FULL.\n"; else cout << "CORRECT: Customer No. 6 could not be added: database FULL.\n"; //TEST print Customer DB cout << "\n[TEST 2]: printCustomerDatabase()\n"; printCustomerDatabase(db,num); cout << "\n[TEST 3]: printCustomerDatabase() Empty DB. (Should output DB empty)\n"; printCustomerDatabase(db,temp =0); //TEST remove Customer DB cout << "\n[TEST 4]: removeCustomer()\n"; if(removeCustomer(id=7,db,num)) cout << "CORRECT: Customer No. 7 removed. There are now: " << num << " Customers in database. (Should be 6)\n"; else cout << "INCORRECT: Customer 7 could not be removed: did not exist in database. (Customer should exist)\n"; if(removeCustomer(id=20,db,num)) cout << "INCORRECT: Customer 20 removed. There are now: " << num << " Customers in database. (Should not remove: Customer does not exits\n"; else cout << "CORRECT: Customer No. 20 could not be removed: did not exist in database.\n"; cout<< "\nCustomer No.7 should not be in output:\n\n"; printCustomerDatabase(db,num); //TEST change Customer number cout << "\n[TEST 5]: changeCustomerNumber()\n"; if(changeCustomerID(id=4,temp=15, db,num)) cout << "CORRECT: Customer No. 4 successfully updated to 15.\n"; else cout << "INCORRECT: Customer 4 could not be updated: did not exist in database or nummber 15 in use. " << "(Should be able to change.\n"; cout<< "\nCustomer No.4 should not be present, Customer No. 15 is present with total 4:\n\n"; printCustomerDatabase(db,num); if(changeCustomerID(id=90,temp=8, db,num)) cout << "\nINCORRECT: Customer number 90 successfully updated. (Customer should not exist)\n"; else cout << "\nCORRECT: Customer No. 90 could not be updated: did not exist in database.\n"; if(changeCustomerID(id=2,temp=10, db,num)) cout << "INCORRECT: Customer number 2 successfully updated. (Should not be updated because Num already exists)\n"; else cout << "CORRECT: Customer No.2 could not be updated to No. 10: num already in use.\n"; //TEST change Customer total cout << "\n[TEST 6]: changeCustomerTotal()\n"; if(changeCustomerTotal(id=5,total=10, db,num)) cout << "CORRECT: Customer 5 total successfully updated to 10.\n"; else cout << "INCORRECT: Customer 5 could not be updated: did not exist in database. (Should exist)\n"; cout << "\nPrint Customer 5 which should have total = 10\n\n"; printCustomerDatabase(db,num); if(changeCustomerTotal(id=13,total=12, db,num)) cout << "\nINCORRECT: Customer 13 total successfully updated. (Customer should not exist)\n"; else cout << "\nCORRECT: Customer No. 13 could not be updated: did not exist in database.\n"; //TEST get Best Customer total cout << "\n[TEST 7]: getBestCustomerTotal()\n"; double bestTotal = getBestCustomerTotal(db,num); if(bestTotal==-1) cout << "INCORRECT: No best total: database is empty. (Should not be empty)\n"; else if (bestTotal == 12) cout << "CORRECT: Best Customer total is: " << bestTotal << ". (Should be 12) \n"; else cout << "INCORRECT: Best Customer total is: " << bestTotal << ". (Should be 12) \n"; bestTotal = getBestCustomerTotal(db,temp =0); if(bestTotal==-1) cout << "CORRECT: No best total: database is empty.\n"; else cout << "INCORRECT: Best Customer total is: " << bestTotal << ". (Should return -1 no Customers) \n"; } // main program calls the functions most of which you need to write int main(){ int option, numCusts=0; int id, newid; double total, newtotal, maxtotal; // the Customer database (with numCusts customers in it) customer customerinfo[MAXCUST]; // Calling test function testFunction(customerinfo, numCusts); for(;;){ option=mainMenu(); if(option==MAXOPT) break; // end of program switch(option) { case 1: // add new customer cout << "Enter customer id and total: "; cin >> id >> total; if(addCustomer(id,total,customerinfo,numCusts)) cout << "Customer added. There are now: " << numCusts << " customers in database.\n"; else cout << "Customer could not be added: already in database or database full.\n"; break; case 2: // remove existing Customer cout << "Enter customer id: "; cin >> id; if(removeCustomer(id,customerinfo,numCusts)) cout << "Customer removed. There are now: " << numCusts << " Customers in database.\n"; else cout << "Customer could not be removed: did not exist in database.\n"; break; case 3: // print database information, one customer per line, with total to 2 // decimal places printCustomerDatabase(customerinfo,numCusts); break; case 4: // change customer id cout << "Enter existing customer id, and new customer id: "; cin >> id >> newid; if(changeCustomerID(id,newid,customerinfo,numCusts)) cout << "customer id successfully updated.\n"; else cout << "Customer could not be updated: did not exist in database or new id in use.\n"; break; case 5: // change Customer total cout << "Enter customer id, and new Customer total: "; cin >> id >> newtotal; if(changeCustomerTotal(id,newtotal,customerinfo,numCusts)) cout << "Customer total successfully updated.\n"; else cout << "Customer could not be updated: did not exist in database.\n"; break; case 6: // find best customer total maxtotal = getBestCustomerTotal(customerinfo,numCusts); if(maxtotal==-1) cout << "No customers: database is empty.\n"; else cout << "Best customer total is: " << maxtotal << ".\n"; break; default: // should never get here break; } } pause(); return 0; }