-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder.cpp
More file actions
70 lines (53 loc) · 1.53 KB
/
Order.cpp
File metadata and controls
70 lines (53 loc) · 1.53 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
#include <climits>
#include "Order.h"
#include "Drone.h"
// Constructors
// Getters
bool Order::operator<(const Order &other) const {
return m_id < other.getId();
}
// Order Processing
void Order::makeOffer(Drone * drone, int cost) {
// A CLAIMED order cannot be made an offer
if(m_state == CLAIMED) {
// TODO throw fitting exception
return;
}
m_state = OFFERED;
m_offers.insert({drone, cost});
}
void Order::accept() {
// When already claimed, this does nothing
if(m_state == CLAIMED) {
return;
}
// Find best offer
int min_cost = INT_MAX;
int optimal_drone_id = -1;
for(auto e : m_offers) {
if(e.second < min_cost) {
optimal_drone_id = e.first->getId();
}
}
// Get correct reference
for(auto e : m_offers) {
if(e.first->getId() == optimal_drone_id) {
// If already Accepted an offer, cancel Accept
m_accepted_drone->cancel();
// Accept Offer
e.first->accept(this);
break;
}
}
}
bool Order::hasOpen(std::vector<Order> &orders) {
for(auto it : orders) {
if(it.m_state == OPEN) {
return true;
}
}
return false;
}
bool Order::isClaimed() const {
return m_state == CLAIMED;
}