about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/3/C++/name-card.cc
blob: b6c075185fd5f75509125e9128281c4016b2fc34 (plain) (blame)
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
#include <regex>
#include <stdexcept>
#include <string>

#include "name-card.h"

using namespace std;

const regex name_pattern ("[ A-Za-z]+");
const regex phone_pattern ("[-0-9]+");
// This should be good enough I guess?
const regex email_pattern ("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");

void
NameCard::set_name (string s)
{
  // I miss Raku so much
  smatch m;
  if (!regex_match (s, m, name_pattern))
    throw invalid_argument{"invalid name"};
  name = s;
}

void
NameCard::set_phone (string s)
{
  smatch m;
  if (!regex_match (s, m, phone_pattern))
    throw invalid_argument{"invalid number"};
  phone = s;
}

void
NameCard::set_email (string s)
{
  smatch m;
  if (!regex_match (s, m, email_pattern))
    throw invalid_argument{"invalid name"};
  email = s;
}

NameCard::NameCard (string n, string p, string e)
{
  set_name (n);
  set_phone (p);
  set_email (e);
}