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 ^_^

No comments: