#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <sstream>
using namespace std;
//struct lol{
//
//int pass;
//string user;
//
//};
//int main()
//{
//    lol clints[100];
//      cin>>clints[0].pass;
//      cin>>clint1[0].user;
//      int arr[100];
//
//}
string fname,lname,address,telephone,mobile,email,password="",repassword;
string naddress,ntelephone,nmobile,nemail,npassword,nrepassword;
int balancee=0,id=rand();
void firstname()
{
    char ch;
	do 
	{
		cout<<"Please enter your first name"<<endl;
		getline(cin,fname);
		ch=' ';
		for(int i=0 ; i<fname.size() ; i++)
		{
			if(i==fname.size()-1);
			break;
			if(fname[i+1]<'a' || fname[i+1]>'z' || fname[0]<'A' || fname[0]>'Z')
			{
				cout<<"Please enter a valid name."<<endl;
				cout<<"Press y to contniue."<<endl;
				cin>>ch;
				cin.ignore();
				break;
			}
		}
	}while(ch=='y');
}

void lastname()
{
	char ch;
	do
	{
		cout<<"Please enter your last name"<<endl;
		getline(cin,lname);
		ch=' ';
		for(int i=0 ; i<lname.size() ; i++)
		{
			if(i==lname.size()-1);
			break;
			if(lname[i+1]<'a' || lname[i+1]>'z' || lname[0]<'A' || lname[0]>'Z')
			{
				cout<<"Please enter a valid name."<<endl;
				cout<<"Press y to contniue."<<endl;
				cin>>ch;
				cin.ignore();
			}
		}
	}while(ch=='y');
}

void telephonenum()
{
	char ch;
	do
	{
		cout<<"Please enter your telephone number."<<endl;
		getline(cin,telephone);
		ch=' ';
		for(int i=0 ; i<telephone.size() ; i++)
		{
			if(telephone.size()!=8 || telephone[i]<'0' || telephone[i]>'9')
			{
				cout<<"Please enter a valid telephone number."<<endl;
				cout<<"Press y to contniue."<<endl;
				cin>>ch;
				cin.ignore();
				break;
			}
		}
	}while(ch=='y');
}

void mobilenum()
{
	char ch;
	do
	{
		cout<<"Please enter your mobile number."<<endl;
		getline(cin,mobile);
		ch=' ';
		for(int i=0 ; i<mobile.size() ; i++)
		{
			if(mobile.size()!=11 || mobile[i]<'0' || mobile[i]>'9')
			{
				cout<<"Please enter a valid telephone number."<<endl;
				cout<<"Press y to contniue."<<endl;
				cin>>ch;
				cin.ignore();
				break;
			}
		}
	}while(ch=='y');
}

void deposite()
{
	char ch;
	int money;
	do
	{
		cout<<"Your balance is "<<balancee<<endl;
		cout<<"Please enter an amount of money to be deposited"<<endl;
		cin>>money;
		balancee+=money;
		cout<<"Your balance is "<<balancee<<endl;
		cout<<"If you want to deposite again,press y"<<endl;
		cin>>ch;
	}while(ch=='y');

}

void withdraw()
{
	char ch;
	int money;
	do
	{
		cout<<"Your balance is "<<balancee<<endl;
		cout<<"Please enter an amount of money to be withdrawn"<<endl;
		cin>>money;
		if(balancee>money && money>0)
		{
			balancee-=money;
			cout<<"Your balance is "<<balancee<<endl;
		}
		else
		{
			cout<<"Your balance is not enough..Please enter a valid amount"<<endl;
		}
		cout<<"If you want to withdraw again,press y"<<endl;
		cin>>ch;
	}while(ch=='y');
}

void balance()
{
	char ch;
	do
	{
		cout<<"Your balance is "<<balancee<<endl;
		cout<<"If you want to see your balance again,press y"<<endl;
		cin>>ch;
	}while(ch=='y');
}


void eaddress()
{
	char ch;
	do
	{
		cout<<"Please enter your new address."<<endl;
		getline(cin,naddress);
		ch=' ';
		address=naddress;
		cout<<"If you want modify again press y"<<endl;
		cin>>ch;
		cin.ignore();
	}while(ch=='y');
}

