-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOrder.cpp
More file actions
89 lines (75 loc) · 2.25 KB
/
Order.cpp
File metadata and controls
89 lines (75 loc) · 2.25 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
#include "Order.h"
constexpr auto DEFAULT_UNIT_PRICE = 2;
Order::Order()
{
startStation = nullptr;
destinationStation = nullptr;
unitPrice = DEFAULT_UNIT_PRICE;
numOfTickets = 1;
GetTotalPrice();
qDebug() << "[Order]New order is created.";
}
Order::~Order()
{
qDebug() << "Last order deleted.";
}
void Order::SetStartStation(Station* station)
{
startStation = station;
qDebug() << "[Order]Set start: " << station->name;
}
void Order::SetDestinationStation(Station* station)
{
destinationStation = station;
qDebug() << "[Order]Set destination: " << station->name;
}
void Order::SetNumOfTickets(uint num)
{
numOfTickets = num;
qDebug() << "[Order]Set number of tickets: " << num;
}
QMap<QString, uint> fareMap;
void ReadFareData(QString fileName, QMap<QString, uint> &_fareMap)
{
qDebug() << "Loading fare data...";
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << file.errorString();
return;
}
QTextStream in(&file);
QStringList destinationList = in.readLine().split(",");
while (!in.atEnd()) {
QString line = in.readLine();
QStringList tokens = line.split(",");
QString startStationId = tokens[0];
for (qsizetype i = 1; i < tokens.size(); ++i) {
QString destinationStationId = destinationList[i];
uint price = tokens[i].toUInt();
_fareMap.insert(startStationId + destinationStationId, price);
}
}
qDebug() << "Fare data read successfully.";
file.close();
}
void Order::GetUnitPrice(QMap<QString, uint>& _fareMap)
{
if (startStation == nullptr || destinationStation == nullptr) {
qDebug() << "[Order]Error: Start station or destination station is not set.";
return;
}
// 查找价格
QString key = startStation->id + destinationStation->id;
if (_fareMap.contains(key)) {
unitPrice = _fareMap[key];
qDebug() << "[Order]Get unit price: " << unitPrice << "¥| Route: " << key;
} else {
unitPrice = DEFAULT_UNIT_PRICE;
qDebug() << "[Order]Error: No fare data for this route: " << key;
}
}
void Order::GetTotalPrice()
{
totalPrice = unitPrice * numOfTickets;
qDebug() << "[Order]Get total price: " << totalPrice << "¥";
}