Top Banner
Further Programming Concepts in C++TP016028 UC2F1007CSE Contents Introduction......................................................... 2 Class Diagram........................................................ 3 Use Case Diagram..................................................... 4 Activity Diagram..................................................... 5 Sequence Diagram..................................................... 6 Object-Oriented Concepts............................................. 7 Classes.............................................................7 Employee Parent Class.............................................7 Admin Child Class of Employee.....................................8 HR Child Class of Employee........................................8 Objects.............................................................9 In Login() function...............................................9 In displaySearch() function.......................................9 Encapsulation......................................................10 Constructors.......................................................11 Employee Class Constructor.......................................11 Admin Class Constructor..........................................11 HR Class Constructor.............................................11 Inheritance........................................................12 Polymorphism.......................................................12 Overriding.......................................................12 Overloading......................................................14 Validation.......................................................... 15 Gender field validation............................................15 Simple choices validation..........................................15 Validate Designation Entry.........................................15 Validate Department Entry..........................................16 1
40
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

ContentsIntroduction.................................................................................................................................................2

Class Diagram..............................................................................................................................................3

Use Case Diagram........................................................................................................................................4

Activity Diagram..........................................................................................................................................5

Sequence Diagram.......................................................................................................................................6

Object-Oriented Concepts...........................................................................................................................7

Classes.....................................................................................................................................................7

Employee Parent Class........................................................................................................................7

Admin Child Class of Employee............................................................................................................8

HR Child Class of Employee.................................................................................................................8

Objects....................................................................................................................................................9

In Login() function................................................................................................................................9

In displaySearch() function..................................................................................................................9

Encapsulation........................................................................................................................................10

Constructors..........................................................................................................................................11

Employee Class Constructor..............................................................................................................11

Admin Class Constructor....................................................................................................................11

HR Class Constructor.........................................................................................................................11

Inheritance............................................................................................................................................12

Polymorphism.......................................................................................................................................12

Overriding..........................................................................................................................................12

Overloading.......................................................................................................................................14

Validation..................................................................................................................................................15

Gender field validation..........................................................................................................................15

Simple choices validation......................................................................................................................15

Validate Designation Entry....................................................................................................................15

Validate Department Entry....................................................................................................................16

Validate IC field......................................................................................................................................16

Validate Date fields................................................................................................................................17

1

Page 2: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Validate search select............................................................................................................................17

Test Plan....................................................................................................................................................18

Validation..............................................................................................................................................18

System Reliability...................................................................................................................................21

Program sample outputs...........................................................................................................................22

Login screen...........................................................................................................................................22

Admin Menu..........................................................................................................................................22

HR Menu................................................................................................................................................23

Normal staff Menu................................................................................................................................23

View Details...........................................................................................................................................24

Register new account............................................................................................................................24

Edit account...........................................................................................................................................26

Search Prompt.......................................................................................................................................26

Search Result.........................................................................................................................................27

Additional Features...................................................................................................................................28

Add Designation and specify designation’s access outside program.....................................................28

Add Departments outside program.......................................................................................................28

Multi Field Search..................................................................................................................................28

Conclusion.................................................................................................................................................29

References.................................................................................................................................................30

2

Page 3: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Introduction

Throughout the project creation period it took me 4 phases of routine in order to complete

the assignment. The first phase that I dived into head first is the learning phase where I read

books, learned the syntaxes and understood how object oriented programming concepts, this

phase took the longest period. The second phase is the project design phase, in this phase I

created the designs of the screen outputs and the flow of the program. The third phases is the

programming phase this is where I put in the logics for the solutions, object oriented

programming concepts and validations to the project solution. The last phase is the testing phase,

in the testing phase I used the program, tested its inputs and output and also add in some last

minutes logic correction and design correction.

This assignment was a success for one reason is due to the module’s combined lecturers

guidance their help in showing us how object oriented programming concepts can be used to

solve problems allowed us to apply the OOP concepts that made the programming task more

easier, other than that I was also able to learn and understand the syntax and features of C++

faster because of the lectures and lab session conducted. The library was also a big help although

most of the time the book needed is not available and the borrowing period of the book is too

short still it was able to provide me with some reasonable books while waiting for the book that I

needed.

3

Page 4: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Class Diagram

4

Page 5: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Use Case Diagram

5

Page 6: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

6

Page 7: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Activity Diagram

7

Page 8: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Sequence Diagram

8

Page 9: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Object-Oriented Concepts

Classes

