功能模块设计
基于C++语言实现的个人财务管理系统,采用面向对象的思维进行设计,包含以下功能模块:
- 添加收支记录
- 删除收支记录
- 修改收支记录
- 查询收支记录
- 个人收支记录一览表
- 清空系统数据
主要运行界面
系统完整代码
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
| #include <iostream> #include <algorithm> #include <string> #include <iomanip> #include <fstream> #include <vector> #include <sstream> #include <ctime> using namespace std;
class Record { public: string num; string date; double amount; string type; string remarks; Record() { } Record(string num, string date, string type, double amount, string remarks) { this->num = num; this->date = date; this->type = type; this->amount = amount; this->remarks = remarks; } void showRecordInfo() { cout << left << setw(12) << num; cout << left << setw(20) << date; cout << left << setw(16) << type; cout << left << setw(16) << amount; cout << left << setw(20) << remarks << endl; } static void showHeader() { cout << left << setw(12) << "序号"; cout << left << setw(20) << "日期"; cout << left << setw(16) << "类型"; cout << left << setw(16) << "金额"; cout << left << setw(20) << "备注" << endl; } };
class FinanceSystem { public: vector<Record> rs; double money; FinanceSystem() { } FinanceSystem(vector<Record> rs, double money) { this->rs = rs; this->money = money; } ~FinanceSystem() { } void init(double money) { this->money = money; readFile(); } void menu() { string sel = "0"; system("cls"); cout << "\t\t\t*********欢迎来到个人财务管理系统***********" << endl; cout << "\t\t\t|------------------------------------------|" << endl; cout << "\t\t\t| 1 添加收支记录 |" << endl; cout << "\t\t\t|------------------------------------------|" << endl; cout << "\t\t\t| 2 删除收支记录 |" << endl; cout << "\t\t\t|------------------------------------------|" << endl; cout << "\t\t\t| 3 修改收支记录 |" << endl; cout << "\t\t\t|------------------------------------------|" << endl; cout << "\t\t\t| 4 查询收支记录 |" << endl; cout << "\t\t\t|------------------------------------------|" << endl; cout << "\t\t\t| 5 个人收支记录一览表 |" << endl; cout << "\t\t\t|------------------------------------------|" << endl; cout << "\t\t\t| 6 清空系统数据 |" << endl; cout << "\t\t\t|------------------------------------------|" << endl; cout << "\t\t\t| 0 退出 |" << endl; cout << "\t\t\t|------------------------------------------|" << endl; cout << "\t\t\t请选择【0-6】:"; cin >> sel; while(sel != "0" && sel != "1" && sel != "2" && sel != "3" && sel != "4" && sel != "5" && sel != "6") { cout << "\t\t\t输入不合法,请重新选择【0-6】:"; cin >> sel; } if(sel == "1") { this->insertRs(); this->menu(); } else if(sel == "2") { this->deleteRs(); this->menu(); } else if(sel == "3") { this->updateRs(); this->menu(); } else if(sel == "4") { this->selectRs(); this->menu(); } else if(sel == "5") { this->displayRs(); this->menu(); } else if(sel == "6") { this->clearSystem(); this->menu(); } else if(sel == "0") { exit(0); } }
void readFile() { ifstream ifs; ifs.open("records.txt", ios::in); int n = 0; ifs >> n; for(int i = 0; i < n; i++) { Record r; ifs >> r.num >> r.date >> r.amount >> r.type >> r.remarks; rs.push_back(r); } ifs.close(); } void writeFile() { ofstream ofs; ofs.open("records.txt", ios::out); ofs << rs.size() << endl; for (int i = 0; i < rs.size(); i++) { ofs << rs[i].num << " " << rs[i].date << " " << rs[i].amount << " " << rs[i].type << " " << rs[i].remarks << endl; } ofs.close(); } void traverseRs() { cout << "\t--------------------------------------------------------------------------------------" << endl; cout<<"\t"; Record::showHeader(); cout << "\t--------------------------------------------------------------------------------------" << endl; for (int i = 0; i < rs.size(); i++) { cout << "\t"; rs[i].showRecordInfo(); } cout << "\t--------------------------------------------------------------------------------------" << endl; }
void insertRs() { while(true) { system("cls"); cout << "\t***************************欢迎来到添加收支记录信息功能*******************************" << endl; cout << "\t个人收支记录一览表"<<endl; this->traverseRs(); string sel = "0"; cout << "\t-----------------" << endl; cout << "\t1 添加收支记录" << endl; cout << "\t2 返回主菜单" << endl; cout << "\t-----------------" << endl; cout << "\t请进行选择【1-2】:"; cin >> sel; while(sel != "1" && sel != "2") { cout << "\t输入不合法,请重新选择【1-2】:"; cin >> sel; } if(sel == "1") { string flag = "1"; while (flag == "1") { cout << "\t开始信息录入:"<<endl; Record r; stringstream ss; ss << rs.size() + 1 ; r.num = ss.str(); cout << "\t日期:"; cin >> r.date; cout << "\t类型(支出 or 收入):"; cin >> r.type; cout << "\t金额:"; cin >> r.amount; cout << "\t备注:"; cin >> r.remarks; rs.push_back(r); writeFile(); cout << "\n\t收支记录信息添加成功!是否继续添加?(1 是 0 否)" << endl; cout << "\t请进行选择【0-1】:"; cin >> flag; while(flag != "0" && flag != "1") { cout << "\t输入不合法,请重新选择【0-1】:"; cin >> flag; } } cout << "\t"; system("pause"); } else { break; } } } void deleteRs() { while (true) { system("cls"); cout << "\t***************************欢迎来到删除收支记录信息功能*******************************" << endl; cout << "\t个人收支记录一览表"<<endl; this->traverseRs(); string sel = "0"; cout << "\t--------------------" << endl; cout << "\t1 按收支记录序号删除" << endl; cout << "\t2 返回主菜单" << endl; cout << "\t--------------------" << endl; cout << "\t注意:删除完成后会自动更新所有记录的序号!" << endl; cout << "\t请进行选择【1-2】:"; cin >> sel; while(sel != "1" && sel != "2") { cout << "\t输入不合法,请重新选择【1-2】:"; cin >> sel; } if (sel == "1") { string keyNum; bool flag = false; cout << "\t请输入待删除的收支记录序号:"; cin >> keyNum; for (vector<Record>::iterator it = rs.begin(); it != rs.end(); ++it) { if (it->num == keyNum) { flag = true; cout<< "\n\t序号为" << keyNum << "的收支记录如下:"<<endl; cout << "\t--------------------------------------------------------------------------------------" << endl; cout<<"\t"; Record::showHeader(); cout << "\t--------------------------------------------------------------------------------------" << endl; cout<<"\t"; it->showRecordInfo(); cout << "\t--------------------------------------------------------------------------------------" << endl; cout << "\t是否确认删除该条收支记录?(1 是 0 否)" << endl; cout << "\t请进行选择【0-1】:"; string ch = "0"; cin >> ch; while(ch != "0" && ch != "1") { cout << "\t输入不合法,请重新选择【0-1】:"; cin >> ch; } if (ch == "0") break; else { rs.erase(it); for(int j = 0; j < rs.size(); j++) { stringstream ss; ss << j + 1; rs[j].num = ss.str(); } writeFile(); cout << "\t删除成功!" << endl; break; } } } if (!flag) cout << "\t无法查询到此条收支记录信息,无法删除!\n" << endl; cout << "\t"; system("pause"); } else { break; } } }
void updateRs() { while(true) { system("cls"); cout << "\t***************************欢迎来到修改收支记录信息功能*******************************" << endl; cout << "\t个人收支记录一览表"<<endl; this->traverseRs(); string sel = "0"; cout << "\t---------------------------" << endl; cout << "\t1 按序号修改收支记录信息" << endl; cout << "\t2 返回主菜单" << endl; cout << "\t---------------------------" << endl; cout << "\t请进行选择【1-2】:"; cin >> sel; while(sel != "1" && sel != "2") { cout << "\t输入不合法,请重新选择【1-2】:"; cin >> sel; } if(sel == "1") { bool flag = false; string keyNum; cout << "\t请输入待修改收支记录的序号:"; cin >> keyNum; for (int i = 0; i < rs.size(); i++) { if (rs[i].num == keyNum) { flag = true; cout << "\t待修改收支记录当前信息如下:" << endl; cout << "\t--------------------------------------------------------------------------------------" << endl; cout<<"\t"; Record::showHeader(); cout << "\t--------------------------------------------------------------------------------------" << endl; cout<<"\t"; rs[i].showRecordInfo(); cout << "\t--------------------------------------------------------------------------------------" << endl; Record r = rs[i]; cout << "\t请输入修改后的日期:"; cin >> r.date; cout << "\t请输入修改后的类型:"; cin >> r.type; cout << "\t请输入修改后的金额:"; cin >> r.amount; cout << "\t请输入修改后的备注:"; cin >> r.remarks; cout << "\t是否确认修改?(1 是 0 否)" << endl; cout << "\t请进行选择【0-1】:"; string ch = "0"; cin >> ch; while(ch != "0" && ch != "1") { cout << "\t输入不合法,请重新选择【0-1】:"; cin >> ch; }
if (ch == "0") break; else { rs[i] = r; cout << "\t修改成功!" << endl; writeFile(); break; } } } if (!flag) cout << "\t无法查询到此条收支记录信息,无法修改!\n" << endl; cout<<"\t"; system("pause"); } else { break; } } }
void selectRs() { while (true) { system("cls"); cout << "\t***************************欢迎来到查询收支记录信息功能*******************************" << endl; cout << "\t----------------------------" << endl; cout << "\t1 按类型查询收支记录信息" << endl; cout << "\t2 返回主菜单" << endl; cout << "\t----------------------------" << endl; cout << "\t请进行选择【1-2】:"; string sel = "0"; cin >> sel; while(sel != "1" && sel != "2") { cout << "\t输入不合法,请重新选择【1-2】:"; cin >> sel; } if (sel == "1") { string keyType; bool flag = false; cout << "\t请输入待查询收支记录的类型:"; cin >> keyType; cout << "\t查询结果如下:" << endl; cout << "\t--------------------------------------------------------------------------------------" << endl; cout<<"\t"; Record::showHeader(); cout << "\t--------------------------------------------------------------------------------------" << endl; for (int i = 0; i < rs.size(); i++) { if (rs[i].type == keyType) { flag = true; cout<<"\t"; rs[i].showRecordInfo(); } } cout << "\t--------------------------------------------------------------------------------------" << endl; if (!flag) cout << "\t无法查询到此类记录!\n" << endl; cout << "\t"; system("pause"); } else { break; } } }
void displayRs() { system("cls"); cout << "\t***************************欢迎来到个人收支记录一览表功能*****************************" << endl; cout << "\t表1:个人收支记录一览表"<<endl; this->traverseRs(); double balance = money; for(int i = 0; i < rs.size(); i++) { if(rs[i].type == "支出") balance -= rs[i].amount; else balance += rs[i].amount; } cout << "\t本月初始资金:" << money << "元" << endl; cout << "\t当前余额:" << balance << "元" << endl; cout << "\t"; system("pause"); }
void clearSystem() { while (true) { string sel = "0"; system("cls"); cout << "\t******************************欢迎来到清空系统数据功能*******************************" << endl; cout << "\t------------------" << endl; cout << "\t1 确认清空系统数据" << endl; cout << "\t2 返回主菜单" << endl; cout << "\t------------------" << endl; cout << "\t请慎重选择【1-2】:"; cin >> sel; while(sel != "1" && sel != "2") { cout << "\t输入不合法,请重新输入【1-2】:"; cin >> sel; } if (sel == "1") { rs.clear(); cout << "\t清空成功!" << endl; cout << "\t"; system("pause"); writeFile(); } else { break; } } } };
int main() { FinanceSystem fs; fs.init(3000); fs.menu(); return 0; }
|
运行方法
- 新建一个cpp文件,命名为”main.cpp”。
- 将系统完整代码复制粘贴至”main.cpp”中。
- 利用Dev C++运行”main.cpp”即可。