-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.h
304 lines (284 loc) · 8.64 KB
/
class.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#ifndef EXPERIMENTAMS_CLASS_H
#define EXPERIMENTAMS_CLASS_H
class Card // 储存卡的类
{
private:
std::string CardName; // 卡号
std::string CardPassword; // 密码
int CardStatus{}; // 0-未使用 1-使用中 2-已注销
time_t CreatTime{}; // 创建时间
time_t DeleteTime{}; // 注销时间
time_t LastTime{}; // 上次使用时间
double TotalUse{}; // 总消费额
int UseCount{}; // 消费次数
double Balance{}; // 当前余额
bool IsSettled{}; // 是否结清
public:
/* 构造函数 */
Card() { ; } // 默认构造函数
Card(const std::string &name, const std::string &password, const double &money) // 构造函数
{
CardName = name;
CardPassword = password;
CardStatus = 0;
CreatTime = time(nullptr);
DeleteTime = 0;
LastTime = CreatTime;
TotalUse = 0;
UseCount = 0;
Balance = money;
IsSettled = true;
}
/* 内联成员函数 */
std::string Name() { return CardName; } // 获取卡名
int Status() const { return CardStatus; } // 获取卡状态
double GetBalance() const { return Balance; } // 获取余额
int SettleStat() const { return IsSettled; } // 获取结清状态
void Login() { CardStatus = 1; } // 更改卡状态为上机
void Logoff() // 更改卡状态为下机(更新使用时间、使用次数)
{
CardStatus = 0;
LastTime = time(nullptr);
UseCount++;
}
void Paid() { IsSettled = true; } // 更改卡为已结清
void UnPaid() { IsSettled = false; } // 更改卡为未结清
void Update(double value) { Balance += value; } // 更新余额
void Delete() // 注销卡
{
CardStatus = 2;
DeleteTime = time(nullptr);
}
void SetPassword(const std::string &password) { CardPassword = password; } // 更新密码
/* 一般成员函数 */
void Print(bool= false); // 直接输出Card的信息,若为管理员则同时输出密码
void PrintOneline(); // 单行输出Card的信息
bool CheckPassword(const std::string &); // 比对账号密码用于上机登录
bool Charge(double); // 收费
/* 重载运算符 */
friend std::ostream &operator<<(std::ostream &, const Card &);
friend std::istream &operator>>(std::istream &, Card &);
};
class Bill // 储存账单的类
{
private:
std::string CardName; // 卡号
time_t StartTime{}; // 上机时间
time_t EndTime{}; // 下机时间
double Fare{}; // 金额
int Type{}; // 计费种类
bool IsPaid{}; // 是否结清
public:
/* 构造函数 */
Bill() { ; } // 默认构造函数
Bill(const std::string &name, const int &type) // 构造函数-上机账单
{
CardName = name;
StartTime = time(nullptr);
EndTime = 0;
IsPaid = false;
Fare = 0;
Type = type;
}
Bill(const std::string &name, const double &value) // 构造函数-充退账单
{
CardName = name;
StartTime = time(nullptr);
EndTime = StartTime;
IsPaid = true;
Fare = value;
Type = -1;
}
/* 内联成员函数 */
void SetEnd(time_t t) { EndTime = t; } // 设置结束日期
void SetPaid() { IsPaid = true; } // 设置结清
void SetFare(double f) { Fare = f; } // 设置费用
std::string GetName() { return CardName; } // 获取卡号
bool Paid() const { return IsPaid; } // 获取是否结清
int GetType() const { return Type; } // 获取种类
time_t GetStart() const { return StartTime; } // 获取开始时间
time_t GetEnd() const { return EndTime; } // 获取结束时间
double GetFare() const { return Fare; } // 获取费用
/* 一般成员函数 */
void Print() const; // 输出Bill的信息
/* 重载运算符 */
friend std::ostream &operator<<(std::ostream &, const Bill &);
friend std::istream &operator>>(std::istream &, Bill &);
};
class Price // 储存计费的类
{
private:
double PricePerHalfHour;
double PriceWholeNight;
double PriceWholeDay;
std::pair<int, int> NightRange;
public:
/* 构造函数 */
Price(double P_HalfHour = 2.0, double P_Night = 27.0, double P_Day = 48.0, int NightStart = 22,
int NightEnd = 7) // 构造函数
{
PricePerHalfHour = P_HalfHour;
PriceWholeNight = P_Night;
PriceWholeDay = P_Day;
NightRange.first = NightStart;
NightRange.second = NightEnd;
}
/* 内联成员函数 */
std::pair<int, int> GetNightRange() { return NightRange; } // 获取包夜范围
/* 一般成员函数 */
void Print() const; // 输出价格
double CalcFare(const time_t &, const time_t &, const int &) const; // 计算费用 0-半小时计费 1-包夜 2-包天
/* 重载运算符 */
friend std::ostream &operator<<(std::ostream &, const Price &);
friend std::istream &operator>>(std::istream &, Price &);
};
struct YearMonthDay // 年月日结构体
{
int Year, Month, Day;
/* 构造函数 */
YearMonthDay()
{
time_t t = time(nullptr);
tm *t_tm = localtime(&t);
Year = t_tm->tm_year + 1900;
Month = t_tm->tm_mon + 1;
Day = t_tm->tm_mday;
}
explicit YearMonthDay(time_t t)
{
tm *t_tm = localtime(&t);
Year = t_tm->tm_year + 1900;
Month = t_tm->tm_mon + 1;
Day = t_tm->tm_mday;
}
YearMonthDay(int y, int m, int d)
{
Year = y;
Month = m;
Day = d;
}
/* 重载运算符 */
bool operator<(const YearMonthDay &t) const
{
if (Year != t.Year)
return Year < t.Year;
else if (Month != t.Month)
return Month < t.Month;
else if (Day != t.Day)
return Day < t.Day;
else
return false;
}
bool operator<=(const YearMonthDay &t) const
{
if (Year != t.Year)
return Year < t.Year;
else if (Month != t.Month)
return Month < t.Month;
else
return Day <= t.Day;
}
bool operator==(const YearMonthDay &t) const
{
if (Year == t.Year && Month == t.Month && Day == t.Day)
return true;
else
return false;
}
// friend std::ostream &operator<<(std::ostream &, const YearMonthDay &);
};
class Stat // 储存统计信息的类
{
private:
YearMonthDay Date; // 日期
int CreatCardCount; // 创建卡次数
int DeleteCardCount; // 注销卡次数
int DepositCount; // 储值次数
int RefundCount; // 退费次数
int ConsumeCount; // 消费次数
double DepositAmount; // 储值量
double RefundAmount; // 退费量
double ConsumeAmount; // 消费量
public:
/* 构造函数 */
Stat()
{
Date = {0, 0, 0};
CreatCardCount = 0;
DeleteCardCount = 0;
DepositCount = 0;
RefundCount = 0;
ConsumeCount = 0;
DepositAmount = 0.0;
RefundAmount = 0.0;
ConsumeAmount = 0.0;
}
explicit Stat(const YearMonthDay &val)
{
Date = val;
CreatCardCount = 0;
DeleteCardCount = 0;
DepositCount = 0;
RefundCount = 0;
ConsumeCount = 0;
DepositAmount = 0.0;
RefundAmount = 0.0;
ConsumeAmount = 0.0;
}
/* 内联成员函数 */
YearMonthDay GetDate() { return Date; }
void SetDate(const YearMonthDay &val) { Date = val; }
void CreatCard(const double &val)
{
CreatCardCount++;
Deposit(val);
}
void DeleteCard(const double &val)
{
DeleteCardCount++;
Refund(val);
}
void Deposit(const double &val)
{
DepositCount++;
DepositAmount += val;
}
void Refund(const double &val)
{
RefundCount++;
RefundAmount += val;
}
void Consume(const double &val)
{
ConsumeCount++;
ConsumeAmount += val;
}
// int cntCreatCard() { return CreatCardCount; }
// int cntDeleteCard() { return DeleteCardCount; }
// int cntDeposit() { return DepositCount; }
// int cntRefund() { return RefundCount; }
// int cntConsume() { return ConsumeCount; }
// double valDeposit() { return DepositAmount; }
// double valRefund() { return RefundAmount; }
// double valConsume() { return ConsumeAmount; }
/* 一般成员函数 */
void Print(); // 输出
/* 重载运算符 */
friend std::ostream &operator<<(std::ostream &, const Stat &);
friend std::istream &operator>>(std::istream &, Stat &);
Stat operator+(const Stat &a) const
{
Stat tmp;
tmp.CreatCardCount = CreatCardCount + a.CreatCardCount;
tmp.DeleteCardCount = DeleteCardCount + a.DeleteCardCount;
tmp.DepositCount = DepositCount + a.DepositCount;
tmp.RefundCount = RefundCount + a.RefundCount;
tmp.ConsumeCount = ConsumeCount + a.ConsumeCount;
tmp.DepositAmount = DepositAmount + a.DepositAmount;
tmp.RefundAmount = RefundAmount + a.RefundAmount;
tmp.ConsumeAmount = ConsumeAmount + a.ConsumeAmount;
return tmp;
}
Stat operator/(const int &a) const;
};
#endif //EXPERIMENTAMS_CLASS_H