Employee Parent Classclass Employee{

friend ostream& operator<< (ostream&,Employee);protected :

string empID;string name;string IC;string gender;string designation;string department;string dateJoined, DOB;string nationality;string religion;string maritialStatus;

public :Employee(void);Employee(int);

Employee(string,string,string,string,string,string,string,string,string,string,string);

virtual bool guiInput(); //to input all data, used during registration of new employee

virtual bool guiInputHR();virtual bool guiMenu();virtual bool guiDetail();virtual bool guiDetailA();virtual bool guiDetailHR();virtual bool guiEdit();virtual bool guiEditA();virtual bool guiEditHR();bool write2File(); //writes the edited employee detailsbool Delete(); //deletes the employee from file//setter and getterstring getEmpID();string getName();string getIC();string getGender();string getDesignation();string getDepartment();string getDateJoined();string getDOB();string getNationality();string getReligion();string getMaritialStatus();

};

The parent class where its fields can be inherited and the methods are publicly available for use

9

Page 10: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Admin Child Class of Employeeclass Admin:public Employee{public :

Admin(void);bool guiMenu();bool searchEmp();

};

The HR class which inherits all the fields and method from the Parent Employee class but overwrites the method called guiMenu() and has an extra new method called searchEmp()

HR Child Class of Employeeclass HR:public Employee{public:

HR(void);bool guiMenu();bool searchEmp();

};

The HR class which inherits all the fields and method from the Parent Employee class but overwrites the method called guiMenu() and has an extra new method called searchEmp()

10

Page 11: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Objects

In Login() function-Retriving An Admin object from file

if(empID.substr(0,1).compare("A") == 0){Admin loged = retriveAdm(convertID(empID));if(loged.getEmpID().compare(empID) != 0){

cout<<"\nPlease enter the correct \"Employee ID\""<<endl;

getch();continue;

}if(!checkPass(convertID(empID),pass)){

cout<<"\nPlease enter the correct \"Password\""<<endl;getch();continue;

}//run Admin objectloged.guiMenu();

}

This function retrives an Admin object from the binary file according to the employee ID and then executes the Admin object’s guiMenu() method.

In displaySearch() function-in displaying employee list from search results

int displaySearch(Employee *a, int n){

int i = 0,j = 0;cout<<"Choice EmpID IC Name"<<endl;for(i = 0; i < n ;i++){

++j;if(j<10)cout<<"'"<<j<<"' - "<<a[i]<<endl;else if(j<100)cout<<"'"<<j<<"' - "<<a[i]<<endl;else if(j<1000)cout<<"'"<<j<<"' - "<<a[i]<<endl;else cout<<"'"<<j<<"'- "<<a[i]<<endl;

}return i;

}

This function get a list of search hits from the file as an array of Employee objects then displays the employee object’s details.

11

Page 12: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

EncapsulationDisplay of employee details

ostream& operator<< (ostream& out, Employee a){out<<a.empID<<" "<<a.IC<<" "<<a.name;return out;

}

This method encapsulates data as the functions using this method does not need to bother or handle the infromation being displayed or the format in which it will display, all the function need to know is that the method returns a string value.

12

Page 13: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Constructors

Employee Class ConstructorEmployee::Employee(void){

empID = "-";name = "-";IC = "-";gender = "-";designation = "-";department = "-";dateJoined = "-";DOB = "-";nationality = "-";religion = "-";maritialStatus = "-";

}

The constructor for the parent class Employee that takes in no values.

Admin Class ConstructorAdmin::Admin(void):Employee(){}

Since the Admin has no new fields all the fields that it has is from the parent Employee class which already has its own constuctor, therefor the Admin class constructor just needs to call one of the parent class constructor, where in this example its the Employee() constructor.

HR Class ConstructorHR::HR(void):Employee(){}

Since the HR has no new fields all the fields that it has is from the parent Employee class which already has its own constuctor, therefor the HR class constructor just needs to call one of the parent class constructor, where in this example its the Employee() constructor.

13

Page 14: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Inheritance

class Employee

class Admin:public Employee

class HR:public Employee

From the example above we can see that the class Admin and HR inherits the Employee class and since the the fields are set under the access modifier of protected and the method are under the the access modifier of public the two child classes has every field and method in them.

Polymorphism

Overriding-in guiMenu() of Employee Class and Admin Class

In Employee class:

bool Employee::guiMenu(){

system("cls");return guiDetail();

}

In Admin class:

