#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>

using namespace std;
using std::ifstream;
using std::setprecision;
using std::string;

enum RequestType { ZERO = 1, CREDIT, DEBIT, END, totalRequests };
string strMenuOpts[totalRequests];

void	Init			();
int		GetRequest		();
bool	ShouldDisplay	(int RequestType, double Balance);
void	OutputLine		(int Acc, const string Name, double Balance);

int main()
{
	int			Request		= ZERO;
	int			Account		= ZERO;
	char		Name[32]	= {0};
	double		Balance		= 0.0;
	
	Init();

	ifstream	inClient("Client.dat", ios::in);

	if(!inClient)	cout << "Failed to open Client.dat\n";
	else
	{
		while(Request != END)
		{
			Request = GetRequest();
			if(Request != END)
			{
				system("cls");
				cout << "Accounts with ";
				switch(Request)
				{
				case ZERO:		cout << "Zero";		break;
				case CREDIT:	cout << "Credit";	break;
				case DEBIT:		cout << "Debit";	break;
				case END:
				default:							break;
				}
				cout << " Balance:\n";
				while(!inClient.eof())
				{
					inClient >> Account >> Name >> Balance;
					if(Account != -1)
					{
						if(ShouldDisplay(Request, Balance))	OutputLine(Account, Name, Balance);
						sprintf(Name, "");
						Account = -1;
						Balance = 0.0;
					}
				}
				inClient.clear();
				inClient.seekg(0);
				cout << "Press any button to continue.\n";
				getch();
			}
		}
	}
	inClient.close();
	cout << "File Closed.\n\n";
	system("pause");

	return 0;
}
void Init()
{
	strMenuOpts[ZERO]	= "List Accounts with Zero Balance";
	strMenuOpts[CREDIT]	= "List Accounts with Credit Balance";
	strMenuOpts[DEBIT]	= "List Accounts with Debit Balance";
	strMenuOpts[END]	= "End Program";
}
int GetRequest()
{
	int Local = 0;
	do
	{
		system("cls");
		cout << "Enter Request from list:\n";
		for(int i = ZERO; i < totalRequests; i++)
			cout << setw(2) << i << ") " << strMenuOpts[i] << "\n";

		cout << "\n";
		cin >> Local;
	} while(Local < ZERO || Local >= totalRequests);
	return Local;
}
bool ShouldDisplay(int RequestType, double Balance)
{
	switch(RequestType)
	{
	case ZERO:		if(Balance == 0.0)	return true; break;
	case CREDIT:	if(Balance < 0.0)	return true; break;
	case DEBIT:		if(Balance > 0.0)	return true; break;
	}
	return false;
}
void OutputLine(int Acc, const string Name, double Bal)
{
	cout << left
		 << setw(10)	<< Acc
		 << setw(13)	<< Name
		 << setw(7)		<< Bal
		 << endl;
}