问题描述
:设计并完成一个家庭财务信息管理系统,能够对家庭日常收支和定期储蓄信
息进行管理,实现对信息的增加,删除,查询,保存等操作。
功能设计
:
(1) 日常收支管理
包含增加收支信息和删除收支信息
(2) 定期的储蓄管理
可以进行存定期和取定期的操作,存定期时要输入经办人姓名,金额,存
期,利率等信息。取定期时输入支取的存款序号,如存在,显示该信息并
提示“确认支取”,按 y 确认支取,计算本息。
(3) 收支信息查询
可以进行收入信息、支出信息和收支信息的详细查询,可以按年、月进行
收支信息的查询等
(4) 储蓄信息的查询
(5) 收支信息和储蓄信息写入文件中保存
构造收支类Iocome,以及私有成员和构造重载函数
class Iocome
{
public:
Iocome() {
year = 0;
month = 0;
day = 0;
income = 0;
outcome = 0;
}
Iocome(int& yea, int& mon, int& da, double& inco, double& out) {
year = yea; month = mon; day = da; income = inco; outcome = out;
}
void setyear(int&);//输入年份
void setmonth(int&);//输入月份
void setday(int&);//输入天
void setincome(double&);//输入收入
void setoutcome(double&);//输入支出
int showyear(); //输出年份
int showmonth();//输出月份
int showday();//输出天
double showincome();//输出收人
double showoutcome();//输出支出
void display();//输出所有信息
private:
double income;
double outcome;
int year;
int day;
int month;
};
void Iocome::setyear(int& yea) { year = yea; }
void Iocome::setmonth(int& mon) { month = mon; }
void Iocome::setday(int& da) { day = da; }
void Iocome::setincome(double& in) { income = in; }
void Iocome::setoutcome(double& out) { outcome = out; }
int Iocome::showyear() { return year; }
int Iocome::showmonth() { return month; }
int Iocome::showday() { return day; }
double Iocome::showincome() { return income; }
double Iocome::showoutcome() { return outcome; }
void Iocome::display() {
cout << "时间:" << year << "年" << month << "月" << day << "日";
cout << "\t收入:" << income;
cout << "\t支出:" << outcome << endl;
}
构造储蓄类deposit
class deposit //储蓄管理
{
public:
deposit() {
order = 0; money = 0; time = 0; profit = 0; name = '\0';
}
deposit(string& nam, double& mon, int& ti, float& pro, int& ord) {
name = nam;
money = mon; time = ti; profit = pro;
order = ord;
}
string getname();
double getmoney();
int gettime();
float getprofit();
int getorder();
void putname(string& nam);
void putmoney(double& mon);
void puttime(int& ti);
void putprofit(float& pro);
void putorder(int& ord);
private:
string name;
double money;
int time;
float profit;
int order;
};
string deposit::getname() {
return name;
}
int deposit::gettime() { return time; }
float deposit::getprofit() { return profit; }
double deposit::getmoney() { return money; }
int deposit::getorder() { return order; }
void deposit::putname(string& nam) { name = nam; }
void deposit::puttime(int& ti) { time = ti; }
void deposit::putprofit(float& pro) { profit = pro; }
void deposit::putmoney(double& mon) { money = mon; }
void deposit::putorder(int& ord) { order = ord; }
主函数部分(功能的实现)
上面的无参数的构造函数Iocome()用于在主函数中建造无参数的Iocome类型的指针数组(指针数组的建立很重要!!)
建立指针数组io,指向Iocome类,同理建立deposit类指针数组
指针数组用于保存输入的相关数据
int main() {
cout << "欢迎使用本家庭财务管理系统" << endl;
char choose5;
int i = 0, time; string name; float profit; int w = 1;//w为存蓄的序号
int num = 0;
int choose1, choose2, choose3, choose4, year, month, day; double money, income, outcome;
Iocome* io[100];//建立指针数组io,指向Iocome类
deposit* c[100];//建立指针数组c,指向deposit类
接下来就是相关功能的实现了
1.实现日常收支管理功能:
增加收支信息
cout << "请分别输入增加收支信息的日期(按年月日顺序)\n";
cin >> year >> month >> day;
cout << "输入今日的收入\n";
cin >> income;
cout << "输入今日的支出\n";
cin >> outcome;
io[i] = new Iocome(year, month, day, income, outcome);
cout << "收支信息已保存" << endl;
i++;
break;
删除收支信息(io[k]->其内部函数,用于提取io[k]所指向的数据)
删除后要将被删除的指针io[k]赋空
int year1, month1, day1;
cout << "输入要删除的收支信息的日期:(按年月日顺序)" << endl;
cin >> year1;
cin >> month1;
cin >> day1;
for (int k = 0; k < i; k++) {
if (io[k]->showyear() == year1 && io[k]->showmonth() == month1 && io[k]->showday() == day1)//若满足待删除条件
{
cout << "删除成功";
num++;
delete io[k];
io[k] = NULL; //指针要赋空!!!
break;
}
}
if (num == 0) //num记录是否存在待删除信息
{
cout << "删除信息有误,删除失败\n";
}
break;
}
查询收支信息
int cyear, cmonth;
cout << "输入待查询的年份和月份:\n";
cin >> cyear;
cin >> cmonth;
cout << "该年月所有的收支信息如下:";
for (int k = 0; k < i; k++) {
if (io[k]->showyear() == cyear && io[k]->showmonth() == cmonth)
{
io[k]->display();
}
}
将收支信息存入文件中(txt格式)
{ofstream outfile;
string filepath = "收支信息.txt";
outfile.open(filepath);
if (!outfile) {
cout << "打开文件失败" << endl;
exit(1);
}
outfile << "所有的收支信息如下:\n";
for (int j = 0; j < i; j++)
{
if (io[j] != NULL)//指针非空
outfile << io[j]->showyear() << "年" << io[j]->showyear() << "月" << io[j]->showday() << "日\t" << "支出:" << io[j]->showoutcome() << "元" << "收入:" << io[j]->showincome() << "元" << endl;
}
outfile.close();
cout << "写入成功" << endl;
2.实现定期的储蓄管理系统
存定期
cout << "输入经办人姓名:"; cin >> name;
cout << "输入存入金额:"; cin >> money;
cout << "输入存期:"; cin >> time;
cout << "输入利率:"; cin >> profit;
c[h] = new deposit(name, money, time, profit, w);
cout << "存入成功,存款序号为:" << w;
h++;
w++;
取定期
int order1, n = 0;
cout << "输入存款序号:";
cin >> order1;
for (int m = 0; m < h; m++) {
if (order1 == c[m]->getorder()) //指针数组c中元素c[m]指向对象中成员函数
{
cout << "请核实您的存款信息:\n";
cout << "经办人姓名:" << c[m]->getname() << "\t存入的金额:" << c[m]->getmoney() << "\t存期:" << c[m]->gettime() << "天\t" << "利率" << c[m]->getprofit();
cout << "是否确认支取,如果是请按‘y’";
cin >> choose5;
double salary = c[m]->getmoney() * c[m]->getprofit() + c[m]->getmoney();
if (choose5 == 'y') {
cout << "本息为:" << salary << "\n取出成功\n";
delete c[m];
c[m] != NULL; //指针置空
}n++;
存入文件
ofstream outfile;
string filepath = "储蓄信息.txt";
outfile.open(filepath);
if (!outfile) {
cout << "打开文件失败" << endl;
exit(1);
}
for (int o = 0; o < h; o++)
{
if (c[o] != NULL)//指针非空
outfile << "经办人姓名:" << c[o]->getname() << "\t存入金额:" << c[o]->getmoney() << "\t存期:"
<< c[o]->gettime() << "天" << "\t利率:" << c[o]->getprofit() << "\t存款序号" << c[o]->getorder() << endl;
}
outfile.close();
cout << "存入成功" << endl;
退出系统(须使用exit函数)
case 4: //退出系统
{
cout << "感谢使用本系统";
exit(0);
break; }
default:
cout << "输入有误,请重新输入:\n"; break;
}
至此,整个系统的功能就都已经实现啦!下面附上自己敲的源代码
#include<iostream>
#include<fstream>
using namespace std;
class Iocome
{
public:
Iocome() {
year = 0;
month = 0;
day = 0;
income = 0;
outcome = 0;
}
Iocome(int& yea, int& mon, int& da, double& inco, double& out) {
year = yea; month = mon; day = da; income = inco; outcome = out;
}
void setyear(int&);//输入年份
void setmonth(int&);//输入月份
void setday(int&);//输入天
void setincome(double&);//输入收入
void setoutcome(double&);//输入支出
int showyear(); //输出年份
int showmonth();//输出月份
int showday();//输出天
double showincome();//输出收人
double showoutcome();//输出支出
void display();//输出所有信息
private:
double income;
double outcome;
int year;
int day;
int month;
};
void Iocome::setyear(int& yea) { year = yea; }
void Iocome::setmonth(int& mon) { month = mon; }
void Iocome::setday(int& da) { day = da; }
void Iocome::setincome(double& in) { income = in; }
void Iocome::setoutcome(double& out) { outcome = out; }
int Iocome::showyear() { return year; }
int Iocome::showmonth() { return month; }
int Iocome::showday() { return day; }
double Iocome::showincome() { return income; }
double Iocome::showoutcome() { return outcome; }
void Iocome::display() {
cout << "时间:" << year << "年" << month << "月" << day << "日";
cout << "\t收入:" << income;
cout << "\t支出:" << outcome << endl;
}
class deposit //储蓄管理
{
public:
deposit() {
order = 0; money = 0; time = 0; profit = 0; name = '\0';
}
deposit(string& nam, double& mon, int& ti, float& pro, int& ord) {
name = nam;
money = mon; time = ti; profit = pro;
order = ord;
}
string getname();
double getmoney();
int gettime();
float getprofit();
int getorder();
void putname(string& nam);
void putmoney(double& mon);
void puttime(int& ti);
void putprofit(float& pro);
void putorder(int& ord);
private:
string name;
double money;
int time;
float profit;
int order;
};
string deposit::getname() {
return name;
}
int deposit::gettime() { return time; }
float deposit::getprofit() { return profit; }
double deposit::getmoney() { return money; }
int deposit::getorder() { return order; }
void deposit::putname(string& nam) { name = nam; }
void deposit::puttime(int& ti) { time = ti; }
void deposit::putprofit(float& pro) { profit = pro; }
void deposit::putmoney(double& mon) { money = mon; }
void deposit::putorder(int& ord) { order = ord; }
int main() {
cout << "欢迎使用本家庭财务管理系统" << endl;
char choose5;
int i = 0, time; string name; float profit; int w = 1;//w为存蓄的序号
int num = 0;
int choose1, choose2, choose3, choose4, year, month, day; double money, income, outcome;
Iocome* io[100];//建立指针数组io,指向Iocome类
deposit* c[100];//建立指针数组c,指向deposit类
for (int d = 0;; d++) {
cout << "\n请选择功能\n1.日常收支管理\t2.定期的储蓄管理\t3.退出本系统\n";
cin >> choose1;
if (choose1 != 1 && choose1 != 2 && choose1 != 3) {
cout << "请选择正确的功能!";
}
if (choose1 == 1) //日常收支管理
{
for (i; ; )
{
cout << "1.增加收支信息\t2.删除收支信息\t3.查询收支信息\t4.将收支信息存入文件\t5.退出本系统" << endl;
cin >> choose2;
switch (choose2)
{
case 1://增加收支信息
{
cout << "请分别输入增加收支信息的日期(按年月日顺序)\n";
cin >> year >> month >> day;
cout << "输入今日的收入\n";
cin >> income;
cout << "输入今日的支出\n";
cin >> outcome;
io[i] = new Iocome(year, month, day, income, outcome);
cout << "收支信息已保存" << endl;
i++;
break;
}
case 2://删除收支信息
{
int year1, month1, day1;
cout << "输入要删除的收支信息的日期:(按年月日顺序)" << endl;
cin >> year1;
cin >> month1;
cin >> day1;
for (int k = 0; k < i; k++) {
if (io[k]->showyear() == year1 && io[k]->showmonth() == month1 && io[k]->showday() == day1)//若满足待删除条件
{
cout << "删除成功";
num++;
delete io[k];
io[k] = NULL; //指针要赋空!!!
break;
}
}
if (num == 0) //num记录是否存在待删除信息
{
cout << "删除信息有误,删除失败\n";
}
break;
}
case 3: //查询收支信息
{
cout << "选择查询方式:\n1.查询所有收入信息\t2.查询所有支出信息\t3.查询所有收支信息\t4.按年月进行收支信息查询";
cin >> choose3; int n = 0, m = 0, b = 0; //n记录是否经历循环
if (choose3 == 1) {
cout << "收入信息如下:\n";
for (int j = 0; j < i; j++)
{
if (io[j] != NULL) //指针非空
{
cout << io[j]->showyear() << "年" << io[j]->showyear() << "月" << io[j]->showday() << "日\t" << "收入:" << io[j]->showincome() << "元" << endl;
n++;
}
}
if (n == 0) {
cout << "暂时没有收入信息\n";
}
}
if (choose3 == 2) {
cout << "支出信息如下:\n";
for (int j = 0; j < i; j++)
{
if (io[j] != NULL) //指针非空
{
cout << io[j]->showyear() << "年" << io[j]->showyear() << "月" << io[j]->showday() << "日\t" << "支出:" << io[j]->showoutcome() << "元" << endl;
m++;
}
}
if (m == 0) {
cout << "暂时没有支出信息\n";
}
}
if (choose3 == 3) //查询所有收支信息
{
cout << "收支信息如下" << endl;
for (int j = 0; j < i; j++) {
if (io[j] != NULL) //指针非空
{
io[j]->display(); b++;
}
}
if (b == 0) {
cout << "暂时没有收入信息\n";
}
}
if (choose3 == 4)
{
int cyear, cmonth;
cout << "输入待查询的年份和月份:\n";
cin >> cyear;
cin >> cmonth;
cout << "该年月所有的收支信息如下:";
for (int k = 0; k < i; k++) {
if (io[k]->showyear() == cyear && io[k]->showmonth() == cmonth)
{
io[k]->display();
}
}
}
break; }
case 4:
{ofstream outfile;
string filepath = "收支信息.txt";
outfile.open(filepath);
if (!outfile) {
cout << "打开文件失败" << endl;
exit(1);
}
outfile << "所有的收支信息如下:\n";
for (int j = 0; j < i; j++)
{
if (io[j] != NULL)//指针非空
outfile << io[j]->showyear() << "年" << io[j]->showyear() << "月" << io[j]->showday() << "日\t" << "支出:" << io[j]->showoutcome() << "元" << "收入:" << io[j]->showincome() << "元" << endl;
}
outfile.close();
cout << "写入成功" << endl;
break; }
case 5:
{ cout << "感谢使用本系统";
exit(0);
break;
}
default:
cout << "输入有误,请重新输入:\n"; break;
}
}
delete io;
}
if (choose1 == 2) //定期储蓄管理
{
for (int h = 0;;) {
cout << "请选择功能:\n1.存定期 2.取定期 3.将存蓄信息存入文件 4.退出本系统\n";
cin >> choose4;
switch (choose4) {
case 1: //存定期
{
cout << "输入经办人姓名:"; cin >> name;
cout << "输入存入金额:"; cin >> money;
cout << "输入存期:"; cin >> time;
cout << "输入利率:"; cin >> profit;
c[h] = new deposit(name, money, time, profit, w);
cout << "存入成功,存款序号为:" << w;
h++;
w++;
break; }
case 2: //取定期
{
int order1, n = 0;
cout << "输入存款序号:";
cin >> order1;
for (int m = 0; m < h; m++) {
if (order1 == c[m]->getorder()) //指针数组c中元素c[m]指向对象中成员函数
{
cout << "请核实您的存款信息:\n";
cout << "经办人姓名:" << c[m]->getname() << "\t存入的金额:" << c[m]->getmoney() << "\t存期:" << c[m]->gettime() << "天\t" << "利率" << c[m]->getprofit();
cout << "是否确认支取,如果是请按‘y’";
cin >> choose5;
double salary = c[m]->getmoney() * c[m]->getprofit() + c[m]->getmoney();
if (choose5 == 'y') {
cout << "本息为:" << salary << "\n取出成功\n";
delete c[m];
c[m] != NULL; //指针置空
}n++;
break;
}
} //通过n的大小可以知道是否经历循环,从而判断输入是否都正确
if (n == 0) {
cout << "存款序号有误,请重新输入:\n";
}
break; }
case 3: //储蓄信息存入文件
{
ofstream outfile;
string filepath = "储蓄信息.txt";
outfile.open(filepath);
if (!outfile) {
cout << "打开文件失败" << endl;
exit(1);
}
for (int o = 0; o < h; o++)
{
if (c[o] != NULL)//指针非空
outfile << "经办人姓名:" << c[o]->getname() << "\t存入金额:" << c[o]->getmoney() << "\t存期:"
<< c[o]->gettime() << "天" << "\t利率:" << c[o]->getprofit() << "\t存款序号" << c[o]->getorder() << endl;
}
outfile.close();
cout << "存入成功" << endl;
break; }
case 4: //退出系统
{
cout << "感谢使用本系统";
exit(0);
break; }
default:
cout << "输入有误,请重新输入:\n"; break;
}
}
}
if (choose1 == 3) {
cout << "感谢使用本系统";
exit(0);
}
}
return 0;
}
初学C++时候的编的程序,代码量有些费眼睛,还请见谅。
都看到这里了,何不点个赞再宠幸下一位?(狗头)