about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/3/C++/automobile.cc
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT2.2/labwork/3/C++/automobile.cc')
-rw-r--r--usth/ICT2.2/labwork/3/C++/automobile.cc56
1 files changed, 56 insertions, 0 deletions
diff --git a/usth/ICT2.2/labwork/3/C++/automobile.cc b/usth/ICT2.2/labwork/3/C++/automobile.cc
new file mode 100644
index 0000000..d274a60
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/automobile.cc
@@ -0,0 +1,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);
+}