bool Admin::guiMenu(){char choice;bool loopmenu = true;

do{system("cls");cout<<endl;cout<<setw(35)<<"-----------------"<<endl;cout<<setw(35)<<"# ADMINISTRATOR #"<<endl;cout<<setw(35)<<"-----------------"<<endl;cout<<endl;

cout<<"'1' - My Account"<<endl;cout<<"'2' - Register New Employee"<<endl;cout<<"'3' - Search"<<endl;cout<<"'4' - Settings"<<endl;cin>>choice;cin.clear();cin.sync();

14

Page 15: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

if (choice == '1') {

guiDetailA();}else if (choice == '2'){

Employee input;input.guiInput();

}else if (choice == '3'){

searchEmp();}else if (choice == '4');else if (choice == 'x' || choice == 'X') loopmenu = false;else{

printf("\a");cout<<"please choose either 1 , 2 , 3 , 4 or 'X'"<<endl;getche();

}}while (loopmenu);return true;

}

The since the Employee class methods are inherited into Admin class the guiMenu() method of Employee class is overridden with the guiMenu() method in the Admin class.

15

Page 16: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Overloading-for Employee class constructors

Employee::Employee(void){

empID = "-";name = "-";IC = "-";gender = "-";designation = "-";department = "-";dateJoined = "-";DOB = "-";nationality = "-";religion = "-";maritialStatus = "-";

}

Employee::Employee(string empID1, string name1, string IC1, string gender1, string designation1, string department1, string dateJoined1, string DOB1, string nationality1, string religion1, string maritialStatus1){

empID = empID1;name = name1;IC = IC1;gender = gender1;designation = designation1;department = department1;dateJoined = dateJoined1;DOB = DOB1;nationality = nationality1;religion = religion1;maritialStatus = maritialStatus1;

}

From the example above we can see the constructors are for the same Employee class but there are two of them, this is made possible by to overloading the constructors with different parameters and different amounts of parameters to take in.

16

Page 17: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Validation

Gender field validation//VALIDATE GENDER FIELDbool validateF_M(char check){

switch (check){case 'F' :case 'f' :case 'm' :case 'M' :{

return true; }

default : {cout<<"\nplease choose either 'F' or 'M'"<<endl;return false;

}}

}

Simple choices validation//VALIDATE SIMPLE CHOICEbool validateSC(char choice){

switch (choice){case '1':case '2':{

return true; }

default :{cout<<"please choose either '1' or '2'"<<endl;return false;

}}

}

Validate Designation Entry//VALIDATE DESIGNATION ENTRYbool validateDes(char choice[]){

int i = 0;dyString *a = NULL;

while(choice[i] != '\0'){if(choice[i] > 57 || choice[i] < 48){

cout<<"please enter only numbers"<<endl;return false;

}i++;

}i = 0;a = readDes();while(a != NULL){

a = a->next;

17

Page 18: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

i++;}if(atoi(choice)> i){

cout<<"choose numbers smaller then "<<++i<<endl;return false;

}return true;

}

Validate Department Entry//VALIDATE DEPARTMENT ENTRYbool validateDep(char choice[]){

int i = 0;dyString *a = NULL;

while(choice[i] != '\0'){if(choice[i] > 57 || choice[i] < 48){

cout<<"please enter only numbers"<<endl;return false;

}i++;

}i = 0;a = readDep();while(a != NULL){

a = a->next;i++;

}if(atoi(choice)> i){

cout<<"choose numbers smaller then "<<++i<<endl;return false;

}return true;

}

Validate IC field//VALIDATE IC FIELDbool validateIC(char ic[]){

string checkIC = ic;int i = 0;bool check = true;

if(strlen(ic) > 12 || strlen(ic) < 12){cout<<"your IC number must be exactly 12 characters long with no

space or aplhabets"<<endl;check = false;

}while(ic[i] != '\0'){

if(ic[i] > 57 || ic[i] < 48){cout<<"please enter only numbers"<<endl;return false;

}i++;

}return check;

18

Page 19: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

}

Validate Date fields//VALIDATE DATE FIELDSbool validateDate(char date[]){

bool check = true;int i = 0;

if(strlen(date) > 10 || strlen(date) < 10){cout<<"please follow \"DD/MM/YYYY\" format"<<endl;check = false;

}while(date[i] != '\0'){

if(!(date[i] <= 58 && date[i] >= 47)){cout<<"please enter only numbers or '/'"<<endl;return false;

}i++;

}return check;

}

Validate search select//VALIDATE SEARCH SELECTbool validateLastNode(int lenght, string choice){

bool check = false;

if(atoi(choice.c_str()) <= lenght && atoi(choice.c_str()) > 0)check = true;

else check = false;

return check;}

19

Page 20: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Test Plan

Validation

Component Criteria Expected result Actual result

Validate gender field F || f || M || m Return true Return true

Else Display error “please choose either ‘F’ or ‘M’”.Return false

Display error “please choose either ‘F’ or ‘M’”.Return false

Validate Designation field

