-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.cpp
More file actions
100 lines (93 loc) · 2.48 KB
/
Driver.cpp
File metadata and controls
100 lines (93 loc) · 2.48 KB
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
#include "stdafx.h"
#include "Driver.h"
#include "DriverManager.h"
Driver::Driver(const std::string& name, std::shared_ptr<CarFactory> factory, std::shared_ptr<class DriverManager> manager, uint32_t id, std::shared_ptr<std::mutex> mut)
: factory_(factory)
, name_(name)
, manager_(manager)
, id_(id)
, whatCar_(WhatCar::NoCar)
, mutex_(mut)
{
std::unique_ptr<Car> carToSale_ = nullptr;
std::unique_ptr<Car> car_ = nullptr;
}
std::string Driver::GetName()
{
return name_;
}
void Driver::BuyCar(const std::string& color)
{
if (car_ != nullptr)
{
carToSale_ = std::move(car_);
}
car_ = factory_->BuildCar(color);
}
WhatCar* Driver::GetWhatCar()
{
return &whatCar_;
}
std::unique_ptr<Car> Driver::SellCar()
{
return std::unique_ptr<Car>(carToSale_.release());
}
void Driver::BuyUsedCar(std::shared_ptr<Driver> driver)
{
if (driver != NULL)
{
car_ = std::move(driver->SellCar());
car_->BecomeBY();
}
}
void Driver::Start()
{
uint32_t timeToByBY = 5000;
uint32_t timeToByNew = 10000;
uint32_t timeToByNew2 = 15000;
uint32_t start = clock();
COORD coordinates;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
coordinates.X = 0;
while (true)
{
uint32_t end = clock();
uint32_t curTimeWork = end - start;
if ((curTimeWork) > timeToByBY && (curTimeWork) < timeToByNew && whatCar_ == WhatCar::NoCar)
{
std::shared_ptr<Driver> driver = manager_->WhoIsReadyToSale();
if (driver != NULL)
{
this->BuyUsedCar(driver);
whatCar_ = WhatCar::BY;
}
}
else if ((curTimeWork) > timeToByNew && whatCar_ != WhatCar::New)
{
this->BuyCar("green");
whatCar_ = WhatCar::New;
}
else if ((curTimeWork) > timeToByNew2 && whatCar_ == WhatCar::New)
{
this->BuyCar("green");
}
if (car_ != nullptr)
{
mutex_->lock();
coordinates.Y = id_;
SetConsoleCursorPosition(hConsole, coordinates);
std::cout << name_ << " I have a car ";
car_->Drive();
mutex_->unlock();
}
else
{
mutex_->lock();
coordinates.Y = id_;
SetConsoleCursorPosition(hConsole, coordinates);
std::cout << name_ << ": I'll go on foot\n";
mutex_->unlock();
}
Sleep(1000);
}
}