Wednesday, December 28, 2005

Assignment 2

My code for my computer programming assignment 2. You been warned that reading it might cause ur brain in deeply stressed. Please use the compiler that have been post in the last post to get a clearer view on the program.


#include <iostream>

#include <string>

#include <fstream>

using namespace std;



class Account

{

    public:

        int acc_type;

        string acc_name1;

        string acc_ic1;

        string acc_contactNo1;

        string acc_address1;

        string acc_name2;

        string acc_ic2;

        string acc_contactNo2;

        string acc_address2;

        float acc_balance;

        int index_no;

        int acc_no;

        Account(): acc_name2("0"),
acc_ic2("0"), acc_contactNo2("0"),

                  
acc_address2("0"), acc_balance(0.0){}

        int input_data(int index, int accNo);

        void display_data();

        void display_type();

        int s_to_n_convert(string x);

        float s_to_f_convert(string x);

        string display_accType(int x);

        int read_file();

        void errormsg();

};



int Account::read_file()

{

    ifstream read;

    char answer;

    read.open ("Account.txt");

    if (read.fail()==1)

    {

        cout << "\aFile not found!\n";

        do{

            cout << "Want
to create new file? (Y/N): ";

            cin >>
answer;

            if(answer ==
89 || answer == 121)

            acc_no =
1000001;

            else if
(answer == 'n' || answer == 'N')

            return 0;

            else

            cerr << "\aInvalid
respond entered, please try again!\n";

            system("pause");

        }while(answer!=89&&answer!=121&&answer!='n'&&answer!=
'N');

    }

}



void Account::display_type()

{

    cout << "Account Type: " << endl;

    cout << "1. Personal Saving Account" << endl;

    cout << "2. Join Saving Account (AND signing)" << endl;

    cout << "3. Join Saving Account (OR signing)" << endl;

}



string Account::display_accType(int x)

{

    switch (x)

    {

        case 1: return "1. Personal Saving
Account"; break;

        case 2: return "2. Join Saving
Account (AND signing)"; break;

        case 3: return "3. Join Saving
Account (OR signing)"; break;

    }

}



int Account::s_to_n_convert(string x)

{

    char temp1[255];

    strcpy (temp1, x.c_str());

    int temp = atoi(temp1);

    return temp;

}



float Account::s_to_f_convert(string x)

{

    char temp1[255];

    strcpy (temp1, x.c_str());

    float temp = atof(temp1);

    return temp;

}

int Account::input_data(int index, int accNo)

{

    string respond;

    do{

        system("cls");

        //cout << index << endl;

        Account::display_type();

        cout << "Please choose the type of
account (0 to exit): ";

        getline(cin,respond);

        if(respond == "0")

        return 0;

        acc_type = s_to_n_convert(respond);

        if(acc_type<=0 || acc_type >= 4)

        {

            cerr << "\aYou
have enter a invalid choice, please try again\n" ;

            system("pause");

        }

    }while(acc_type<=0 || acc_type >= 4);



    if(index == 0)

    acc_no = 1000000;

    else

    acc_no = accNo + 1;

   

    system("cls");

    cout << "======================" << endl;

    cout << "First Customer Detials" << endl;

    cout << "======================" << endl;

    cout << "Please enter the customer name (0 to exit): ";

    getline (cin, acc_name1);

    if(acc_name1 == "0")

    {

        Account::errormsg();

        return 0;

    }



    cout << "Please enter the customer IC no (0 to exit): ";

    getline(cin, acc_ic1);

    if(acc_ic1 == "0")

    {

        Account::errormsg();

        return 0;

    }



    cout << "Please enter the customer contact No (0 to exit): ";

    getline(cin, acc_contactNo1);

    if(acc_contactNo1 == "0")

    {

        Account::errormsg();

        return 0;

    }





    cout << "Please enter the customer address (0 to exit): ";

    getline(cin, acc_address1);

    if(acc_address1 == "0")

    {

        Account::errormsg();

        return 0;

    }



    if(acc_type == 2 || acc_type == 3)

    {

        system("cls");

        cout << "=======================" <<
endl;

        cout << "Second Customer Detials" <<
endl;

        cout << "=======================" <<
endl;

        cout << "Please enter the customer
name (0 to exit): ";

        getline (cin, acc_name2);

        if (acc_name2 == "0")

        {

           
Account::errormsg();

            return 0;

        }

        cout << "Please enter the customer IC
no (0 to exit): ";

        getline(cin, acc_ic2);

        if(acc_ic2 == "0")

        {

           
Account::errormsg();

            return 0;

        }



        cout << "Please enter the customer
contact No (0 to exit): ";

        getline(cin, acc_contactNo2);

        if(acc_contactNo2 == "0")

        {

           
Account::errormsg();

            return 0;

        }



        cout << "Please enter the customer
address (0 to exit): ";

        getline(cin, acc_address2);

        if(acc_address2 == "0")

        {

           
Account::errormsg();

            return 0;

        }

    }



    system("cls");

    do{

        system("cls");

        cout << "Please enter the amount to
deposit(Type 'exit' to return) :RM";

        string temp;

        getline(cin, temp);

        if(temp == "exit")

        return 0;

        acc_balance = s_to_f_convert(temp);

        if(acc_balance <= 0)

        {

            cerr << "\aYou
have enter an invalid deposit,"

                   
" please reenter again!" << endl;

            system("Pause");

        }

    }while(acc_balance <= 0);



    Account::display_data();

   

    char respond1;

    do{

        cout << "Is the detials above are
correct? (Y/N)" << endl;

        cin >> respond1;

        if(respond1 == 89 || respond1 == 121)

        {

            ofstream
read;

           
read.open("account.txt", ios::out|ios::app);

            read <<
acc_no << ";" << acc_type << ";" << acc_name1 << ";"

                
<< acc_ic1 << ";" << acc_contactNo1 << ";" << acc_address1

                
<< ";" << acc_name2 << ";" << acc_ic2 << ";" << acc_contactNo2

                
<< ";" << acc_address2 << ";" << acc_balance << ";" <<endl;

            read.close();

            cout <<
"Registration Sucessful!\n";

        }

        else if(respond1==78&&respond1==110)

        cerr << "\aRegistration Unsucessful!\n";

        else
if(respond1!=89&&respond1!=121&&respond1!=78&&respond1!=110)

        cerr <<"\aYou have enter an invalid
choice, please reenter again\n";

    }while
(respond1!=89&&respond1!=121&&respond1!=78&&respond1!=110);

    system("Pause");

    cin.ignore();

    return 0;

}



