aboutsummaryrefslogtreecommitdiff
path: root/cpp00/ex01/Contact.cpp
blob: e8a9c0cffdbe828cdbc086c2019702dadf4ba3c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   Contact.cpp                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/13 09:05:59 by charles           #+#    #+#             */
/*   Updated: 2020/11/09 12:30:12 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "Contact.hpp"

Contact Contact::prompt()
{
    Contact c;

    Contact::promptString("first name: ", c.m_firstName);
    Contact::promptString("last name:  ", c.m_lastName);
    Contact::promptString("nickname:   ", c.m_nickname);
    Contact::promptString("login:      ", c.m_login);

    if (std::cin.good())
        std::cout << "address:" << std::endl;
    Contact::promptInt("|  house number: ", c.m_address.m_houseNum);
    Contact::promptInt("|  post code:    ", c.m_address.m_postCode);
    Contact::promptString("|  street name:  ", c.m_address.m_street);
    Contact::promptString("|  city:         ", c.m_address.m_city);

    Contact::promptString("email:      ", c.m_email);
    Contact::promptString("phone:      ", c.m_phone);

    if (std::cin.good())
        std::cout << "birthday:" << std::endl;
    Contact::promptInt("|  day:           ", c.m_birthday.m_day);
    Contact::promptInt("|  month:         ", c.m_birthday.m_month);
    Contact::promptInt("|  year:          ", c.m_birthday.m_year);

    Contact::promptString("favorite meal:   ", c.m_favMeal);
    Contact::promptString("underware color: ", c.m_underwareColor);
    Contact::promptString("darkest secret:  ", c.m_darkestSecret);
    return c;
}

void Contact::promptString(std::string promptString, std::string &s)
{
    do
    {
        if (!std::cin.good())
        {
            s = "";
            return;
        }
        std::cout << promptString << std::flush;
        std::getline(std::cin, s);
        if (!std::cin.good())
        {
            s = "";
            return;
        }
        if (s.empty())
            std::cout << "Error: input can't be empty" << std::endl;
    }
    while (s.empty());

}

void Contact::promptInt(std::string promptString, int &i)
{
    do
    {
        if (!std::cin.good())
        {
            i = -1;
            return;
        }
        std::cout << promptString << std::flush;
        i = getInt();
        if (!std::cin.good())
        {
            i = -1;
            return;
        }
        if (i == -1)
            std::cout << "Error: Not a valid number, please try again" << std::endl;
    }
    while (i == -1);
}

std::string Contact::trimedName(std::string name)
{
    if (name.length() > 10)
        return name.substr(0, 9) + ".";
    return name;
}

void Contact::preview() const
{
    std::cout << std::setw(10) << trimedName(m_firstName) << "|"
              << std::setw(10) << trimedName(m_lastName)  << "|"
              << std::setw(10) << trimedName(m_nickname);
}

void Contact::put() const
{
    std::cout << "first name:      " << m_firstName << std::endl;
    std::cout << "last name:       " << m_lastName  << std::endl;
    std::cout << "nickname:        " << m_nickname  << std::endl;
    std::cout << "login:           " << m_login     << std::endl;

    std::cout << "address:         "
              << "Street " << m_address.m_street
              << " Nb "    << m_address.m_houseNum
              << " "       << m_address.m_postCode
              << " "       << m_address.m_city
              << std::endl;

    std::cout << "email:           " << m_email << std::endl;
    std::cout << "phone:           " << m_phone << std::endl;

    std::cout << "birthday:        "
              << m_birthday.m_day   << "/"
              << m_birthday.m_month << "/"
              << m_birthday.m_year
              << std::endl;

    std::cout << "favourite meal:  " << m_favMeal        << std::endl;
    std::cout << "underware color: " << m_underwareColor << std::endl;
    std::cout << "dardest secret:  " << m_darkestSecret  << std::endl;
}