#pragma warning(disable:4786)
#include "accountmanage.h"
#include "consumeManage.h"
#include "formAppendConsume.h"
#include "validateType.h"
#include "converType.h"
#include "messagebox.h"
#include "trim.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
CFormAppendConsume::CFormAppendConsume()
{
page = 1;
maxpage = 0;
countPage = 0;
}
vector <int> &CFormAppendConsume::IntList()
{
return strInt;
}
void CFormAppendConsume::Load()
{
CConsumeManage &conMgr=CConsumeManage::GetInstance();
CAccountManage &accMgr=CAccountManage::GetInstance();
vector <CConsume> vecCon;
CConsume con;
Clear();
char info[][12]={"DATE: ","Money: ","Peoples: ","Choose: ","Explain: "};
char tips[][40]={"Automatically","[0 -- 1000]","[1 -- 100]","+,-,/","200 bit"};
string str;
string acclist = "";
CDateType date;
double money;
int count;
float bal;
string explain = "";
char id[10];
enum DialogResult result;
PrintTitle("Append Consume");
for(int i=0; i<5; i++)
{
JmpXY(3,8+2*i);
cout.unsetf(ios::left | ios::right);
cout<<setw(11)<<setfill(' ')<<*(info+i);
cout<<setw(21)<<' ';
JmpXY(35,8+2*i);
cout.setf(ios::left);
cout<<setw(25)<<*(tips+i);
}
JmpXY(3,6);
cout<<setw(74)<<setfill('-')<<'-';
JmpXY(3,18);
cout<<setw(74)<<setfill('-')<<'-';
for(i=0; i<11; i++)
{
JmpXY(50,7+i);
cout<<'|';
}
cout.fill(' ');
LoadAccount();
PrintOnePage();
cout.unsetf(ios::left | ios::right);
if(InputDate(date) == 0)
{
return;
}
con.SetDate(date);
if(InputMoney(money) == 0)
{
return;
}
con.SetMoney(money);
if(InputCount(count) == 0)
{
return;
}
bal = (money/count)*(-1);
con.SetCount(count);
do
{
if(InputOneKey(str) == 0)
{
return;
}
if(str == "/")
{
if(count == countPage)
{
for(i=0; i<countPage; i++)
{
acclist += itoa(tmpAcc[i].GetID(),id,10);
acclist += "/";
}
break;
}
else
{
CMessageBox::ShowMessage("Count person is not equal accountList person!!!");
}
}
else
{
if(InputAccountID(count,str,acclist) == 0)
{
return;
}
break;
}
} while (1);
con.SetAccountID(acclist);
if(InputExplain(explain) == 0)
{
return ;
}
con.SetExplain(explain);
result = CMessageBox::ShowMessage("Input info finish! You can choose Y/N ,[Y]:save info,[N]:Not Save and return:",YesNo);
switch(result)
{
case Yes:
if(str == "/")
{
for(i=0; i<countPage; i++)
{
accMgr.ChgAccBalance(tmpAcc[i].GetID(),bal);
}
}
else
{
ConsumeBill(acclist);
for(i=0; i<strInt.size(); i++)
{
accMgr.ChgAccBalance(strInt[i],bal);
}
}
accMgr.SaveAccountData();
conMgr.AppendConsume(con);
conMgr.SaveConsumeData();
ClearXY();
break;
case No:
return;
}
}
void CFormAppendConsume::LoadAccount()
{
JmpXY(52,7);
cout<<"Name";
cout.setf(ios::right);
cout<<setw(20)<<setfill(' ')<<"ID";
CAccountManage &accMgr=CAccountManage::GetInstance();
vector <CAccount> vecAcc;
vecAcc = accMgr.PrintAccountList();
vecAcc = accMgr.PrintAccountList();//返回账户列表
int count = accMgr.GetSize();//获取账户个数
for(int i=0; i<count; i++)
{
if(vecAcc[i].GetRemark() && vecAcc[i].GetBal() < 5.0)
{
tmpAcc.push_back(vecAcc[i]);
countPage++;
}
}
maxpage = (countPage-1) / Row + 1;
JmpXY(52,16);
cout<<"Total:"<<countPage<<" page/maxpage:"<<page<<"/"<<maxpage;
JmpXY(52,17);
cout<<" [Page UP] / [Page Down]";
}
void CFormAppendConsume::ClearPrint()
{
for(int i=0; i<8; i++)//清空局部区域
{
JmpXY(53,8+i);
cout<<setw(23)<<" ";
}
}
void CFormAppendConsume::PrintOnePage()
{
for(int i=(page-1)*Row,j=0; i<countPage && i<page*Row ; i++)//分页打印
{
JmpXY(53,8+j);
cout.unsetf(ios::left | ios::right);
cout<<setw(21)<<setiosflags(ios::left)<<tmpAcc[i].GetName();
cout<<tmpAcc[i].GetID();
j++;
}
}
void CFormAppendConsume::PressKey(char key)
{
switch(key)
{
case PageUp://向上翻页
if(page == 1)
{
CMessageBox::ShowMessage("This page is first page!");
}
else
{
page--;
}
break;
case PageDown://向下翻页
if(page == maxpage)
{
CMessageBox::ShowMessage("This page is last page!");
}
else
{
page++;
}
break;
}
}
int CFormAppendConsume::InputDate(CDateType &date)
{
int _key;
string _date="";
do
{
_date = "";
JmpXY(14,8);
cout<<setw(12)<<' ';
JmpXY(14,8);
_date = date.PrintDate();
_key = GetString(_date,10);
if(_key == Esc)
{
return 0;
}
if(_key == PageUp || _key == PageDown)
{
PressKey(_key);
ClearPrint();
PrintOnePage();
}
else
{
bool i = CValidateType::IsDate(_date);
if(i == false)
{
CMessageBox::ShowMessage("Date type is YYYY-MM-DD!press anykey to continue!");
getch();
ClearXY();
continue;
}
else
{
JmpXY(14,8);
cout<<_date;
date.SetDate(_date);
break;
}
}
} while (1);
return 1;
}
int CFormAppendConsume::InputMoney(double &money)
{
string strMoney;
int _key;
do
{
strMoney = "";
JmpXY(14,10);
cout<<setw(12)<<' ';
JmpXY(14,10);
_key = GetString(strMoney,6);
CTrim::Trim(strMoney);
if(_key == Esc)
{
return 0;
}
if(_key == PageUp || _key == PageDown)
{
PressKey(_key);
ClearPrint();
PrintOnePage();
}
else
{
bool i = CValidateType::IsDouble(strMoney);
if(i == false)
{
CMessageBox::ShowMessage("You must input digit and point!");
getch();
ClearXY();
continue;
}
else
{
JmpXY(14,10);
cout<<setw(10)<<' ';
money = CConverType::StrToDouble(strMoney);
if(money<0 || money>1000)
{
CMessageBox::ShowMessage("Money Range is [0 -- 999.99]!");
getch();
ClearXY();
continue;
}
JmpXY(14,10);
cout.precision(2);
cout<<fixed<<money;
break;
}
}
} while (1);
return 1;
}
int CFormAppendConsume::InputCount(int &_count)
{
int _key;
string strcount;
do
{
strcount = "";
JmpXY(14,12);
cout<<setw(12)<<' ';
JmpXY(14,12);
_key = GetString(strcount,3);
CTrim::Trim(strcount);
if(_key == Esc)
{
return 0;
}
if(_key == PageUp || _key == PageDown)
{
PressKey(_key);
ClearPrint();
PrintOnePage();
}
else
{
bool i = CValidateType::IsDigit(strcount);
if(i == false)
{
CMessageBox::ShowMessage("You must input digit!");
getch();
ClearXY();
continue;
}
else
{
JmpXY(14,12);
cout<<setw(10)<<' ';
_count = CConverType::StrToInt(strcount);
if(_count<1 || _count>100)
{
CMessageBox::ShowMessage("Money Range is [1 -- 100]!");
getch();
ClearXY();
continue;
}
JmpXY(14,12);
cout<<_count;
break;
}
}
} while (1);
return 1;
}
int CFormAppendConsume::InputOneKey(string &str)
{
int _key;
do
{
str = "";
JmpXY(14,14);
cout<<setw(12)<<' ';
JmpXY(14,14);
_key = GetString(str,1);
if(_key == Esc)
{
return 0;
}
if(_key == PageUp || _key == PageDown)
{
PressKey(_key);
ClearPrint();
PrintOnePage();
}
else
{
if(str == "+" || str == "-" || str == "/")
{
break;
}
else
{
CMessageBox::ShowMessage("You only can input '+','-','/'");
getch();
ClearXY();
}
}
} while (1);
return 1;
}
int CFormAppendConsume::InputAccountID(int _count,string str,string &straccountID)
{
int _key;
int judge;
JmpXY(2,19);
cout<<"AccountID: ";
do
{
_key = GetString(straccountID,60);
if(_key == Esc)
{
return 0;
}
if(_key == PageUp || _key == PageDown)
{
PressKey(_key);
ClearPrint();
PrintOnePage();
}
e