#include <iostream>
#include<fstream>
#include <cstdio>

using namespace std;

class exchange{
    public:
    string num,name,pass,cash,findNum,findPass;
    float bal,sentMoney;
    fstream file,file_1;

    void signUp()
    {
        cout<<endl<<"----------SIGNUP---------"<<endl;
        cout<<endl<<"Enter Your Name : ";
        getline(cin,name);
        cout<<endl<<"Enter Your Phone Number : ";
        getline(cin,num);
        cout<<endl<<"Enter Your Password : ";
        getline(cin,pass);

        file.open("loginData.txt",ios :: out | ios :: app);
        file<<num<<"*"<<pass<<"*"<<name<<"*0"<<endl;
        file.close();
    }

    void logIn()
    {
        cout<<endl<<"----------LOGIN---------"<<endl;
        cout<<endl<<"Enter Your Phone Number : ";
        getline(cin,findNum);
        cout<<endl<<"Enter Your Password : ";
        getline(cin,findPass);

        file.open("loginData.txt",ios :: in);
        getline(file,num,'*');
        getline(file,pass,'*');
        getline(file,name,'*');
        getline(file,cash,'\n');
        bool f=true;
        while(!file.eof())
        {
            if(findNum==num && findPass==pass)
            {
                cout<<endl<<"Hello "<<name<<endl;
                f=false;
                break;
            }
            else
            {
                getline(file,num,'*');
                getline(file,pass,'*');
                getline(file,name,'*');
                getline(file,cash,'\n');
            }

        }
        if(f) cout<<endl<<"Invalid Password or Number";
        file.close();

        bal=stof(cash);
    }
    void balance()
    {

        cout<<endl<<"Your Account Balance is : ";
        printf("%.2f TK",bal);
    }
    void sent()
    {
        string revNum,revName,revPass,revCash;
        float revBal;
        cout<<endl<<"Enter Receiver Number : ";
        getline(cin,revNum);
        cout<<endl<<"Enter the Amount : ";
        cin>>sentMoney;

        file.open("loginData.txt",ios :: in);
        file_1.open("new.txt",ios :: out | ios :: app);

        getline(file,num,'*');
        getline(file,pass,'*');
        getline(file,name,'*');
        getline(file,cash,'\n');
        while(!file.eof())
        {
            if(findNum==num)
            {
                bal-=sentMoney;
                cash=to_string(bal);
            }
            else if(revNum==num)
            {
                revBal=stof(cash);
                cash=to_string(revBal+sentMoney);
            }
            file_1<<num<<'*'<<pass<<'*'<<name<<'*'<<cash<<endl;
            getline(file,num,'*');
            getline(file,pass,'*');
            getline(file,name,'*');
            getline(file,cash,'\n');

        }
        file.close();
        file_1.close();

        remove("loginData.txt");
        rename("new.txt","loginData.txt");

        cout<<endl<<"Successful...!";

    }


}money;

int main()
{
    char choice;
    cout<<"\n---------LenDen App--------\n";
    cout<<"\n1- Login";
    cout<<"\n2- Sign-Up";
    cout<<"\n3- Exit";
    cout<<"\nEnter Your Choice :: ";
    cin>>choice;

    switch(choice){
        case '1':
            cin.ignore();
            money.logIn();
            char c;
            cout<<"\n1- Check balance";
            cout<<"\n2- Send Money";
            cout<<"\nEnter Your Choice :: ";
            cin>>c;
            cin.ignore();
            if(c=='1')
            {
                money.balance();
            }
            else
            {
                money.sent();
            }
        break;
        case '2':
            cin.ignore();
            money.signUp();
        break;
        case '3':
            return 0;
        break;
        defualt:
            cout<<"Invalid Selection...!";
    }
    return 0;
}