Advertisement
Guest User

Untitled

a guest
Dec 19th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.52 KB | None | 0 0
  1. /*
  2.  * Address Book ALPHA
  3.  */
  4. #include<iostream>
  5. #include<string>
  6. #include"Record.h"
  7. using namespace std;
  8.  
  9. // menu variable
  10. string mainChoice;
  11. string subChoice;
  12.  
  13. // input date varible
  14. string Name;
  15. string Email;
  16. int Phonenumber;
  17.  
  18. // search varible
  19. string searchName;
  20. string searchEmail;
  21. int searchPhonenumber;
  22. bool successfulSearch;
  23.  
  24. // record varible
  25. string totalCreate = 0;
  26. int totalRecords = 0;
  27. Record arrayRecords[100];
  28.  
  29. int main() {
  30.     while (mainChoice[0] != '5') {
  31.         // main menu OK
  32.         cout << endl;
  33.         cout << "***********MAIN MENU*************" << endl;
  34.         cout << "1. Initialize the address book" << endl;
  35.         cout << "2. Create person contact information" << endl;
  36.         cout << "3. Lookup person contact information" << endl;
  37.         cout << "4. Lookup all person contact information" << endl;
  38.         cout << "5. Quit" << endl;
  39.         cout << endl;
  40.  
  41.         mainChoice = '0';
  42.         cout << "Please enter your chioce (1, 2, 3, 4 or 5 to quit): " << endl;
  43.         getline(cin, mainChoice);
  44.  
  45.         switch (mainChoice[0]) {
  46.    
  47.         case '1': // 1. Initialize the address book OK
  48.            
  49.             // delete all date OK
  50.             totalRecords = 0;
  51.             for (int i = 0; i < 100; i++) {
  52.                 arrayRecords[i].setName("");
  53.                 arrayRecords[i].setEmail("");
  54.                 arrayRecords[i].setPhonenumber(NULL);
  55.             }
  56.  
  57.             // output name list OK
  58.             // input date OK
  59.             cout << "Name is : Lawrence Cheung" << endl
  60.                 << "Email is: enccl@eie.polyu.edu.hk" << endl
  61.                 << "Telephone number is: 27666131" << endl;
  62.  
  63.                 arrayRecords[0].setName("Lawrence Cheung");
  64.                 arrayRecords[0].setEmail("enccl@eie.polyu.edu.hk");
  65.                 arrayRecords[0].setPhonenumber(27666131);
  66.  
  67.             cout << "Name is: Helen Wong" << endl
  68.                 << "Email is: helenworng@yahoo.com.hk" << endl
  69.                 << "Telephone number is: 94665888" << endl;
  70.  
  71.             arrayRecords[1].setName("Helen Wong");
  72.             arrayRecords[1].setEmail("helenworng@yahoo.com.hk");
  73.             arrayRecords[1].setPhonenumber(94665888);
  74.  
  75.             cout << "Name is: Simon Sui" << endl
  76.                 << "Email is: ss123@gmail.com" << endl
  77.                 << "Telephone number is: 64441234" << endl;
  78.  
  79.             arrayRecords[2].setName("Simon Sui");
  80.             arrayRecords[2].setEmail("ss123@gmail.com");
  81.             arrayRecords[2].setPhonenumber(64441234);
  82.  
  83.             cout << "Name is: Mary Ho" << endl
  84.                 << "Email is: ho.mary10@netvigator.com" << endl
  85.                 << "Telephone number is: 21111112" << endl;
  86.  
  87.             arrayRecords[3].setName("Mary Ho");
  88.             arrayRecords[3].setEmail("ho.mary10@netvigator.com");
  89.             arrayRecords[3].setPhonenumber(21111112);
  90.  
  91.             totalRecords = 4;
  92.             cout << "Initialzation is completed." << endl;
  93.             break;
  94.  
  95.         case '2': // 2. Create person contact information          
  96.             cout << "Please enter the total number of records to be created: " << endl;
  97.             getline(cin, totalCreate);
  98.  
  99.             //int test = atoi(totalCreate.c_str());
  100.  
  101.             for (int i = 0;; i++) {
  102.                 // totalCreate checking loop OK
  103.                 if (atoi(totalCreate.c_str()) <= 0 || atoi(totalCreate.c_str()) > 10
  104.                     || totalCreate.find_first_not_of("0123456789", 0) < totalCreate.length()) {
  105.  
  106.                     cout << "The input is invalid. It must be and integer not greater than 10." << endl;
  107.                     cout << "Please enter the total number of records to be created: ";
  108.                     getline(cin, totalCreate);
  109.                 }
  110.                 else {
  111.                     break;
  112.                 }
  113.             }
  114.            
  115.  
  116.             for (int i = totalRecords; i < (totalRecords + atoi(totalCreate.c_str())); i++) {
  117.                 // name input OK
  118.                 cout << "Name is: ";
  119.                 getline(cin, Name);
  120.                 getline(cin, Name);
  121.  
  122.                 for (int j = 0;; j++) {
  123.                     // name checking loop
  124.                     if (Name.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ", 0) < Name.length()) {
  125.                         cout << "The input is invalid. Only space characters and letters are allowed." << endl;
  126.                         cout << "Name is: ";
  127.                         getline(cin, Name);
  128.                     }
  129.                     else {
  130.                         break;
  131.                     }
  132.                 }
  133.  
  134.                 arrayRecords[i].setName(Name);
  135.  
  136.                 // email input OK
  137.                 cout << "Email is: ";
  138.                 cin >> Email;
  139.  
  140.                 for (int j = 0;; j++) {
  141.                     // email checking loop
  142.                     if (Email.find("@", 0) == 0 || Email.find("@", 0) == Email.length() - 1 || Email.find("@", 0) >= Email.length() ) {
  143.                         cout << "The input is invalid. One character '@' must be found and there must be some characters before and after the character '@'." << endl;
  144.                         cout << "Email is: ";
  145.                         cin >> Email;  
  146.                     }
  147.                     else {
  148.                         break;
  149.                     }
  150.                 }
  151.  
  152.                 arrayRecords[i].setEmail(Email);
  153.  
  154.                 // telephone number input OK
  155.                 cout << "Telephone number is: ";
  156.                 cin >> Phonenumber;
  157.  
  158.                 for (int j = 0;; j++) {
  159.                     // telephone number checking loop OK
  160.                     if (!cin || Phonenumber - 10000000 <= 0 || Phonenumber > 99999999) {
  161.                         cin.clear();
  162.                         cin.ignore(numeric_limits < streamsize > ::max(), '\n');
  163.                         cout << "The input is invalid. It must be an 8-digit integer and the first digit cannot be zero." << endl;
  164.                         cout << "Telephone number is: ";
  165.                         cin >> Phonenumber;
  166.                     }
  167.                     else {
  168.                         break;
  169.                     }
  170.                 }
  171.  
  172.                 arrayRecords[i].setPhonenumber(Phonenumber);
  173.             }
  174.            
  175.             totalRecords += atoi(totalCreate.c_str());
  176.             cout << "All new records are saved!" << endl;
  177.             break;
  178.  
  179.         case '3': // 3. Lookup person contact information          
  180.             subChoice = '0';
  181.             while (subChoice[0] != 'd') {
  182.                 // sub-menu OK
  183.                 cout << endl;
  184.                 cout << "***********SUB-MENU*************" << endl;
  185.                 cout << "a. Search record by name" << endl;
  186.                 cout << "b. Search record by email address" << endl;
  187.                 cout << "c. Search record by telephone number" << endl;
  188.                 cout << "d. Return to main menu" << endl;
  189.                 cout << endl;
  190.  
  191.                 cout << "Please enter your chioce (a, b, c, or d to return): " << endl;
  192.                 getline(cin, subChoice);
  193.  
  194.                 switch (subChoice[0]) {
  195.                
  196.                 case 'a': // a. Search record by name                  
  197.                     cout << "Please enter a name to search: " << endl;
  198.                     getline(cin, searchName);
  199.                    
  200.                     for (int j = 0;; j++) {
  201.                         // name checking loop
  202.                         if (searchName.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ", 0) < searchName.length()) {
  203.                             cout << "The input is invalid. Only space characters and letters are allowed." << endl;
  204.                             cout << "Please enter a name to search : " << endl;;
  205.                             getline(cin, searchName);
  206.                         }
  207.                         else {
  208.                             break;
  209.                         }
  210.                     }
  211.  
  212.                     // search name
  213.                     successfulSearch = false;
  214.                     for (int i = 0; i < totalRecords; i++) {
  215.                         if (arrayRecords[i].getName() == searchName) {
  216.                             cout << endl << "A record is found!" << endl;
  217.                             cout << "Name is: " << arrayRecords[i].getName() << endl
  218.                                 << "Eamil is: " << arrayRecords[i].getEmail() << endl
  219.                                 << "Telephone number is: " << arrayRecords[i].getPhonenumber() << endl;
  220.                             successfulSearch = true;
  221.                         }
  222.                     }
  223.                     if (successfulSearch != true) {
  224.                         cout << endl << "Sorry, no record is found!" << endl;
  225.                     }
  226.                     break;
  227.  
  228.                 case 'b': // b. Search record by email address                 
  229.                     cout << "Please enter an email address to search: " << endl;
  230.                     cin >> searchEmail;
  231.  
  232.                     for (int j = 0;; j++) {
  233.                         // email checking loop
  234.                         if (searchEmail.find("@", 0) == 0 || searchEmail.find("@", 0) == searchEmail.length() - 1 || searchEmail.find("@", 0) >= searchEmail.length()) {
  235.                             cout << "The input is invalid. One character '@' must be found and there must be some characters before and after teh character '@'." << endl;
  236.                             cout << "Please enter an email address to search: " << endl;
  237.                             cin >> searchEmail;
  238.                         }
  239.                         else {
  240.                             break;
  241.                         }
  242.                     }
  243.  
  244.                     // search email
  245.                     successfulSearch = false;
  246.                     for (int i = 0; i < totalRecords; i++) {
  247.                         if (arrayRecords[i].getEmail() == searchEmail) {
  248.                             cout << endl << "A record is found!" << endl;
  249.                             cout << "Name is: " << arrayRecords[i].getName() << endl
  250.                                 << "Eamil is: " << arrayRecords[i].getEmail() << endl
  251.                                 << "Telephone number is: " << arrayRecords[i].getPhonenumber() << endl;
  252.                             successfulSearch = true;
  253.                         }
  254.                     }
  255.                     if (successfulSearch != true) {
  256.                         cout << endl << "Sorry, no record is found!" << endl;
  257.                     }
  258.                     break;
  259.  
  260.                 case 'c': // c. Search record by telephone number                  
  261.                     cout << "Please enter a telephone number to search: " << endl;
  262.                     cin >> searchPhonenumber;
  263.  
  264.                     // search phone number
  265.                     successfulSearch = false;
  266.                     for (int i = 0; i < totalRecords; i++) {
  267.                         if (arrayRecords[i].getPhonenumber() == searchPhonenumber) {
  268.                             cout << endl << "A record is found!" << endl;
  269.                             cout << "Name is: " << arrayRecords[i].getName() << endl
  270.                                 << "Eamil is: " << arrayRecords[i].getEmail() << endl
  271.                                 << "Telephone number is: " << arrayRecords[i].getPhonenumber() << endl;
  272.                             successfulSearch = true;
  273.                         }
  274.                     }
  275.                     if (successfulSearch != true) {
  276.                         cout << endl << "Sorry, no record is found!" << endl;
  277.                     }
  278.                     break;
  279.  
  280.                 case 'd': // d.Return to main menu                 
  281.                     break;
  282.  
  283.                 default: // error message OK                   
  284.                     cout << endl << "Invalid input. Please enter again!" << endl;
  285.                     break;
  286.                 }
  287.            
  288.             }
  289.             break;
  290.  
  291.         case '4': // 4. Lookup all person contact information OK           
  292.             for (int i = 0; i < totalRecords; i++) {
  293.                 cout << "Name is: " << arrayRecords[i].getName() << endl
  294.                     << "Eamil is: " << arrayRecords[i].getEmail() << endl
  295.                     << "Telephone number is: " << arrayRecords[i].getPhonenumber() << endl;
  296.             }
  297.             break;
  298.  
  299.         case '5': //5. Quit OK         
  300.             cout << "Goodbye!" << endl;
  301.             break;
  302.  
  303.         default: // error message OK           
  304.             cout << endl << "Invalid input. Please enter again!" << endl;
  305.             cout << "main choice 0 is: " << mainChoice[0] << endl;
  306.             break;
  307.         }
  308.     }
  309.  
  310.     return 0;
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement