目标要求:
一、系统功能简介:个人财务收支管理系统可以将日常收入、支出流水保存起来,随时更新、查询收支记录及收支平衡情况。
二、课程设计要求:
1、采用面向对象的方法进行系统分析与设计,利用 UML 类图绘制分析设计结果。
2、按照时间顺序保存收支记录,所有收支记录保存在磁盘文件当中,系统开始运行时从
磁盘读取数据,退出系统时自动将所有数据写入磁盘文件。
3、系统对用户录入的数据具有一定的容错能力,例如,输入的日期为 2022 年 2 月 29 日,
提醒用户重新录入时间。
4、系统有自动提醒功能:当实时收支平衡数额为负数时,发出超支警示;当实时收支平
衡数额达到预期目标时,提示实现财务自由。
5、系统主菜单样例:
1-添加收入记录
2-添加支出记录
3-查看起止日期内的收入记录
4-查看起止日期内的支出记录
5-查看实时收支平衡情况
6-按照日期查找收支记录
7-按照日期修改收支记录
8-按照日期删除收支记录
9-统计起止日期内的各类别收支情况
0-退出系统
程序设计思路
定义三个类,分别命名为Date,Fee,Manager,Date类里面含有年月日成员变量以及重载的判断时间的>、<、=、≥、≤;Fee类里面含有时间、类型、金额成员变量;Manager类里面含有各种功能的实现,每一次修改数据后都把数据存放到文件中,同时新建一个功能10,以用于目标的设定
代码展示
Date.h
#pragma once
#pragma once
#include<iostream>
#include<Windows.h>
using namespace std;
class Date
{
private:
int year;
int month;
int day;
public:
Date(int year, int month, int day) :
year(year), month(month), day(day) {}
int getyear() {
return year;
}
int getmonth() {
return month;
}
int getday() {
return day;
}
bool operator == (Date b) {
if (year == b.year && month == b.month && day == b.day)
return true;
else
return false;
}
bool operator < (Date a) {
if (year > a.year)
return false;
else if (year < a.year)
return true;
else { //同年
if (month > a.month)
return false;
else if (month < a.month)
return true;
else //同月
return day < a.day;
}
}
bool operator <= (Date a) {
if (year > a.year)
return false;
else if (year < a.year)
return true;
else { //同年
if (month > a.month)
return false;
else if (month < a.month)
return true;
else //同月
return day <= a.day;
}
}
bool operator >= (Date a) {
if (year < a.year)
return false;
else if (year > a.year)
return true;
else { //同年
if (month < a.month)
return false;
else if (month > a.month)
return true;
else //同月
return day >= a.day;
}
}
};
Fee.h
#pragma once
#pragma once
#include<iostream>
#include"Date.h"
using namespace std;
class Fee {
public:
Fee(double amount, int type, Date date) :
amount(amount), type(type), date(date) { };
Date getdate() {
return date;
}
int gettype() {
return type;
}
double getamount() {
return amount;
}
void showdate() {
cout << date.getyear() << "年 " << date.getmonth() << "月 " << date.getday() << "日 ";
}
private:
double amount;
int type;
Date date;
};
Manager.h
#pragma once
#include<iostream>
#include"Date.h"
#include"Fee.h"
#include<vector>
using namespace std;
class Manager {
public:
Manager(vector<Fee> income, vector<Fee> expense, double balance, double target) :
income(income),expense(expense),balance(balance),target(target){ }
void addIrecord(vector<Fee> income, double balance);
void addErecord(vector<Fee> expense, double balance);
void checkIrecord(vector<Fee>income);
void checkErecord(vector<Fee>expense);
void judgeamount(vector<Fee>income, vector<Fee>expense, double target);
void showstatistics(vector<Fee> income, vector<Fee> expense);
void searchrecord(vector<Fee> income, vector<Fee> expense);
void editrecord(vector<Fee> income, vector<Fee> expense);
void deleterecord(vector<Fee> income, vector<Fee> expense);
void settarget();
double getbalance() {
return balance;
}
double gettarget() {
return target;
}
private:
vector<Fee> income;
vector<Fee> expense;
double balance;
double target;
};
Manager.cpp
#include<iostream>
#include"Manager.h"
#include<vector>
#include"Date.h"
#include"Fee.h"
#include<algorithm>
#include<sstream>
#include<fstream>
using namespace std;
bool judge(int year, int month, int day) {
SYSTEMTIME st;
GetLocalTime(&st);
if (year > st.wYear || year < 1995) {
cout << "年份输入错误,请重新输入!" << endl;
return false;
}
if (year == st.wYear) {
if (month > st.wMonth || month < 1) {
cout << "月份输入错误,请重新输入!" << endl;
return false;
}
if (month == st.wMonth) {
if (day > st.wDay || day < 1) {
cout << "日期输入错误,请重新输入!" << endl;
return false;
}
}
}
if (month > 12 || month < 1) {
cout << "月份输入错误,请重新输入!" << endl;
return false;
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
if (day > 31 || day < 1) {
cout << "时间输入错误,请重新输入!" << endl;
return false;
}
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30 || day < 1) {
cout << "时间输入错误,请重新输入!" << endl;
return false;
}
}
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
if (day > 29 || day < 1) {
cout << "时间输入错误,请重新输入!" << endl;
return false;
}
}
else {
if (day > 28 || day < 1) {
cout << "时间输入错误,请重新输入!" << endl;
return false;
}
}
}
return true;
}
bool comp(Fee fee1, Fee fee2) {
return fee1.getdate() < fee2.getdate();
}
void reserve(vector<Fee> fee, string filename) {
ofstream os;
sort(fee.begin(), fee.end(), comp);
os.open(filename, ios::trunc);
os.close();
os.open(filename, ios::out | ios::app);
for (vector<Fee>::iterator it = fee.begin(); it != fee.end(); it++) {
os << it->gettype() << " ";
os << it->getdate().getyear() << " " << it->getdate().getmonth() << " " << it->getdate().getday();
os << " " << it->getamount() << endl;
}
os.close();
}
bool printIrecord(vector<Fee> income, Date date) {
bool flag = false;
for (auto it = income.begin(); it != income.end(); it++) {
if (date == it->getdate()) {
switch (it->gettype()) {
case 1:
cout << "工资收入 ";
break;
case 2:
cout << "理财收入 ";
break;
case 3:
cout << "副业收入 ";
break;
case 4:
cout << "福利收入 ";
break;
case 5:
cout << "其他收入 ";
break;
}
cout << " " << it->getamount() << endl;
flag = true;
}
}
if (flag == false)
cout << "无\n" << endl;
return flag;
}
bool printErecord(vector<Fee> expense, Date date) {
bool flag = false;
for (auto it = expense.begin(); it != expense.end(); it++) {
if (date == it->getdate()) {
switch (it->gettype()) {
case 1:
cout << "生活支出 ";
break;
case 2:
cout << "娱乐支出 ";
break;
case 3:
cout << "医疗支出 ";
break;
case 4:
cout << "教育支出 ";
break;
case 5:
cout << "其他支出 ";
break;
}
cout << " " << it->getamount() << endl;
flag = true;
}
}
if (flag == false)
cout << "无\n" << endl;
return flag;
}
void Manager::settarget() {
cout << "请制定你的财务目标:";
double target;
cin >> target;
if (target < 0) {
cout << "输入目标非法,请重新输入:";
cin >> target;
}
this->target = target;
}
void Manager::addIrecord(vector<Fee> income, double balance) {
int temp;
int adate[3];
double a;
bool flag = false;
addI1:
cout << "请输入收入的类型\n"
"1-工资收入\n"
"2-理财收入\n"
"3-副业收入\n"
"4-福利收入\n"
"5-其他收入\n"
"请输入你要输入的数字:" << endl;
cin >> temp;
if (temp != 1 && temp != 2 && temp != 3 && temp != 4 && temp != 5) {
cout << "输入不合法,请重新输入!\n" << endl;
goto addI1;
}
addI2:
cout << "请输入收入的金额:" << endl;
cin >> a;
if (a <= 0) {
cout << "输入不合法,请重新输入!\n" << endl;
goto addI2;
}
while (flag == false)
{
cout << "请输入收入这笔钱的时间(依次为年月日,中间用空格隔开):";
cin >> adate[0] >> adate[1] >> adate[2];
flag = judge(adate[0], adate[1], adate[2]);
}
Date date(adate[0], adate[1], adate[2]);
Fee fee(a, temp, date);
income.push_back(fee);
reserve(income, "Idata.txt");
cout << "添加成功!" << endl;
judgeamount(income, expense, target);
}
void Manager::addErecord(vector<Fee> expense, double balance) {
int temp;
int adate[3];
double a;
bool flag = false;
addE1:
cout << "请输入支出的类型\n"
"1-生活支出\n"
"2-娱乐支出\n"
"3-医疗支出\n"
"4-教育支出\n"
"5-其他支出\n"
"请输入你要输入的数字:" << endl;
cin >> temp;
if (temp != 1 && temp != 2 && temp != 3 && temp != 4 && temp != 5) {
cout << "输入不合法,请重新输入!\n" << endl;
goto addE1;
}
addE2:
cout << "请输入支出的金额:" << endl;
cin >> a;
if (a <= 0) {
cout << "输入不合法,请重新输入!\n" << endl;
goto addE2;
}
while (flag == false)
{
cout << "请输入支出这笔钱的时间(依次为年月日,中间用空格隔开):";
cin >> adate[0] >> adate[1] >> adate[2];
flag = judge(adate[0], adate[1], adate[2]);
}
Date date(adate[0], adate[1], adate[2]);
Fee fee(a, temp, date);
expense.push_back(fee);
reserve(expense, "Edata.txt");
cout << "添加成功!" << endl;
judgeamount(income, expense, target);
}
void Manager::judgeamount(vector<Fee>income, vector<Fee>expense, double target) {
cout << "此时的财务余额为:";
double balance = 0;
for (auto fee = income.begin(); fee != income.end(); fee++) {
balance += fee->getamount();
}
for (auto fee = expense.begin(); fee != expense.end(); fee++) {
balance -= fee->getamount();
}
getbalance();
cout.precision(18);
cout << balance << endl;
//cout << target << endl;
if (balance >= target)
cout << "恭喜!目前您已实现财务自由" << endl;
else if (balance < 0) {
cout << "当前为财务赤字!请注意合理消费" << endl;
}
}
void Manager::checkIrecord(vector<Fee> income) {
int datebegin[3], datend[3];
bool flag1 = false;
bool flag2 = false;
while (1)
{
cout << "请输入你想查找的日期\n";
while (flag1 == false)
{
cout << "开始日期(依次为年月日,中间用空格隔开):";
cin >> datebegin[0] >> datebegin[1] >> datebegin[2];
flag1 = judge(datebegin[0], datebegin[1], datebegin[2]);
}
while (flag2 == false)
{
cout << "结束日期(依次为年月日,中间用空格隔开):";
cin >> datend[0] >> datend[1] >> datend[2];
flag2 = judge(datend[0], datend[1], datend[2]);
}
if (datebegin[0] > datend[0]) {
flag1 = false;
flag2 = false;
cout << "开始日期比结束日期大,请重新输入!" << endl;
}
else if (datebegin[0] == datend[0]) {
if (datebegin[1] > datend[1]) {
flag1 = false;
flag2 = false;
cout << "开始日期比结束日期大,请重新输入!" << endl;
}
else if (datebegin[1] == datend[1]) {
if (datebegin[2] > datend[2]) {
flag1 = false;
flag2 = false;
cout << "开始日期比结束日期大,请重新输入!" << endl;
}
else
break;
}
else
break;
}
else
break;
}
Date dateBegin(datebegin[0], datebegin[1], datebegin[2]);
Date dateEnd(datend[0], datend[1], datend[2]);
flag2 = false;
double sum = 0;
for (auto it = income.begin(); it != income.end(); it++) {
if (it->getdate() >= dateBegin && it->getdate() <= dateEnd) {
sum += it->getamount();
switch (it->gettype()) {
case 1:
cout << "工资收入 ";
break;
case 2:
cout << "理财收入 ";
break;
case 3:
cout << "副业收入 ";
break;
case 4:
cout << "福利收入 ";
break;
case 5:
cout << "其他收入 ";
break;
}
it->showdate();
cout << " ";
cout << it->getamount() << endl;
flag2 = true;
}
}
cout.precision(18);
cout << "该段时间内总收入为:" << sum << endl;
if (flag2 == false)
{
cout << "该时间段内没有收入记录!\n" << endl;
}
}
void Manager::checkErecord(vector<Fee> expense) {
int datebegin[3], datend[3];
bool flag1 = false;
bool flag2 = false;
while (1)
{
cout << "请输入你想查找的日期\n";
while (flag1 == false)
{
cout << "开始日期(依次为年月日,中间用空格隔开):";
cin >> datebegin[0] >> datebegin[1] >> datebegin[2];
flag1 = judge(datebegin[0], datebegin[1], datebegin[2]);
}
while (flag2 == false)
{
cout << "结束日期(依次为年月日,中间用空格隔开):";
cin >> datend[0] >> datend[1] >> datend[2];
flag2 = judge(datend[0], datend[1], datend[2]);
}
if (datebegin[0] > datend[0]) {
flag1 = false;
flag2 = false;
cout << "开始日期比结束日期大,请重新输入!" << endl;
}
else if (datebegin[0] == datend[0]) {
if (datebegin[1] > datend[1]) {
flag1 = false;
flag2 = false;
cout << "开始日期比结束日期大,请重新输入!" << endl;
}
else if (datebegin[1] == datend[1]) {
if (datebegin[2] > datend[2]) {
flag1 = false;
flag2 = false;
cout << "开始日期比结束日期大,请重新输入!" << endl;
}
else
break;
}
else
break;
}
else
break;
}
Date dateBegin(datebegin[0], datebegin[1], datebegin[2]);
Date dateEnd(datend[0], datend[1], datend[2]);
flag2 = false;
double sum = 0;
for (auto it = expense.begin(); it != expense.end(); it++) {
if (it->getdate() >= dateBegin && it->getdate() <= dateEnd) {
sum += it->getamount();
switch (it->gettype()) {
case 1:
cout << "生活支出 ";
break;
case 2:
cout << "娱乐支出 ";
break;
case 3:
cout << "医疗支出 ";
break;
case 4:
cout << "教育支出 ";
break;
case 5:
cout << "其他支出 ";
break;
}
it->showdate();
cout << " ";
cout << it->getamount() << endl;
flag2 = true;
}
}
cout.precision(18);
cout << "该段时间内总支出为:" << sum << endl;
if (flag2 == false)
{
cout << "该时间段内没有支出记录!\n" << endl;
}
}
void Manager::searchrecord(vector<Fee> income, vector<Fee> expense) {
int Sdate[3];
bool flag = false;
while (flag == false)
{
cout << "请输入想要查找的日期:(依次为年月日,中间用空格隔开):";
cin >> Sdate[0] >> Sdate[1] >> Sdate[2];
flag = judge(Sdate[0], Sdate[1], Sdate[2]);
}
Date date(Sdate[0], Sdate[1], Sdate[2]);
cout << "当天的收入记录:" << endl;
flag = printIrecord(income, date);
cout << endl;
cout << "当天的支出记录:" << endl;
flag = printErecord(expense, date);
}
void Manager::deleterecord(vector<Fee> income, vector<Fee> expense) {
int Ddate[3];
int i, type;
double amount;
bool flag = false;
bool flag1 = false;
bool flag2 = false;
while (flag == false)
{
cout << "请输入想要删除的日期:(依次为年月日,中间用空格隔开):" << endl;
cin >> Ddate[0] >> Ddate[1] >> Ddate[2];
flag = judge(Ddate[0], Ddate[1], Ddate[2]);
}
Date date(Ddate[0], Ddate[1], Ddate[2]);
cout << "当天的收入记录:" << endl;
flag1 = printIrecord(income, date);
cout << endl;
cout << "当天的支出记录:" << endl;
flag2 = printErecord(expense, date);
if (flag1 == true || flag2 == true)
{
flag = false;
while (flag == false)
{
cout << "请选择删除的是“收入记录”还是“支出记录”:(1-收入记录,2-支出记录)" << endl;
cin >> i;
if (i == 1) {
Type3:
cout << "请输入要删除的类型\n"
"1-工资收入\n"
"2-理财收入\n"
"3-副业收入\n"
"4-福利收入\n"
"5-其他收入\n"
"请输入你要删除的数字:" << endl;
cin >> type;
if (type != 1 && type != 2 && type != 3 && type != 4 && type != 5) {
cout << "输入不合法,请重新输入!\n" << endl;
goto Type3;
}
cout << "请输入要删除的金额:" << endl;
cin >> amount;
for (vector<Fee>::iterator it = income.begin(); it != income.end(); it++) {
if (date == it->getdate() && type == it->gettype() && amount == it->getamount()) {
it = income.erase(it);
reserve(income, "Idata.txt");
flag = true;
//防止多删
break;
}
}
if (flag == false) {
cout << "输入的记录不存在,请重新输入!" << endl;
}
}
else if (i == 2) {
Type4:
cout << "请输入要删除的类型\n"
"1-生活支出\n"
"2-娱乐支出\n"
"3-医疗支出\n"
"4-教育支出\n"
"5-其他支出\n"
"请输入你要删除的数字:" << endl;
cin >> type;
if (type != 1 && type != 2 && type != 3 && type != 4 && type != 5) {
cout << "输入不合法,请重新输入!\n" << endl;
goto Type4;
}
cout << "请输入要删除的金额:" << endl;
cin >> amount;
for (vector<Fee>::iterator it = expense.begin(); it != expense.end(); it++) {
if (date == it->getdate() && type == it->gettype() && amount == it->getamount()) {
it = expense.erase(it);
reserve(expense, "Edata.txt");
flag = true;
//防止多删
break;
}
}
if (flag == false) {
cout << "输入的记录不存在,请重新输入!" << endl;
}
}
else {
cout << "输入错误,请重新输入!\n" << endl;
flag = false;
}
}
cout << "删除后当天的收入记录:" << endl;
flag1 = printIrecord(income, date);
cout << endl;
cout << "删除后当天的支出记录:" << endl;
flag2 = printErecord(expense, date);
cout << "删除成功!" << endl;
judgeamount(income, expense, target);
}
else {
cout << "无可删除的记录!\n" << endl;
}
}
void Manager::editrecord(vector<Fee> income, vector<Fee> expense) {
int Edate[3];
int i, type, t;
double amount, a;
bool flag = false;
bool flag1 = false;
bool flag2 = false;
while (flag == false)
{
cout << "请输入想要修改的日期:(依次为年月日,中间用空格隔开):\n";
cin >> Edate[0] >> Edate[1] >> Edate[2];
flag = judge(Edate[0], Edate[1], Edate[2]);
}
Date date(Edate[0], Edate[1], Edate[2]);
cout << "当天的收入记录:" << endl;
flag1 = printIrecord(income, date);
cout << endl;
cout << "当天的支出记录:" << endl;
flag2 = printErecord(expense, date);
if (flag1 == true || flag2 == true) {
flag = false;
while (flag == false) {
cout << "请选择修改的是“收入记录”还是“支出记录”:(1-收入记录,2-支出记录)" << endl;
cin >> i;
if (i == 1) {
Type1:
cout << "请输入要修改的类型\n"
"1-工资收入\n"
"2-理财收入\n"
"3-副业收入\n"
"4-福利收入\n"
"5-其他收入\n"
"请输入你要修改的类型: " << endl;
cin >> type;
if (type != 1 && type != 2 && type != 3 && type != 4 && type != 5) {
cout << "输入不合法,请重新输入!\n" << endl;
goto Type1;
}
cout << "请输入要修改的金额:" << endl;
cin >> amount;
for (vector<Fee>::iterator it = income.begin(); it != income.end(); it++) {
if (date == it->getdate() && type == it->gettype() && amount == it->getamount()) {
editI1:
cout << "请输入修改后的类型\n"
"1-工资收入\n"
"2-理财收入\n"
"3-副业收入\n"
"4-福利收入\n"
"5-其他收入\n"
"请输入修改后的数字:" << endl;
cin >> t;
if (t != 1 && t != 2 && t != 3 && t != 4 && t != 5) {
cout << "输入不合法,请重新输入!\n" << endl;
goto editI1;
}
editI2:
cout << "请输入修改后的金额:" << endl;
cin >> a;
if (a <= 0) {
cout << "输入不合法,请重新输入!\n" << endl;
goto editI2;
}
it = income.erase(it);
Fee fee(a, t, date);
income.push_back(fee);
reserve(income, "Idata.txt");
flag = true;
break;
}
}
if (flag == false) {
cout << "输入要修改的记录不存在,请重新输入!\n" << endl;
}
}
else if (i == 2) {
Type2:
cout << "请输入要修改的类型\n"
"1-生活支出\n"
"2-娱乐支出\n"
"3-医疗支出\n"
"4-教育支出\n"
"5-其他支出\n"
"请输入你要修改的数字:" << endl;
cin >> type;
if (type != 1 && type != 2 && type != 3 && type != 4 && type != 5) {
cout << "输入不合法,请重新输入!\n" << endl;
goto Type2;
}
cout << "请输入要修改的金额:" << endl;
cin >> amount;
for (vector<Fee>::iterator it = expense.begin(); it != expense.end(); it++) {
if (date == it->getdate() && type == it->gettype() && amount == it->getamount()) {
editE1:
cout << "请输入修改后的类型\n"
"1-生活支出\n"
"2-娱乐支出\n"
"3-医疗支出\n"
"4-教育支出\n"
"5-其他支出\n"
"请输入修改后的数字:" << endl;
cin >> t;
if (t != 1 && t != 2 && t != 3 && t != 4 && t != 5) {
cout << "输入不合法,请重新输入!\n" << endl;
goto editE1;
}
editE2:
cout << "请输入修改后的金额:" << endl;
cin >> a;
if (a <= 0) {
cout << "输入不合法,请重新输入!\n" << endl;
goto editE2;
}
it = expense.erase(it);
Fee fee(a, t, date);
expense.push_back(fee);
reserve(expense, "Edata.txt");
flag = true;
break;
}
}
if (flag == false) {
cout << "输入要修改的记录不存在,请重新输入!\n" << endl;
}
}
else {
cout << "输入错误,请重新输入!\n" << endl;
flag = false;
}
}
cout << "修改后当天的收入记录:" << endl;
flag1 = printIrecord(income, date);
cout << endl;
cout << "修改后当天的支出记录:" << endl;
flag2 = printErecord(expense, date);
cout << "修改成功!" << endl;
}
else {
cout << "无可修改的记录!\n" << endl;
}
}
void Manager::showstatistics(vector<Fee> income, vector<Fee> expense) {
int startDate[3];
int endDate[3];
bool flag1 = false;
bool flag2 = false;
while (1)
{
cout << "请输入你想查找的日期\n";
while (flag1 == false)
{
cout << "开始日期(依次为年月日,中间用空格隔开):";
cin >> startDate[0] >> startDate[1] >> startDate[2];
flag1 = judge(startDate[0], startDate[1], startDate[2]);
}
while (flag2 == false)
{
cout << "结束日期(依次为年月日,中间用空格隔开):";
cin >> endDate[0] >> endDate[1] >> endDate[2];
flag2 = judge(endDate[0], endDate[1], endDate[2]);
}
if (startDate[0] > endDate[0]) {
flag1 = false;
flag2 = false;
cout << "开始日期比结束日期大,请重新输入!" << endl;
}
else if (startDate[0] == endDate[0]) {
if (startDate[1] > endDate[1]) {
flag1 = false;
flag2 = false;
cout << "开始日期比结束日期大,请重新输入!" << endl;
}
else if (startDate[1] == endDate[1]) {
if (startDate[2] > endDate[2]) {
flag1 = false;
flag2 = false;
cout << "开始日期比结束日期大,请重新输入!" << endl;
}
else
break;
}
else
break;
}
else
break;
}
Date sdate(startDate[0], startDate[1], startDate[2]);
Date edate(endDate[0], endDate[1], endDate[2]);
flag1 = flag2 = false;
cout << "工资收入: " << endl;
for (auto i : income) {
if (i.getdate() >= sdate && edate >= i.getdate() && i.gettype() == 1) {
i.showdate();
cout << " " << i.getamount() << endl;
flag1 = true;
}
}
if (flag1 == false)
cout << "无" << endl;
cout << endl;
cout << "生活支出: " << endl;
for(auto j : expense) {
if (j.getdate() >= sdate && edate >= j.getdate() && j.gettype() == 1) {
j.showdate();
cout << " " << j.getamount() << endl;
flag2 = true;
}
}
if (flag2 == false)
cout << "无" << endl;
cout << endl;
flag1 = flag2 = false;
cout << "理财收入: " << endl;
for (auto i : income) {
if (i.getdate() >= sdate && edate >= i.getdate() && i.gettype() == 2) {
i.showdate();
cout << " " << i.getamount() << endl;
flag1 = true;
}
}
if (flag1 == false)
cout << "无" << endl;
cout << endl;
cout << "娱乐支出: " << endl;
for (auto j : expense) {
if (j.getdate() >= sdate && edate >= j.getdate() && j.gettype() == 2) {
j.showdate();
cout << " " << j.getamount() << endl;
flag2 = true;
}
}
if (flag2 == false)
cout << "无" << endl;
cout << endl;
flag1 = flag2 = false;
cout << "副业收入: " << endl;
for (auto i : income) {
if (i.getdate() >= sdate && edate >= i.getdate() && i.gettype() == 3) {
i.showdate();
cout << " " << i.getamount() << endl;
flag1 = true;
}
}
if (flag1 == false)
cout << "无" << endl;
cout << endl;
cout << "医疗支出: " << endl;
for (auto j : expense) {
if (j.getdate() >= sdate && edate >= j.getdate() && j.gettype() == 3) {
j.showdate();
cout << " " << j.getamount() << endl;
flag2 = true;
}
}
if (flag2 == false)
cout << "无" << endl;
cout << endl;
flag1 = flag2 = false;
cout << "福利收入: " << endl;
for (auto i : income) {
if (i.getdate() >= sdate && edate >= i.getdate() && i.gettype() == 4) {
i.showdate();
cout << " " << i.getamount() << endl;
flag1 = true;
}
}
if (flag1 == false)
cout << "无" << endl;
cout << endl;
cout << "教育支出: " << endl;
for (auto j : expense) {
if (j.getdate() >= sdate && edate >= j.getdate() && j.gettype() == 4) {
j.showdate();
cout << " " << j.getamount() << endl;
flag2 = true;
}
}
if (flag2 == false)
cout << "无" << endl;
cout << endl;
flag1 = flag2 = false;
cout << "其他收入: " << endl;
for (auto i : income) {
if (i.getdate() >= sdate && edate >= i.getdate() && i.gettype() == 5) {
i.showdate();
cout << " " << i.getamount() << endl;
flag1 = true;
}
}
if (flag1 == false)
cout << "无" << endl;
cout << endl;
cout << "其他支出: " << endl;
for (auto j : expense) {
if (j.getdate() >= sdate && edate >= j.getdate() && j.gettype() == 5) {
j.showdate();
cout << " " << j.getamount() << endl;
flag2 = true;
}
}
if (flag2 == false)
cout << "无" << endl;
cout << endl;
}
main.cpp
#include<iostream>
#include"Date.h"
#include"Fee.h"
#include<vector>
#include"Manager.h"
#include<sstream>
#include<fstream>
using namespace std;
int main() {
int i;
double t = 10000;
cout << " 系统为您设定的初始财务目标为10000,如需更改请先制定您的财务目标\n\n";
do {
ifstream in("Idata.txt"); //txt文件与源代码要在一个文件夹内
string line;
int type;
int y, m, d;
double amount;
double balance = 0;
double target = 0;
vector<Fee> income;
if (in) // 有该文件
{
while (getline(in, line)) //从文件中逐行读取
{
istringstream strStream(line);
strStream >> type;
strStream >> y >> m >> d;
Date date(y, m, d);
strStream >> amount;
Fee fee(amount, type, date);
income.push_back(fee);
}
}
else // 没有该文件
{
cout << "no such file" << endl;
}
in.close();
ifstream In("Edata.txt"); //txt文件与源代码要在一个文件夹内
string Line;
int Type;
int Y, M, D;
double Amount;
vector<Fee> expense;
if (In) // 有该文件
{
while (getline(In, Line)) // line中不包括每行的换行符
{
istringstream strStream(Line);
strStream >> Type;
strStream >> Y >> M >> D;
Date date(Y, M, D);
strStream >> Amount;
Fee fee(Amount, Type, date);
expense.push_back(fee);
}
}
else // 没有该文件
{
cout << "no such file" << endl;
}
In.close();
Manager manager(income, expense, balance, t);
cout << " 欢迎使用个人账户管理系统 \n" << endl;
cout << " 输入1 - 添加收入记录 \n";
cout << " 输入2 - 添加支出记录 \n";
cout << " 输入3 - 查看起止日期内的收入记录 \n";
cout << " 输入4 - 查看起止日期内的支出记录 \n";
cout << " 输入5 - 查看实时收支平衡情况 \n";
cout << " 输入6 - 按照日期查找收支记录 \n";
cout << " 输入7 - 按照日期修改收支记录 \n";
cout << " 输入8 - 按照日期删除收支记录 \n";
cout << " 输入9 - 统计起止日期内的各类别收支情况 \n";
cout << " 输入10- 制定财务目标 \n";
cout << " 输入0 - 退出系统 \n\n";
enter://定义局部变量,使用goto函数
cout << "请输入你的选择:" << endl;
cin >> i;
if (i != 1 && i != 2 && i != 3 && i != 4 && i != 5 && i != 6 && i != 7 && i != 8 && i != 9 && i!= 10 && i != 0) {
cout << "输入格式错误,请重新输入\n" << endl;
goto enter;
}
switch (i) {
case 1:
manager.addIrecord(income, balance);
cout << endl;
break;
case 2:
manager.addErecord(expense, balance);
cout << endl;
break;
case 3:
manager.checkIrecord(income);
cout << endl;
break;
case 4:
manager.checkErecord(expense);
cout << endl;
break;
case 5:
manager.judgeamount(income, expense, manager.gettarget());
break;
case 6:
manager.searchrecord(income, expense);
cout << endl;
break;
case 7:
manager.editrecord(income, expense);
cout << endl;
break;
case 8:
manager.deleterecord(income, expense);
cout << endl;
break;
case 9:
manager.showstatistics(income, expense);
cout << endl;
break;
case 10:
cout << "当前目标为:" << manager.gettarget() << endl;
manager.settarget();
t = manager.gettarget();
cout << "修改成功" << endl;
break;
case 0:
exit(0);
}
} while (i != 0);
return 0;
}