Wednesday, May 20, 2009

Different Programming Approaches - Library Management System

In my Institute (UIET) I learnt C, C++ and also PHP ; learnt different approaches of programming mainly procedural and object oriented. Of course I secured an A in all programming courses but several a times I was unable to decide on, which one will be better approach for a certain program. 

Whenever I was given a problem to program, My first instinct was to program that in procedural method although I could give you proper definitions and examples of all the object oriented concepts I learnt. But the problem was that I was unable to think in OOP on the first hand. 

Last few days I have been learning to mitigate this confusion.  and here, am sharing a few things with you.  
Let us consider the simplest example of Library Management System- 

SRS- Software Requirement Specification for our software-
  1. The manager should be able to add a profile (student +book).
  2. He should be able to update and remove a profile.
  3. He can issue, re-issue and return a book.
  4. He must be able to check the fine on a certain book at a certain account. 
  5. He must be able to see the list of issued or non issued books, i.e. we are looking for sorting and searching on a book or profile basis. 
Of course there can/should be a plenty of other facilities, but these which are enlisted are enough to achieve the motive of the post.

First Approach-  Procedure Oriented Programming

Divide and conquer -is called the approach, we simply think of a program  having a certain no. of small functions which do a certain job for it and then finally returning the output to its caller. Its like a hierarchy of caller and doer programs. 

Data structure - A book will have its name, a unique id no., name of publisher, course name etc. 
so the best data structure as it comes in mind is 'struct' for it is meant to collect different type of data in one single data structure. 

Similarly a student will have - A unique roll no. name, course, address etc. also the struct will do great here. 

we can define them before  'int main() ' like- 

struct book
{
int book_id;
char book_name[50];
char book_publisher[50];
char book_course[50][5];
}

struct student
{
int rollno;
char student_fname[50];
char student_lname[50];
int book_counter;
}

Now as we see there will be many profiles and ample of books, how are we gonna organize them?we can make use of array to do that, fixed or of variable size. also we can store the books and profiles in linked list- kind of data structures, oh yes, you can argue that , this can increase the overhead but think positively, it can be a great help while dynamic allocation of memory and also in arranging the books and profiles in preferred order. 
for eg- we can insert a book according to its course, id no. or may be according to the issued date or something like that. 

Then we have simple functions to get our things done, for example we will just input the name roll no and course of the student, and a function named as add_profile will add a new student profile in the linked list we just talked about. 
Similarly we can add a book, via some add_book function. 

we just need to know proper function signature to get our work done. 

But here occurs another problem- that every time you loose your library and student list as you stop execution of the program -by making use of 'file handling' we can overcome this problem. 




No comments:

Post a Comment