void Account::display_data()

{

    system("cls");

    cout << "Account Detials" << endl << endl;

    cout << "Account No: " << acc_no << endl;

    cout << "Account Type: " << display_accType(acc_type) << endl;

    cout << "Account Balance: RM" << acc_balance << endl << endl;

    cout << "Detials of 1st customer" << endl;

    cout << "=======================" << endl;

    cout << "Name : " << acc_name1 << endl;

    cout << "IC : " << acc_ic1 << endl;

    cout << "Contact No: " << acc_contactNo1<< endl;

    cout << "Address : " << acc_address1<< endl;

    if(acc_type == 2 || acc_type == 3)

    {

        cout << endl;

        cout << "Detials of 2nd customer" <<
endl;

        cout << "=======================" <<
endl;

        cout << "Name : " << acc_name2 <<
endl;

        cout << "IC : " << acc_ic2 << endl;

        cout << "Contact No: " <<
acc_contactNo2<< endl;

        cout << "Address : " <<
acc_address2<< endl;

    }



}



void Account::errormsg()

{

    cerr << "\aRegistration Cancelled\n";

    system("Pause");

}



class date

{

    protected:

        int year, month, day, hour, minute,
sec, dayweek;

        string dayofweek;



    public:



        void selectday(int day);

        void setdate();

        void displaydate();

};



void date::selectday(int day)

{

    int x = day;

    switch (day)

    {

        case 1: dayofweek = "Monday"; break;

        case 2: dayofweek = "Tuesday"; break;

        case 3: dayofweek = "Wednesday";
break;

        case 4: dayofweek = "Thursday";
break;

        case 5: dayofweek = "Friday"; break;

        case 6: dayofweek = "Saturday";
break;

        case 0: dayofweek = "Sunday"; break;

    }

}



void date::setdate()

{

    SYSTEMTIME st;//

    GetSystemTime(&st);



    year = st.wYear;

    month = st.wMonth;

    day = st.wDay;

    hour = st.wHour+8;

    dayweek = st.wDayOfWeek;

    if(hour >23)

    {

        hour = hour-24;

        day++;

        dayweek++;

        if(day > 31)

        {

            day = 1;

            month++;

            if(month >12)

            {

               
month = 0;

               
year++;

            }

        }

    }

    if (dayweek > 6)

    dayweek = 0;

    minute = st.wMinute;

    sec = st.wSecond;

    selectday(st.wDayOfWeek);

   

}



void date::displaydate()

{

    date::setdate();

    //cout << "Year: " << year << endl;

    //cout << "Month: " << month << endl;

    //cout << "Day: " << day << endl;

    //cout << "Hour: " << hour << endl;

    //cout << "Minutes: " << minute << endl;

    //cout << "Seconds: " << sec << endl;

    //cout << "Day: " << dayofweek <<endl;

    cout << dayofweek << ", ";

    if(day < 10)

    cout << "0" << day << "-";

    else

    cout << day << "-" ;

   

    if(month < 10)

    cout << "0" << month << "-";

    else

    cout << month << "-" ;



    cout << year << ", ";

   

    if(hour < 10)

    cout << "0" << hour << ":";

    else

    cout << hour << ":" ;

   

    if(minute < 10)

    cout << "0" << minute << ":";

    else   

    cout << minute << ":" ;



    if(sec < 10)

    cout << "0" << sec ;

    else

    cout << sec ;



    cout << "(GMT+8)" << endl;

}



class AccountRead : public Account

{

    protected:



    public:

        Account database[50];

        int read_file(int mode);

        int display_data(int mode);

        int search(int x);

