Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Address Book ALPHA
- */
- #include<iostream>
- #include<string>
- #include"Record.h"
- using namespace std;
- // menu variable
- string mainChoice;
- string subChoice;
- // input date varible
- string Name;
- string Email;
- int Phonenumber;
- // search varible
- string searchName;
- string searchEmail;
- int searchPhonenumber;
- bool successfulSearch;
- // record varible
- string totalCreate = 0;
- int totalRecords = 0;
- Record arrayRecords[100];
- int main() {
- while (mainChoice[0] != '5') {
- // main menu OK
- cout << endl;
- cout << "***********MAIN MENU*************" << endl;
- cout << "1. Initialize the address book" << endl;
- cout << "2. Create person contact information" << endl;
- cout << "3. Lookup person contact information" << endl;
- cout << "4. Lookup all person contact information" << endl;
- cout << "5. Quit" << endl;
- cout << endl;
- mainChoice = '0';
- cout << "Please enter your chioce (1, 2, 3, 4 or 5 to quit): " << endl;
- getline(cin, mainChoice);
- switch (mainChoice[0]) {
- case '1': // 1. Initialize the address book OK
- // delete all date OK
- totalRecords = 0;
- for (int i = 0; i < 100; i++) {
- arrayRecords[i].setName("");
- arrayRecords[i].setEmail("");
- arrayRecords[i].setPhonenumber(NULL);
- }
- // output name list OK
- // input date OK
- cout << "Name is : Lawrence Cheung" << endl
- << "Email is: enccl@eie.polyu.edu.hk" << endl
- << "Telephone number is: 27666131" << endl;
- arrayRecords[0].setName("Lawrence Cheung");
- arrayRecords[0].setEmail("enccl@eie.polyu.edu.hk");
- arrayRecords[0].setPhonenumber(27666131);
- cout << "Name is: Helen Wong" << endl
- << "Email is: helenworng@yahoo.com.hk" << endl
- << "Telephone number is: 94665888" << endl;
- arrayRecords[1].setName("Helen Wong");
- arrayRecords[1].setEmail("helenworng@yahoo.com.hk");
- arrayRecords[1].setPhonenumber(94665888);
- cout << "Name is: Simon Sui" << endl
- << "Email is: ss123@gmail.com" << endl
- << "Telephone number is: 64441234" << endl;
- arrayRecords[2].setName("Simon Sui");
- arrayRecords[2].setEmail("ss123@gmail.com");
- arrayRecords[2].setPhonenumber(64441234);
- cout << "Name is: Mary Ho" << endl
- << "Email is: ho.mary10@netvigator.com" << endl
- << "Telephone number is: 21111112" << endl;
- arrayRecords[3].setName("Mary Ho");
- arrayRecords[3].setEmail("ho.mary10@netvigator.com");
- arrayRecords[3].setPhonenumber(21111112);
- totalRecords = 4;
- cout << "Initialzation is completed." << endl;
- break;
- case '2': // 2. Create person contact information
- cout << "Please enter the total number of records to be created: " << endl;
- getline(cin, totalCreate);
- //int test = atoi(totalCreate.c_str());
- for (int i = 0;; i++) {
- // totalCreate checking loop OK
- if (atoi(totalCreate.c_str()) <= 0 || atoi(totalCreate.c_str()) > 10
- || totalCreate.find_first_not_of("0123456789", 0) < totalCreate.length()) {
- cout << "The input is invalid. It must be and integer not greater than 10." << endl;
- cout << "Please enter the total number of records to be created: ";
- getline(cin, totalCreate);
- }
- else {
- break;
- }
- }
- for (int i = totalRecords; i < (totalRecords + atoi(totalCreate.c_str())); i++) {
- // name input OK
- cout << "Name is: ";
- getline(cin, Name);
- getline(cin, Name);
- for (int j = 0;; j++) {
- // name checking loop
- if (Name.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ", 0) < Name.length()) {
- cout << "The input is invalid. Only space characters and letters are allowed." << endl;
- cout << "Name is: ";
- getline(cin, Name);
- }
- else {
- break;
- }
- }
- arrayRecords[i].setName(Name);
- // email input OK
- cout << "Email is: ";
- cin >> Email;
- for (int j = 0;; j++) {
- // email checking loop
- if (Email.find("@", 0) == 0 || Email.find("@", 0) == Email.length() - 1 || Email.find("@", 0) >= Email.length() ) {
- cout << "The input is invalid. One character '@' must be found and there must be some characters before and after the character '@'." << endl;
- cout << "Email is: ";
- cin >> Email;
- }
- else {
- break;
- }
- }
- arrayRecords[i].setEmail(Email);
- // telephone number input OK
- cout << "Telephone number is: ";
- cin >> Phonenumber;
- for (int j = 0;; j++) {
- // telephone number checking loop OK
- if (!cin || Phonenumber - 10000000 <= 0 || Phonenumber > 99999999) {
- cin.clear();
- cin.ignore(numeric_limits < streamsize > ::max(), '\n');
- cout << "The input is invalid. It must be an 8-digit integer and the first digit cannot be zero." << endl;
- cout << "Telephone number is: ";
- cin >> Phonenumber;
- }
- else {
- break;
- }
- }
- arrayRecords[i].setPhonenumber(Phonenumber);
- }
- totalRecords += atoi(totalCreate.c_str());
- cout << "All new records are saved!" << endl;
- break;
- case '3': // 3. Lookup person contact information
- subChoice = '0';
- while (subChoice[0] != 'd') {
- // sub-menu OK
- cout << endl;
- cout << "***********SUB-MENU*************" << endl;
- cout << "a. Search record by name" << endl;
- cout << "b. Search record by email address" << endl;
- cout << "c. Search record by telephone number" << endl;
- cout << "d. Return to main menu" << endl;
- cout << endl;
- cout << "Please enter your chioce (a, b, c, or d to return): " << endl;
- getline(cin, subChoice);
- switch (subChoice[0]) {
- case 'a': // a. Search record by name
- cout << "Please enter a name to search: " << endl;
- getline(cin, searchName);
- for (int j = 0;; j++) {
- // name checking loop
- if (searchName.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ", 0) < searchName.length()) {
- cout << "The input is invalid. Only space characters and letters are allowed." << endl;
- cout << "Please enter a name to search : " << endl;;
- getline(cin, searchName);
- }
- else {
- break;
- }
- }
- // search name
- successfulSearch = false;
- for (int i = 0; i < totalRecords; i++) {
- if (arrayRecords[i].getName() == searchName) {
- cout << endl << "A record is found!" << endl;
- cout << "Name is: " << arrayRecords[i].getName() << endl
- << "Eamil is: " << arrayRecords[i].getEmail() << endl
- << "Telephone number is: " << arrayRecords[i].getPhonenumber() << endl;
- successfulSearch = true;
- }
- }
- if (successfulSearch != true) {
- cout << endl << "Sorry, no record is found!" << endl;
- }
- break;
- case 'b': // b. Search record by email address
- cout << "Please enter an email address to search: " << endl;
- cin >> searchEmail;
- for (int j = 0;; j++) {
- // email checking loop
- if (searchEmail.find("@", 0) == 0 || searchEmail.find("@", 0) == searchEmail.length() - 1 || searchEmail.find("@", 0) >= searchEmail.length()) {
- cout << "The input is invalid. One character '@' must be found and there must be some characters before and after teh character '@'." << endl;
- cout << "Please enter an email address to search: " << endl;
- cin >> searchEmail;
- }
- else {
- break;
- }
- }
- // search email
- successfulSearch = false;
- for (int i = 0; i < totalRecords; i++) {
- if (arrayRecords[i].getEmail() == searchEmail) {
- cout << endl << "A record is found!" << endl;
- cout << "Name is: " << arrayRecords[i].getName() << endl
- << "Eamil is: " << arrayRecords[i].getEmail() << endl
- << "Telephone number is: " << arrayRecords[i].getPhonenumber() << endl;
- successfulSearch = true;
- }
- }
- if (successfulSearch != true) {
- cout << endl << "Sorry, no record is found!" << endl;
- }
- break;
- case 'c': // c. Search record by telephone number
- cout << "Please enter a telephone number to search: " << endl;
- cin >> searchPhonenumber;
- // search phone number
- successfulSearch = false;
- for (int i = 0; i < totalRecords; i++) {
- if (arrayRecords[i].getPhonenumber() == searchPhonenumber) {
- cout << endl << "A record is found!" << endl;
- cout << "Name is: " << arrayRecords[i].getName() << endl
- << "Eamil is: " << arrayRecords[i].getEmail() << endl
- << "Telephone number is: " << arrayRecords[i].getPhonenumber() << endl;
- successfulSearch = true;
- }
- }
- if (successfulSearch != true) {
- cout << endl << "Sorry, no record is found!" << endl;
- }
- break;
- case 'd': // d.Return to main menu
- break;
- default: // error message OK
- cout << endl << "Invalid input. Please enter again!" << endl;
- break;
- }
- }
- break;
- case '4': // 4. Lookup all person contact information OK
- for (int i = 0; i < totalRecords; i++) {
- cout << "Name is: " << arrayRecords[i].getName() << endl
- << "Eamil is: " << arrayRecords[i].getEmail() << endl
- << "Telephone number is: " << arrayRecords[i].getPhonenumber() << endl;
- }
- break;
- case '5': //5. Quit OK
- cout << "Goodbye!" << endl;
- break;
- default: // error message OK
- cout << endl << "Invalid input. Please enter again!" << endl;
- cout << "main choice 0 is: " << mainChoice[0] << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement