Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author - Nfinity [Aman]
- // System - One
- // Date - 11/13/2018 {mm/dd/yyyy}
- #include <iostream>
- #include <string.h>
- #include <stdlib.h>
- #include <algorithm>
- using namespace std;
- class Contact{
- private:
- string name;
- string number;
- int numberId;
- int isSet = 0;
- public:
- void setContact(string name,string number,int numberId){
- this->name = name;
- this->number = number;
- this->numberId = numberId;
- cout<<"Contact Saved"<<endl;
- }
- int getIsSet(){
- return this->isSet;
- }
- string getNumber(){
- return this->number;
- }
- string getName(){
- return this->name;
- }
- };
- class PhoneBook{
- private:
- const static int contactListLength = 100;
- Contact contacts[100];
- int numberId = 0;
- public:
- int checkIfSaved(string number){
- int i = 0;
- for(i = 0;i<contactListLength;i++){
- if(contacts[i].getIsSet() == 0){
- break;
- }
- else{
- if(number.compare(contacts[i].getNumber()) == 0){
- return 1;
- }
- else{
- continue;
- }
- }
- }
- return 0;
- }
- void saveContact(){
- string name,number;
- cout<<"Enter Contact Name : ";
- cin>>name;
- cout<<"Enter Contact Number : ";
- cin>>number;
- if(checkIfSaved(number) == 0){
- contacts[numberId].setContact(name,number,numberId);
- this->numberId++;
- pause();
- clearScreen();
- }
- else{
- cout<<"Contact Already Present! With number "<<number<<endl;
- }
- }
- void printAllContacts(){
- clearScreen();
- int i = 0;
- cout<<"Contacts"<<endl;
- printLines();
- for(i=0;i<numberId;i++){
- cout<<"Name : "<<contacts[i].getName()<<endl;
- cout<<"Number : "<<contacts[i].getNumber()<<endl;
- }
- printLines();
- pause();
- clearScreen();
- }
- void printContactWithIndex(int index){
- cout<<"Name : "<<contacts[index].getName()<<endl;
- cout<<"Number : "<<contacts[index].getNumber()<<endl;
- }
- void searchUsingNumber(){
- clearScreen();
- string number;
- cout<<"Enter atleast starting 3 Numbers to search"<<endl;
- cout<<"Enter Number To Search : ";
- cin>>number;
- printLines();
- int lenghtOfEnteredNumber = number.length();
- int i = 0, flag = 0;
- for(i=0; i < numberId; i++){
- string numberSplited = contacts[i].getNumber().substr(0,lenghtOfEnteredNumber);
- if(number.compare(numberSplited) == 0){
- printLines();
- printContactWithIndex(i);
- printLines();
- flag++;
- }
- }
- if(flag == 0){
- cout<<"No Match(s) Found!"<<endl;
- }
- pause();
- clearScreen();
- }
- void searchUsingName(){
- clearScreen();
- string name;
- cout<<"Enter atleast starting 3 Character to search"<<endl;
- cout<<"Enter Name To Search : ";
- cin>>name;
- // Convert Name To LowerCase
- name = converToLower(name);
- int lenghtOfEnteredName = name.length();
- int i = 0, flag = 0;
- for(i=0; i < numberId; i++){
- string nameSplitted = converToLower(contacts[i].getName().substr(0,lenghtOfEnteredName));
- if(name.compare(nameSplitted) == 0){
- printLines();
- printContactWithIndex(i);
- printLines();
- flag++;
- }
- }
- if(flag == 0){
- cout<<"No Match(s) Found!"<<endl;
- }
- pause();
- clearScreen();
- }
- string converToLower(string s){
- string data = s;
- transform(data.begin(), data.end(), data.begin(), ::tolower);
- return data;
- }
- void printLines(){
- int i=0,lineCount = 25;
- for(i = 0; i <= lineCount; i++){
- cout<<"=";
- }
- cout<<"\n";
- }
- void printHelp(){
- cout<<"Actions"<<endl;
- cout<<"Press 1 : To Save Contact"<<endl;
- cout<<"Press 2 : To Display Contacts"<<endl;
- cout<<"Press 3 : To Search Using Number"<<endl;
- cout<<"Press 4 : To Search Using Name"<<endl;
- cout<<"Press 99 : To Exit"<<endl;
- actions();
- }
- void actions(){
- int action = 0;
- cout<<"Action : ";
- cin>>action;
- if(action == 1){
- saveContact();
- }
- else if(action == 2){
- printAllContacts();
- }
- else if(action == 3){
- searchUsingNumber();
- }
- else if(action == 4){
- searchUsingName();
- }
- else if(action == 99){
- exit(0);
- }
- else{
- cout<<"Please Enter A Valid Option"<<endl;
- }
- }
- void clearScreen(){
- system("CLS");
- }
- void pause(){
- system("pause");
- }
- PhoneBook(){
- while(1==1){
- printHelp();
- }
- }
- };
- int main(){
- PhoneBook pb;
- }
Add Comment
Please, Sign In to add comment