        void menu1();

        int deposit();

        float withdraw(int i);



};



int AccountRead:: read_file(int mode)

//to read from customer.txt to array

//mode 1 is for create the textfile if it fails to find the textfile

//mode 2 doesn't create the textfile even if it fails to find the textfile

{



    ifstream read;

    char answer;

    read.open ("Account.txt", ios::in);

    if (read.fail()==1)//if it fails to open the text file

    {

        if(mode == 1)//mode 1

        {

            system("cls");

            cout << "\aFile
not found!\n";

            do{

               
cout << "Want to create new file? (Y/N): ";

               
cin >> answer;

               
cin.ignore();

               
if(answer == 'y' || answer == 'Y')

               
{

                   
ofstream write ("Account.txt", ios::out);//create Account.txt

                   
write.close();

               
}

               
else if (answer == 'n' || answer == 'N')

               
return 0;

               
else

               
{

                   
cerr << "\aInvalid respond entered, please try again!\n";

                   
system("pause");

               
}

            }while(answer!=89&&answer!=121&&answer!='n'&&answer!=
'N');

        }

        else if(mode == 2)//mode 2

        {

            return
0;//return null value as it can't found the textfile

        }


    }

    read.close();

    char temp2[255];

    ifstream file_op("Account.txt", ios::in);

    int i=0;

    while (!file_op.eof())//to read the textfile into array

    {

        file_op.getline(temp2, 255, ';');

        database[i].acc_no = atoi(temp2);

   

        file_op.getline(temp2, 255, ';');

        database[i].acc_type = atoi(temp2);

   

        file_op.getline(temp2, 255, ';');

        database[i].acc_name1 = temp2;



        file_op.getline(temp2, 255, ';');

        database[i].acc_ic1 = temp2;



        file_op.getline(temp2, 255, ';');

        database[i].acc_contactNo1 = temp2;



        file_op.getline(temp2, 255, ';');

        database[i].acc_address1 = temp2;



        file_op.getline(temp2, 255, ';');

        database[i].acc_name2 = temp2;



        file_op.getline(temp2, 255, ';');

        database[i].acc_ic2 = temp2;



        file_op.getline(temp2, 255, ';');

        database[i].acc_contactNo2 = temp2;



        file_op.getline(temp2, 255, ';');

        database[i].acc_address2 = temp2;



        file_op.getline(temp2, 255, ';');

        database[i].acc_balance =
atof(temp2);



   

        i++;//to initiate the counter

    }

    file_op.close();

    return i-1;

}





int AccountRead :: display_data(int mode)

{

    system("cls");

    int x = AccountRead::read_file(mode);//to read the file

    if(x == 0)//if there is no records in the textfile

    {

        cerr << "\aNo Records Found!\n";

        system("pause");

        return 0;

    }

    int i;

    do{

        system("cls");

        i = AccountRead::search(x);//to
search the record

        if(i == 1000)

        return 0;

    }while(i >= 1000);



    //to display the result

    system("cls");

    cout <<
"==============================================================\n";

   

    cout << "Account No : " << database[i].acc_no << endl;

    cout << "Account Type : "

         <<
Account::display_accType(database[i].acc_type) << endl;

    cout << "Customer 1 Name : " << database[i].acc_name1 << endl;

    cout << "Customer 1 IC : " << database[i].acc_ic1 << endl;

    cout << "Customer 1 Contact: " << database[i].acc_contactNo1
<< endl;

    cout << "Customer 1 Address: " << database[i].acc_address1 <<
endl;

    if(database[i].acc_type!=1)

    {

        cout << "Customer 2 Name : " <<
database[i].acc_name2 << endl;

        cout << "Customer 2 IC : " <<
database[i].acc_ic2 << endl;

        cout << "Customer 2 Contact: " <<
database[i].acc_contactNo2 << endl;

        cout << "Customer 2 Address: " <<
database[i].acc_address2 << endl;

    }

    cout << "Account Balance : RM" << database[i].acc_balance <<
endl;

    cout <<
"===========================================================\n";

    system("Pause");

}



int AccountRead::search(int x)

{

    cout << "Please enter the account no (0 to exit): ";

    string temp;

    getline(cin, temp);

    if(temp == "0")//to exit

    return 1000;

    int search = Account::s_to_n_convert(temp);//convert string
to int

    if(search == 0)//for invalid input

    {

        cout << "\aNo record found!\n";

        system("pause");

        return 6000;

    }

    for(int i = 0; i <= x; i++)//search record

    {

        if(AccountRead::database[i].acc_no ==
search)//for success search

        return i;//return the index no

    }

    cout << "\aNo record found!\n";//for fail search

    system("pause");

    return 6000;

}



float AccountRead::withdraw(int i)