void etelephone()
{
	char ch;
	do
	{
		cout<<"Please enter your new telephone number."<<endl;
		getline(cin,ntelephone);
		ch=' ';
		for(int i=0 ; i<ntelephone.size() ; i++)
		{
			if(ntelephone.size()!=8 || ntelephone[i]<'0' || ntelephone[i]>'9')
			{
				cout<<"Please enter a valid telephone number."<<endl;
				break;
			}
			else
			{
				telephone=ntelephone;
				break;
			}
		}
		cout<<"If you want modify again press y"<<endl;
		cin>>ch;
		cin.ignore();
	}while(ch=='y');
}

void emobile()
{
	char ch;
	do
	{
		cout<<"Please enter your new mobile number."<<endl;
		getline(cin,nmobile);
		ch=' ';
		for(int i=0 ; i<nmobile.size() ; i++)
		{
			if(nmobile.size()!=11 || nmobile[i]<'0' || nmobile[i]>'9')
			{
				cout<<"Please enter a valid mobile number."<<endl;
				break;
			}
			else
			{
				mobile=nmobile;
				break;
			}
		}
		cout<<"If you want modify again press y"<<endl;
		cin>>ch;
		cin.ignore();
	}while(ch=='y');
}

void eemail()
{
	char ch;
	do
	{
		cout<<"Please enter your new e-mail."<<endl;
		getline(cin,nemail);
		ch=' ';
		email=nemail;
		cout<<"If you want modify again press y"<<endl;
		cin>>ch;
		cin.ignore();
	}while(ch=='y');
}

void epassword()
{
	char ch1;
	do
	{
		char ch2;
		do
		{
			cout<<"Please enter your new password."<<endl;
			getline(cin,npassword);
			ch2=' ';
			cout<<"Please re-enter your password"<<endl;
			getline(cin,nrepassword);
			if(npassword==nrepassword)
			{
				password=npassword;
			}
			else
			{
				cout<<"The passwords are not identical..Please try again"<<endl;
				cout<<"Press y to re-type again"<<endl;
				cin>>ch2;
				cin.ignore();
			}
		}while(ch2=='y');
		cout<<"If you want modify again press y"<<endl;
		cin>>ch1;
		cin.ignore();
	}while(ch1=='y');
}

void edit()
{
	char ch=' ';
	do
	{
		int choice;
		cout<<"What information would you like to edit ?"<<endl;
		cout<<"1-Address."<<endl;
		cout<<"2-Telephone."<<endl;
		cout<<"3-Mobile."<<endl;
		cout<<"4-E-mail."<<endl;
		cout<<"5-Password."<<endl;
		cin>>choice;
		switch (choice)
		{
		case 1:
			eaddress();
			break;
		case 2:
			etelephone();
			break;
		case 3:
			emobile();
			break;
		case 4:
			eemail();
			break;
		case 5:
			epassword();
			break;
		default:
			cout<<"Please enter a valid option."<<endl;
			break;
		}
		cout<<"If you want to edit again,please enter y"<<endl;
		cin>>ch;
	}while(ch=='y');
}

int save()
{
	string line1,line2,line;
	ifstream myfile("1.txt");
	myfile.is_open();
	for(int i=0 ; i<2 ; i++)
		{
			getline(myfile,line);
		}
	while(myfile.good())
	{
		getline(myfile,line1);
		if(line1==fname)
		{
			getline(myfile,line2);
			if(line2==lname)
			{
				ofstream myfile ("1.txt");
				myfile.is_open();
				myfile<<id<<endl<<password<<endl<<fname<<endl<<lname<<endl<<address<<endl;
				myfile<<telephone<<endl<<mobile<<endl<<email<<endl<<balancee<<endl;
				myfile.close();
				return 0;
			}
		}
		else
		{
			if(myfile.good())
			{
				for(int i=0 ; i<8 ; i++)
				{
					getline(myfile,line);
				}
				continue;
			}
			else
			{
				ofstream myfile ("1.txt",ios::app);
				myfile.is_open();
				myfile<<id<<endl<<password<<endl<<fname<<endl<<lname<<endl<<address<<endl;
				myfile<<telephone<<endl<<mobile<<endl<<email<<endl<<balancee<<endl;
				myfile.close();
				return 0;
			}
		}
	}
	while(!myfile.good())
	{
		ofstream myfile ("1.txt",ios::app);
		myfile.is_open();
		myfile<<id<<endl<<password<<endl<<fname<<endl<<lname<<endl<<address<<endl;
		myfile<<telephone<<endl<<mobile<<endl<<email<<endl<<balancee<<endl;
		myfile.close();
		break;
	}
	return 0;
}

