/ CPP_Modules / CPP_Module_00 / ex01 / Contact.cpp
Contact.cpp
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   Contact.cpp                                        :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/07/19 18:09:04 by gychoi            #+#    #+#             */
 9  /*   Updated: 2023/07/21 19:08:56 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "Contact.h"
14  
15  Contact::Contact(void) {}
16  
17  Contact::Contact(int index)
18  {
19  	_index = index;
20  }
21  
22  void	Contact::MakeContact(void)
23  {
24  	while (true)
25  	{
26  		std::cout << "First name: " << std::flush;
27  		if (std::getline(std::cin >> std::ws, _firstName) && !_firstName.empty())
28  			break;
29  		if (!std::cin.good())
30  			exit(1);
31  	}
32  	while (true)
33  	{
34  		std::cout << "Last name: " << std::flush;
35  		if (std::getline(std::cin >> std::ws, _lastName) && !_lastName.empty())
36  			break;
37  		if (!std::cin.good())
38  			exit(1);
39  	}
40  	while (true)
41  	{
42  		std::cout << "nickname: " << std::flush;
43  		if (std::getline(std::cin >> std::ws, _nickname) && !_nickname.empty())
44  			break;
45  		if (!std::cin.good())
46  			exit(1);
47  	}
48  	while (true)
49  	{
50  		std::cout << "Phone number: " << std::flush;
51  		if (std::getline(std::cin >> std::ws, _phoneNumber) && !_phoneNumber.empty())
52  			break;
53  		if (!std::cin.good())
54  			exit(1);
55  	}
56  	while (true)
57  	{
58  		std::cout << "Darkest secret: " << std::flush;
59  		if (std::getline(std::cin >> std::ws, _darkestSecret) && !_darkestSecret.empty())
60  			break;
61  		if (!std::cin.good())
62  			exit(1);
63  	}
64  	std::cout << "Contact has been created." << '\n' << std::endl;
65  }
66  
67  static std::string TruncateString(const std::string& str)
68  {
69  	std::string	ret = str;
70  
71  	if (ret.length() > 10)
72  		ret = ret.substr(0, 9) + ".";
73  	return (ret);
74  }
75  
76  void	Contact::DisplayBriefContact(void)
77  {
78  	std::cout << "| " << std::setw(10) << _index << " ";
79  	std::cout << "| " << std::setw(10) << TruncateString(_firstName) << " ";
80  	std::cout << "| " << std::setw(10) << TruncateString(_lastName) << " ";
81  	std::cout << "| " << std::setw(10) << TruncateString(_nickname) << " |" << std::endl;
82  }
83  
84  void	Contact::DisplayContact(void)
85  {
86  	std::cout << "First name: " << _firstName << '\n';
87  	std::cout << "Last name: " << _lastName << '\n';
88  	std::cout << "Nickname: " << _nickname << '\n';
89  	std::cout << "Phone number: " << _phoneNumber << '\n';
90  	std::cout << "Darkest secret: " << _darkestSecret << std::endl;
91  }