{

    //to initiate the max amount to withdraw

    float maxWithdraw = database[i].acc_balance/10;

    maxWithdraw=maxWithdraw*10-100;

    if(maxWithdraw < 0)

    maxWithdraw = 0;

    int withdraw;

    do{

        system("cls");

        cout << "Each transaction will charge
RM0.50" << endl;

        cout << "The balance is RM" <<
database[i].acc_balance << endl

             << "The
maximum amount that can withdraw is: RM"

             <<
maxWithdraw << endl << "How much to withdraw? (0 to exit) : RM";

        string temp;

        getline(cin, temp);

        if(temp == "0")//for exit

        return 0;

        withdraw =
Account::s_to_n_convert(temp);//convert string to int

        if(withdraw > maxWithdraw)//for
withdrawal that exceed the balance

        {

            cerr << "\aDebit
amount exceed max withdraw amount.\n";

            system("Pause");

        }

        else if(withdraw == 0)//for invalid
ammount that key in

        {

            cerr << "\aInvalid
withdraw amount!\n";

            system("Pause");

        }

    }while(withdraw > maxWithdraw||withdraw == 0);

    return database[i].acc_balance-=withdraw;//to get the balance
after withdraw

}



int AccountRead::deposit()

{

    system("cls");

    int x = AccountRead::read_file(1);//to read the file

    if(x == 0)//if there is no records in the textfile

    {

        cerr << "\aNo Records Found!\n";

        system("pause");

        return 0;

    }

    int i;

    do{

        system("cls");

        i = AccountRead::search(x);//to
search the record

        if(i == 1000)

        return 0;

    }while(i >= 1000);

    float deposit;

    do{

        system("cls");

        cout << "The balance is RM" <<
database[i].acc_balance << endl

             << "How
much to deposit? (0 to exit): RM";

        string temp;

        getline(cin, temp);//input amount to
deposit

        if(temp == "0")//to exit

        {

            cerr << "\aTransaction
Cancelled!\n";

            system("Pause");

            return 0;

        }

        deposit =
Account::s_to_f_convert(temp);//convert string into float

        if(deposit == 0)//for invalid input

        {

            cerr << "\aInvalid
amount, please key in again!\n";

            system("Pause");

        }

    }while(deposit==0);

    database[i].acc_balance+=deposit;//get the balance after
deposit

    ofstream write;//rewrite the account.txt

    write.open("Account.txt", ios::out);

    for(int j = 0; j < x; j++)

    {

        write << database[j].acc_no << ";"


             
<< database[j].acc_type << ";"

             
<< database[j].acc_name1 << ";"

             
<< database[j].acc_ic1 << ";"

             
<< database[j].acc_contactNo1 << ";"

             
<< database[j].acc_address1 << ";"

             
<< database[j].acc_name2 << ";"

             
<< database[j].acc_ic2 << ";"

             
<< database[j].acc_contactNo2 << ";"

             
<< database[j].acc_address2 << ";"

             
<< database[j].acc_balance << ";" <<endl;

    }

    write.close();

    cout << "Transaction Sucess!!!\n";

    system("Pause");

    return 0;

}



class CheckingAccount : protected AccountRead

{

    private:

        float transactionFee;

        float countFee(float balance);

       

    public:

        CheckingAccount():
transactionFee(0.5){};

        int deposit();

        int withdraw();

};



int CheckingAccount::deposit()

{

    AccountRead::deposit();

    return 0;

}



int CheckingAccount::withdraw()

{

    system("cls");

    int x = AccountRead::read_file(1);//to read the file

    if(x == 0)//if there is no records in the textfile

    {

        cerr << "\aNo Records Found!\n";

        system("pause");

        return 0;

    }

    int i;

    do{

        system("cls");

        i = AccountRead::search(x);//to
search the record

        if(i == 1000)

        return 0;

    }while(i >= 1000);

    //system("cls");



    database[i].acc_balance=AccountRead::withdraw(i);

    if(database[i].acc_balance == 0)

    {

        cout << "\aTransaction Cancelled!\n";


        system("Pause");

        return 0;

    }

    database[i].acc_balance=CheckingAccount::countFee(database[i].acc_balance);

    ofstream write;//rewrite the account.txt

    write.open("Account.txt", ios::out);

    for(int j = 0; j < x; j++)

    {

        write << database[j].acc_no << ";"


             
<< database[j].acc_type << ";"

             
<< database[j].acc_name1 << ";"

             
<< database[j].acc_ic1 << ";"

             
<< database[j].acc_contactNo1 << ";"

             
<< database[j].acc_address1 << ";"

             
<< database[j].acc_name2 << ";"

             
<< database[j].acc_ic2 << ";"

             
<< database[j].acc_contactNo2 << ";"

             
<< database[j].acc_address2 << ";"

             
<< database[j].acc_balance << ";" <<endl;

    }

    write.close();

    cout << "Transaction Sucess!!!\n";

    system("Pause");

    return 0;

}



float CheckingAccount::countFee(float balance)

{

    balance = balance - transactionFee;

    return balance;

}



class SavingAccount : public AccountRead , public date

{

    private:

        int year, month, day, hour, minute,
sec;

        string dayofweek;

        float interest;





    public:

        SavingAccount():interest(6.25){}

        void calculateInterest();

        void read_date();

        void trigger();

        void displayLast();

        void selectday(int day);

        void renewdate();

};



void SavingAccount::calculateInterest()

{

    int index = AccountRead::read_file(2);

    for(int i = 0; i < index; i++)

    {

        double x = database[i].acc_balance*(interest/100);

        database[i].acc_balance+=x;

    }

    ofstream write;//rewrite the account.txt

    write.open("Account.txt", ios::out);

    for(int j = 0; j < index; j++)

    {

        write << database[j].acc_no << ";"


             
<< database[j].acc_type << ";"

             
<< database[j].acc_name1 << ";"

             
<< database[j].acc_ic1 << ";"

             
<< database[j].acc_contactNo1 << ";"

             
<< database[j].acc_address1 << ";"

             
<< database[j].acc_name2 << ";"

             
<< database[j].acc_ic2 << ";"

             
<< database[j].acc_contactNo2 << ";"

             
<< database[j].acc_address2 << ";"

             
<< database[j].acc_balance << ";" <<endl;   

    }   

    write.close();

}



void SavingAccount::read_date()

{

    date::setdate();

    ifstream read ("Login.txt", ios::in);

    char temp[255];

    if(read.fail()==1)

    {

        ofstream write ("Login.txt", ios::out);

        write << date::year << ";" <<
date::month << ";" << date::day << ";"

             
<< date::hour << ";" << date::minute << ";" << date::sec << ";"

             
<< date::dayofweek << ";";

    }

    else

    read.getline(temp,255, ';');

    year = atoi(temp);

    read.getline(temp,255, ';');

    month = atoi(temp);

    read.getline(temp,255, ';');

    day = atoi(temp);

    read.getline(temp,255, ';');

    hour = atoi(temp);

    read.getline(temp,255, ';');

    minute = atoi(temp);

    read.getline(temp,255, ';');

    sec = atoi(temp);

    read.getline(temp,255, ';');

    dayofweek = temp;

    read.close();

}



void SavingAccount::trigger()

{

    SavingAccount::read_date();

    date::setdate();

    if (year < date::year)

    SavingAccount::calculateInterest();

    SavingAccount::renewdate();

}



void SavingAccount::displayLast()

{

    SavingAccount::read_date();

    cout << dayofweek << ", ";

    if(day < 10)

    cout << "0" << day << "-";

    else

    cout << day << "-" ;



    if(month < 10)

    cout << "0" << month << "-";

    else

    cout << month << "-" ;



    cout << year << ", ";



    if(hour < 10)

    cout << "0" << hour << ":";

    else

    cout << hour << ":" ;

   

    if(minute < 10)

    cout << "0" << minute << ":";

    else

    cout << minute << ":" ;

   

    if(sec < 10)

    cout << "0" << sec << endl;

    else

    cout << sec << endl;


}



void SavingAccount::renewdate()

{

    ofstream write ("Login.txt", ios::out);

    write << date::year << ";" << date::month << ";" << date::day
<< ";"

          << date::hour << ";" <<
date::minute << ";" << date::sec << ";"

          << date::dayofweek <<
";";

}



int main(int argc, char *argv[])

{





    system("color 4F");

    int x = 1;

    SavingAccount SA;

    CheckingAccount CA;

    SA.trigger();

    while(x != 5)

    {

        system("cls");

        string respond;

        int answer;

       

        cout <<"Welcome to NERV Banking
System v1.1" << endl;

        cout <<"You logged in at ";

        SA.displayLast();

        cout << "Please select a service: "
<< endl

             << "1.
Register New Account" << endl

             << "2.
Check Account Detials" << endl

             << "3.
Desposit Money" << endl

             << "4.
Withdraw Money" << endl

             << "5.
Exit System (Logout)" << endl

             <<
"Please enter your choice: ";

        getline(cin,respond);

        x = SA.s_to_n_convert(respond);

        switch (x)

        {

            case 1:

               
{

                   
int index = SA.read_file(1);

                   
SA.input_data(index, SA.database[index-1].acc_no);break;

               
};

            case 2:
SA.display_data(2);break;

            case 3:
SA.deposit(); break;

            case 4:
CA.withdraw();break;

            case 5: break;

            default:

               
{

                   
cerr << "\aInvalid choice, please try again\n";

                   
system("Pause");

                   
break;

               
};

        }

    };

    //system("Pause");

    return EXIT_SUCCESS;

}

 


Total: 870 lines ^_^

Tuesday, December 20, 2005

AHHHHHH I NEED A REST!!!!!!!!!!!!!!!!

hmm, i think most of u know that i really bz for these few days, that the main reason that i "MIA" from my blog.
will try to blog abt all things that happen in these few week maybe by this x'mas (if i can finish my computer programming assignment 2 >_<)

things that keep me bz currently:-
1. Academic matters
- stupid lecture that i have let me having a hard timein studying
- super damn assignment that come straight after the previous just submit
- all my mid-terms are within 5 days.
2. GACC (Games Anime Comic Circle)
- stuff happen, yeah is a lot.

Thursday, December 01, 2005

A$$ignment 1

Well, until now not sure that want to blog abt what. Guess i post the "ersult" hrhar i had done for the last whole weeks.

