about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/3/C++/automobile.cc
blob: d274a600f61906211a2b485b9e3eb631e0e13e25 (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
48
49
50
51
52
53
54
55
56
#include <regex>
#include <stdexcept>
#include <string>

#include "automobile.h"

using namespace std;

const regex license_pattern ("[0-9A-Z]+");

void
Automobile::set_license (string s)
{
  smatch m;
  if (!regex_match (s, m, license_pattern))
    throw invalid_argument{"invalid license plate"};
  license = s;
}

void
Automobile::set_fuel (double x)
{
  if (x < 0)
    throw invalid_argument{"negative fuel"};
  fuel = x;
}

void
Automobile::set_speed (double x)
{
  speed = (x < 0) ? 0 : x;
}

Automobile::Automobile (string l, double f, double s)
{
  set_license (l);
  set_fuel (f);
  set_speed (s);
}

void
Automobile::accelerate (double v)
{
  if (v < 0)
    throw invalid_argument{"negative acceleration"};
  if (fuel)
    set_speed (speed + v);
}

void
Automobile::decelerate (double v)
{
  if (v < 0)
    throw invalid_argument{"negative deceleration"};
  set_speed (speed - v);
}