void menu()
{
	char ch=' ';
	do
	{
		cout<<"Please choose one of these operations."<<endl;
		cout<<"1-Deposite Money."<<endl;
		cout<<"2-Withdraw Money."<<endl;
		cout<<"3-Show Your Balance."<<endl;
		cout<<"4-Edit your information"<<endl;
		cout<<"Press the number of the wanted operation."<<endl;
		int choice;
		cin>>choice;
		switch (choice)
		{
		case 1:
			deposite();
			break;
		case 2:
			withdraw();
			break;
		case 3:
			balance();
			break;
		case 4:
			edit();
			break;
		default:
			save();
			break;
		}
		cout<<"If you want another operation press y."<<endl;
		cin>>ch;
		if(ch!='y')
			save();
	}while(ch=='y');
}

void registeration()
{
	cin.ignore();
	firstname();
	lastname();
	cout<<"Please enter you address."<<endl;
	getline(cin,address);
	telephonenum();
	mobilenum();
	cout<<"Please enter you e-mail."<<endl;
	getline(cin,email);
	cout<<"Your account ID is "<<id<<endl;
	char ch=' ';
	do
	{
		cout<<"Please enter a password to be saved for your account."<<endl;
		getline(cin,password);
		cout<<"Please re-type your password."<<endl;
		getline(cin,repassword);
		if(password==repassword)
		{
			cout<<"Congratulations!..You have successfully created your account."<<endl;
			cout<<"If you want to proceed press 1"<<endl;
			cout<<"Or save your data by pressing 2"<<endl;
			int choice;
			cin>>choice;// to enter his choice
			if(choice==1)
			{
				menu();
			}
			else if(choice==2)
			{
				save();
			}
		}
		else
		{
			cout<<"The passwords are not identical..Please try again."<<endl;
			cout<<"Press y to retype."<<endl;
			cin>>ch;
			cin.ignore();
		}

	}while(ch=='y');
}

void load()
{
	cin.ignore();
	int lid;
	string lpassword;
	string line1,line2,line;
	string lfname,llname,laddress,ltelephone,lmobile,lemail,lbalance;
	char ch=' ';
	do
	{
		cout<<"Please enter you ID"<<endl;
		cin>>lid;
		cin.ignore();
		cout<<"Please enter your password"<<endl;
		getline(cin,lpassword);
		ifstream myfile("1.txt");
		myfile.is_open();
		while(myfile.good())
		{
			getline(myfile,line1);
			stringstream convert1(line1);
			int res;
			if(!(convert1>>res))
				res=0;
			if(lid==res)
			{
				getline(myfile,line2);
				if(lpassword==line2)
				{
					id=lid;
					password=lpassword;
					getline(myfile,lfname);
					fname=lfname;
					getline(myfile,llname);
					lname=llname;
					getline(myfile,laddress);
					address=laddress;
					getline(myfile,ltelephone);
					telephone=ltelephone;
					getline(myfile,lmobile);
					mobile=lmobile;
					getline(myfile,lemail);
					email=lemail;
					getline(myfile,lbalance);
					stringstream convert(lbalance);
					if(!(convert>>balancee))
						balancee=0;
					ch=' ';
					cout<<"If you want to proceed press 1."<<endl;
					cout<<"Or press 2 to save your data."<<endl;
					int choice;
					cin>>choice;
					if(choice==1)
					{
						menu();
						break;
					}
					else if(choice==2)
					{
						save();
						break;
					}

				}
				else
				{
					
					cout<<"Your ID is invlaid..Press y to try again."<<endl;
					cin>>ch;
					cin.ignore();
					break;
				}
			}
			else
			{
				if(myfile.good())
				{
					for(int i=0 ; i<8 ; i++)
					{
						getline(myfile,line);
					}
					continue;
				}
				else
				{	
					cout<<"Your ID is invlaid..Press y to try again."<<endl;
					cin>>ch;
					cin.ignore();
					break;
				}

			}
		}
	}while(ch=='y');
}

int main ()
{
	system("Color 1A");
	char ch=' ';
	do
	{
		cout<<"Welcome to BANQUE DE LA FCIS!"<<endl;
		cout<<"If you are a new client press 1."<<endl;
		cout<<"If you already have an account in the bank press 2."<<endl;
		int choice;
		cin>>choice; // to identify which he/she is a new client or an existing client
		if(choice==1)// if he/she is a new client
		{
			registeration();
		}
		else if(choice==2)// if he/she is an existing user
		{
			load();
		}
		else
		{
			cout<<"Error..Please select a valid option."<<endl;
			cout<<"Press y to continue"<<endl;
			cin>>ch;
		}
	}while(ch=='y');
	return 0;
}