WARNING: PLease do not try to compile it in ur brain, since will let ur brain went overload. As a recommend use Dev C++ version 4.9.8.0 and above


// TCP1241 Assignment 1 Trimester 2 2005/2006
// Done by Choo Chia Poh, Teo Leong Kim, Tan Hsia-Liang
//Copy of the code is prohibited, All Right Reserved

#include <.iostream>
#include <.fstream>
#include <.string>
#include <.cstring>
#include <.conio.c>
#include <.stdlib.h>

using namespace std;
struct cust{
char username[255];
char password[255];
char name[255];
char ic[255];
char contact_no[255];
char address[255];
};

struct book{
char book_code[255];
char pass_name[255];
char pass_ic[255];
char flight_code[255];
char promo_code[255];
char tax[255];
char card_no[255];
int commit;
};

struct promo{
char promo_code[255];
char flight_code[255];
char seats_left[255];
char promo_price[255];
};

struct sche{
char flight_code[255];
char from[255];
char destination[255];
char date[255];
char time[255];
char price[255];
char seats_left[255];
};

void load_cust();
void load_book();
void load_promo();
void load_sche();
void login();
void menu1();
void menu2();
void add_entry();
void add_entry2();
void display_entry();
void display_entry2();
void add_user();
void add_book();
void modify_book();
void display_sche();
void display_book();
void add_book();
void write_entry();
void edit_pass_name(int);
void edit_card_no(int);
void edit_flight_code(int);
void edit_pass_ic(int);
void edit_promo_code(int);
void write_book();
void delete_entry();
void print();
void splash();
cust detial[30];
book book_detail[30];
promo promo_detail[30];
sche sche_detail[30];
book add_detial[1];


//Universal Variable for Login System===========================================
int total_index;
int index;
//==============================================================================

//Universal Variable for User Registration======================================
string username;
string password;
string password2;
string ic;
string contact;
string cust_name;
string address;
//==============================================================================

//Universal Variable for Modify System==========================================
int book_index;
//==============================================================================

//Universial Variable for flight timetable======================================
int sche_index;
//==============================================================================

//Universial Variable for Promo code checking===================================
int promo_index;
//==============================================================================

//Main Function=================================================================
int main()
{
splash();
menu1();
menu2();
system("Pause");
return 0;
}
//==============================================================================

//Menu 1 =======================================================================
void menu1()
{
int choice = 1;
while(choice!=0)
{
clrscr();
gotoxy(20, 5);
cout << "Welcome to Air Asia Online Reservation System" <<>> choice;
if(choice<1||choice>3)
gotoxy(27,15);
cerr << "Invalid choice, please retry again."; }while(choice<1||choice>3);

switch(choice)
{
case 1: clrscr(); login(); cin.ignore(); break;
case 2: clrscr(); add_user(); cin.ignore(); break;
case 3: clrscr(); exit(1); cin.ignore(); break;
}
}
}
//==============================================================================

//Menu 2========================================================================
void menu2()
{

int choice = 1;
while(choice!=0)
{
clrscr();
gotoxy(23, 3);
cout << "Welcome Mr./Ms. " <<>> choice;
if(choice<1||choice>7)
gotoxy(24, 20);
cerr << "Invalid choice, please retry again."; }while(choice<1||choice>7);
switch(choice)
{
case 1: clrscr(); add_book() ; cin.ignore(); break;
case 2: clrscr(); modify_book(); cin.ignore(); break;
case 3: clrscr(); delete_entry(); cin.ignore(); break;
case 4: clrscr(); display_sche(); cin.ignore(); break;
case 5: clrscr(); display_book() ; cin.ignore(); break;
case 6: clrscr(); print(); cin.ignore(); break;
case 7: clrscr(); menu1(); cin.ignore(); break;
}
}
}
//==============================================================================

//Loading function(customer.txt)================================================
//To load the customer.txt into the array
void load_cust()
{
string temp;
ifstream file_op("customer.txt", ios::in);
int i=0;
while (!file_op.eof())
{
file_op.getline(detial[i].username, 255, ';');
temp = detial[i].username;
temp.erase(0,1);
strcpy(detial[i].username, temp.c_str());

file_op.getline(detial[i].password, 255, ';');
temp = detial[i].password;
temp.erase(0,1);
strcpy(detial[i].password, temp.c_str());

file_op.getline(detial[i].name, 2000, ';');
temp = detial[i].name;
temp.erase(0,1);
strcpy(detial[i].name, temp.c_str());

file_op.getline(detial[i].ic, 2000, ';');
temp = detial[i].ic;
temp.erase(0,1);
strcpy(detial[i].ic, temp.c_str());

file_op.getline(detial[i].contact_no, 2000, ';');
temp = detial[i].contact_no;
temp.erase(0,1);
strcpy(detial[i].contact_no, temp.c_str());

file_op.getline(detial[i].address, 2000, ';');
temp = detial[i].address;
temp.erase(0,1);
strcpy(detial[i].address, temp.c_str());

i++;
}
total_index = i;

file_op.close();
}
//==============================================================================