Enter numbers only and must be a valid designation

Return true Return true

If not a number Display error “please enter only numbers”.Return false

Display error “please enter only numbers”.Return false

If not a valid designation Display error “Choose numbers smaller then [last designation number]”.Return false

Display error “Choose numbers smaller then [last designation number]”.Return false

20

Page 21: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Validate Department field

Enter numbers only and must be a valid department

Return true Return true

If not a number Display error “please enter only numbers”.Return false

Display error “please enter only numbers”.Return false

If not a valid department Display error “Choose numbers smaller then [last department number]”.Return false

Display error “Choose numbers smaller then [last department number]”.Return false

Validate IC field String length = 12 and is numbers only

Return true Return true

String length below 12 or above 12

Display error “your IC number must be exactly 12 characters long with no space or alphabets”.Return false

Display error “your IC number must be exactly 12 characters long with no space or alphabets”.Return false

Not numbers only Display error “please enter only numbers”.Return false

Display error “please enter only numbers”.Return false

Validate date fields String length = 10 and is number or ‘/’

Return true Return true

String length is not 10 Display error “please follow “DD/MM/YYYY” format”.Return false

Display error “please follow “DD/MM/YYYY” format”.Return false

If not number or ‘/’ Display error Display error

21

Page 22: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

“please enter only numbers or /”

“please enter only numbers or /”

Validate search select If choice is within search list numbers

Return true Return true

If choice not within search list numbers

Return false Return false

22

Page 23: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

System Reliability

Component Criteria Expected result Actual result

Login Login with admin account.

Displays admin menu.

Displays admin menu.

Login with human resource staff account.

Displays human resource staff menu.

Displays human resource staff menu.

Login with normal staff account.

Displays staff details.

Displays staff details.

View details User select “my account”

Display user’s details.

Display user’s details.

User search for a staff and select staff from the search result.

Display the staff’s details.

Display the staff’s details.

Edit details User select “Edit” to edit details

Prompt for field to edit, store changes and display edited field in user details

Prompt for field to edit, store changes and display edited field in user details.

Delete staff User search for a staff and select “Delete” from search result choice menu.

Removes the staff account and re-display the new search results

Removes the staff account and re-display the new search results

Search staff User select “Search” Prompt for user search string, then display search result, prompt for user search hit staff choice

Prompt for user search string, then display search result, prompt for user search hit staff choice

Register new account User select “Register New Employee”

Prompt for fields entry, validate entry, create new employee ID and display, store new employee, display “registration success”

Prompt for fields entry, validate entry, create new employee ID and display, store new employee, display “registration success”

23

Page 24: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

24

Page 25: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Program sample outputs

Login screen

Admin Menu

25

Page 26: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

HR Menu

Normal staff Menu

26

Page 27: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

View Details

Register new account

27

Page 28: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

28

Page 29: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Edit account

Search Prompt

29

Page 30: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Search Result

30

Page 31: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Additional Features

Add Designation and specify designation’s access outside program

The designation can be specified in the designation.txt file and the designation’s access

can be specify 1,2 or 3 in front of the designation name without space and each 1, 2 and 3

represents the Admin account access, HR account access and Normal staff account access.

Add Departments outside program

The department can be specified in the department.txt file each line in the file represents

a department.

Multi Field Search

The search just takes in one string but it has the ability to search all the fields for

matching results, this way the search menu is simplified and the search results are optimized by

proper separation between the fields and proper formatting of the fields.

31

Page 32: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

Conclusion

Through out the assignment I have realized that even though C++ is not as modern as

Java but C++ has the ability to create large and complex systems due to the fact that it is an

object oriented programming language. C++ also has certain features other programming

languages don’t have like pointers and operator overloading, these features have proved to be

very useful over course of the assignment’s program development. I believe C++ could be better

if its library functions are more well developed like Java’s API. Due to current growth of internet

technologies I believe that C++ as the most demanded programming language is soon to change,

even so the use of C++ to create a system has made me more knowledgeable on the field of

software development

32

Page 33: C++ Assignment

Further Programming Concepts in C++ TP016028 UC2F1007CSE

References

1. DEITEL, H. M., 2002, C++ How to Program.5th edition, London, Prentice Hall.

2. JOYCE FARRELL, 2009, Object-Oriented Programming USING C++, 4th edition, USA,

COURCE TECHNOLOGY CENGAGE Learning

3. 2010, ASCII Table and Description,[online], ASCIITable, Available from

http://www.asciitable.com/, [12/11/2010]

4. 2010, string, [online], cplusplus, Available from

http://www.cplusplus.com/reference/string/string/, [11/11/2010]

33