PhoneBook.cpp
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* PhoneBook.cpp :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/07/19 18:08:37 by gychoi #+# #+# */ 9 /* Updated: 2023/07/21 19:08:32 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "PhoneBook.h" 14 15 int PhoneBook::_index = 0; 16 17 PhoneBook::PhoneBook(void) 18 { 19 for (int i = 0; i < 8; i++) 20 _contacts[i] = Contact(i); 21 } 22 23 void PhoneBook::PrintExit(void) 24 { 25 std::cout << "EXIT" << std::endl; 26 } 27 28 void PhoneBook::PrintUsage(void) 29 { 30 std::cout << "Please enter a command: ADD, SEARCH, EXIT" << '\n'; 31 } 32 33 void PhoneBook::PrintInvalid(void) 34 { 35 std::cout << "Error: Invalid input" << std::endl; 36 } 37 38 static void convertToUppercase(std::string& str) 39 { 40 size_t length = str.length(); 41 for (size_t i = 0; i < length; i++) 42 str[i] = std::toupper(str[i]); 43 } 44 45 void PhoneBook::UsePhoneBook(void) 46 { 47 std::string input; 48 49 PrintUsage(); 50 while (true) 51 { 52 53 std::cout << "> " << std::flush; 54 std::getline(std::cin, input); 55 if (!std::cin.good()) 56 exit(1); 57 convertToUppercase(input); 58 if (input == "EXIT") 59 { 60 PrintExit(); 61 break; 62 } 63 else if (input == "ADD") 64 { 65 UpdatePhoneBook(); 66 PrintUsage(); 67 } 68 else if (input == "SEARCH") 69 { 70 SearchPhoneBook(); 71 PrintUsage(); 72 } 73 else if (!input.empty()) 74 PrintInvalid(); 75 else 76 continue; 77 } 78 } 79 80 void PhoneBook::UpdatePhoneBook(void) 81 { 82 _contacts[_index].MakeContact(); 83 _index = (_index + 1) % 8; 84 } 85 86 static bool isOneDigitNumber(const std::string& str) 87 { 88 return ((str.length() == 1) && std::isdigit(static_cast<unsigned char>(str[0]))); 89 } 90 91 static void showContactList(Contact* _contacts) 92 { 93 std::cout << "| " << std::setw(10) << "index" << " "; 94 std::cout << "| " << std::setw(10) << "first name" << " "; 95 std::cout << "| " << std::setw(10) << "last name" << " "; 96 std::cout << "| " << std::setw(10) << "nickname" << " |" << std::endl; 97 for (size_t i = 0; i < 8; i++) 98 _contacts[i].DisplayBriefContact(); 99 } 100 101 void PhoneBook::SearchPhoneBook(void) 102 { 103 int index; 104 bool flag; 105 std::string input; 106 107 flag = true; 108 while (flag) 109 { 110 showContactList(_contacts); 111 while (true) 112 { 113 std::cout << "Index: " << std::flush; 114 std::getline(std::cin, input); 115 if (!std::cin.good()) 116 exit(1); 117 if (!input.empty() && isOneDigitNumber(input)) 118 { 119 index = input[0] - '0'; 120 if (0 <= index && index <= 7) 121 { 122 _contacts[index].DisplayContact(); 123 break; 124 } 125 } 126 std::cout << "Error: Invalid index." << std::endl; 127 } 128 input.clear(); 129 while (true) 130 { 131 std::cout << "Search for more contacts? [y/n]" << std::endl; 132 std::getline(std::cin, input); 133 if (!std::cin.good()) 134 exit(1); 135 if (input == "y") 136 break; 137 else if (input == "n") 138 { 139 flag = false; 140 std::cout << std::endl; 141 break; 142 } 143 else 144 continue; 145 } 146 input.clear(); 147 } 148 }