//Loading function(booking.txt)=================================================
//To load the booking.txt into the array
void load_book()
{
string temp;
ifstream file_op("booking.txt", ios::in);
int i=0;
while (!file_op.eof())
{
file_op.getline(book_detail[i].book_code, 255, ';');
temp = book_detail[i].book_code;
temp.erase(0,1);
strcpy(book_detail[i].book_code, temp.c_str());

file_op.getline(book_detail[i].pass_name, 255, ';');
temp = book_detail[i].pass_name;
temp.erase(0,1);
strcpy(book_detail[i].pass_name, temp.c_str());

file_op.getline(book_detail[i].pass_ic, 255, ';');
temp = book_detail[i].pass_ic;
temp.erase(0,1);
strcpy(book_detail[i].pass_ic, temp.c_str());

file_op.getline(book_detail[i].flight_code, 255, ';');
temp = book_detail[i].flight_code;
temp.erase(0,1);
strcpy(book_detail[i].flight_code, temp.c_str());

file_op.getline(book_detail[i].promo_code, 255, ';');
temp = book_detail[i].promo_code;
temp.erase(0,1);
strcpy(book_detail[i].promo_code, temp.c_str());

file_op.getline(book_detail[i].tax, 255, ';');
temp = book_detail[i].tax;
temp.erase(0,1);
strcpy(book_detail[i].tax, temp.c_str());

file_op.getline(book_detail[i].card_no, 255, ';');
temp = book_detail[i].card_no;
temp.erase(0,1);
strcpy(book_detail[i].card_no, temp.c_str());

book_detail[i].commit = 0;
i++;
}
book_index = i;
file_op.close();
}
//==============================================================================

//Loading function(promotion.txt)===============================================
//To load the promotion.txt into the array
void load_promo()
{
string temp;
ifstream file_op("promotion.txt", ios::in);
int i=0;
while (!file_op.eof())
{
file_op.getline(promo_detail[i].promo_code, 255, ';');
temp = promo_detail[i].promo_code;
temp.erase(0,1);
strcpy(promo_detail[i].promo_code, temp.c_str());

file_op.getline(promo_detail[i].flight_code, 255, ';');
temp = promo_detail[i].flight_code;
temp.erase(0,1);
strcpy(promo_detail[i].flight_code, temp.c_str());

file_op.getline(promo_detail[i].seats_left, 255, ';');
temp = promo_detail[i].seats_left;
temp.erase(0,1);
strcpy(promo_detail[i].seats_left, temp.c_str());

file_op.getline(promo_detail[i].promo_price, 255, ';');
temp = promo_detail[i].promo_price;
temp.erase(0,1);
strcpy(promo_detail[i].promo_price, temp.c_str());

i++;
}
promo_index = i;
file_op.close();
}
//==============================================================================

//Loading function(schedule.txt)===============================================
//To load the schedule.txt into the array
void load_sche()
{
string temp;
ifstream file_op("schedule.txt", ios::in);
int i=0;
while (!file_op.eof())
{
file_op.getline(sche_detail[i].flight_code, 255, ';');
temp = sche_detail[i].flight_code;
temp.erase(0,1);
strcpy(sche_detail[i].flight_code, temp.c_str());

file_op.getline(sche_detail[i].from, 255, ';');
temp = sche_detail[i].from;
temp.erase(0,1);
strcpy(sche_detail[i].from, temp.c_str());

file_op.getline(sche_detail[i].destination, 255, ';');
temp = sche_detail[i].destination;
temp.erase(0,1);
strcpy(sche_detail[i].destination, temp.c_str());

file_op.getline(sche_detail[i].date, 255, ';');
temp = sche_detail[i].date;
temp.erase(0,1);
strcpy(sche_detail[i].date, temp.c_str());

file_op.getline(sche_detail[i].time, 255, ';');
temp = sche_detail[i].time;
temp.erase(0,1);
strcpy(sche_detail[i].time, temp.c_str());

file_op.getline(sche_detail[i].price, 255, ';');
temp = sche_detail[i].price;
temp.erase(0,1);
strcpy(sche_detail[i].price, temp.c_str());

file_op.getline(sche_detail[i].seats_left, 255, ';');
temp = sche_detail[i].seats_left;
temp.erase(0,1);
strcpy(sche_detail[i].seats_left, temp.c_str());

i++;
}
sche_index = i;
file_op.close();
}
//==============================================================================

//Login=========================================================================
//The login function for the system

void login()
{
load_cust();//load customer textfile to memory
int ch, i;
char username[255];
cin.ignore();
//for username
for(int chance = 0; chance <3; i =" 0;" i="0;" result =" 0;" result =" 2;" result ="="" result ="="" result ="="" chance ="="" index =" i;//to" chance =" 0;">> password;
int result;
if(strcmp(detial[index].password,password)==0)
result = 0;
else
result = 2;
if(result == 2 && chance <= 1) { gotoxy(24,20); cerr << "Password incorrect!"; } else if(result == 2 && chance == 2) { gotoxy(16,20); cerr << "Invalid password have key in, program exiting....."; getch(); exit(1); } else if (result == 0) break; }; system("cls"); menu2(); } //============================================================================== //User Registration============================================================= void add_user() { char answer, answer2; ofstream read("customer.txt", ios::out|ios::app); do{ do{ clrscr(); add_entry(); clrscr(); display_entry(); gotoxy(20, 17); cout << "Are the details above are correct? (Y/N): "; cin >> answer;
if(answer=='n'||answer=='N')
{
gotoxy(20, 17);
cout << "Do you want to reenter your entry? (Y/N): "; cin >> answer2;
}
else if(answer=='y'||answer=='Y')
{
read <<>> username;
password = 'asd';
password2 = 'wewqe';
cin.ignore();
while (password != password2)
{

cout << "Please enter a password: "; cin >> password;

cout <<"Please enter the password again: "; cin >> password2;
if(password != password2)
{
cout << "Both password are unmatch, please enter the password again.\n"; } }; cin.ignore(); cout << "Please enter your name: "; getline(cin,cust_name); cout << "Please enter your IC No.: "; cin >> ic;

cout << "Please enter your contact number: "; cin >> contact;

cin.ignore();
cout << "Please enter your address: "; getline(cin,address); } //============================================================================== //User Registration (Display)=================================================== void display_entry() { gotoxy(24, 5); cout << "Username : " << booking="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" entry="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" temp4 =" 0;" temp3="1;" temp =" atoi(book_detail[book_index-2].book_code);" i ="0;" temp =" strcmp(add_detial[0].flight_code,sche_detail[i].flight_code);" temp="="0)" i ="0;" temp =" strcmp(add_detial[0].promo_code,promo_detail[i].promo_code);" temp="="0)" temp2 =" i;" temp3="strcmp(add_detial[0].flight_code,promo_detail[temp2].flight_code);" temp4 =" atoi(promo_detail[temp2].seats_left);" temp =" 1;" temp="="1)" entry="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="">> answer;
if (tolower(answer)!='y'&& tolower(answer)!='n')
gotoxy(16,20);
cout << "You have enter a invalid respond, please try again\n"; }while(tolower(answer)!='y'&& tolower(answer)!='n'); } //============================================================================== //Write booking entry to text file============================================== void write_entry() { ofstream write("booking.txt", ios::out|ios::app); write << booking="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" i =" 1;" temp =" i;">> num;
if (num<1||num>temp)
cout << "Invalid choice, please retry again."; } while (num<1||num>temp);

do{
clrscr();
cout << "Please choose the attribute that you want to edit:" <<>> choice;
if(choice<1||choice>6)
cerr << "Invalid choice, please retry again."; }while(choice<1||choice>6);
switch(choice)
{
case 1: clrscr(); edit_pass_name(num); break;
case 2: clrscr(); edit_pass_ic(num); break;
case 3: clrscr(); edit_flight_code(num); break;
case 4: clrscr(); edit_promo_code(num); break;
case 5: clrscr(); edit_card_no(num); break;
case 6: clrscr(); write_book();menu2(); break;
};
}while(choice != 8);
}
//==============================================================================

//Edit pass_name================================================================
void edit_pass_name(int num)
{
cin.ignore();
char temp[255];
//cout << card_no="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" flight_code="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" i ="0;" temp =" strcmp(book_detail[num].flight_code,sche_detail[i].flight_code);" temp="="0)" pass_ic="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" promo_code="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" temp4 =" 0;" temp3="1;" i ="0;" temp =" strcmp(book_detail[num].promo_code,promo_detail[i].promo_code);" temp="="0)" temp2 =" i;" temp3="strcmp(book_detail[num].flight_code,promo_detail[temp2].flight_code);" temp4 =" atoi(promo_detail[temp2].seats_left);" temp =" 1;" temp="="1)" txt="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" i =" 0;" detial="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" j =" 0;" k =" 0;" i =" 1;" i =" 0;" timetable="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" i =" 1;" entry="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" i =" 1;" temp =" i;">> num;
if (num<1||num>temp)
cout << "Invalid choice, please retry again.\n"; } while (num<1||num>temp);

book_detail[num].commit = 1;

ofstream write("booking.txt", ios::out);
write << "B"; for (int i = 0; i < commit ="="" print="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" i =" 0;" j =" 1;">> respond;

//To get the ticket price
check3 = strcmp(temp_arr[respond].promo_code,"0");
if(check3 == 0)
{
for(int i = 0; i < check="strcmp(temp_arr[respond].flight_code,sche_detail[i].flight_code);" check ="="0)" temp1 =" atof(sche_detail[i].price);" i =" 0;" check="strcmp(temp_arr[respond].flight_code,promo_detail[i].flight_code);" check ="="0)" temp1 =" atof(promo_detail[i].promo_price);" i =" 0;" check2 ="strcmp(temp_arr[respond].flight_code,sche_detail[i].flight_code);" check2="="0)">***********<> " << endl;
cout << " ******** " << endl;
cout << " ****** " << endl;
cout << " **** " << endl;
cout << " ** " << endl;
cout << " * " << endl << "\n";
cout << " ~~~~ Now Everyone Can Fly ~~~~";
getch();
}