Click here to Skip to main content
15,907,395 members
Articles / Programming Languages / C#
Article

A simple library catalog system in C#

Rate me:
Please Sign up or sign in to vote.
1.91/5 (9 votes)
15 Nov 2007Ms-RL2 min read 70.3K   5.7K   12   8
This article describes a simple library catalog system developed in C# which can be used as the foundation.

Introduction

In this article we will be examining the functionality made possible by inheritance and polymorphism. Note that this application requires you to login to begin with. Three logins are created when the application starts, one for each type of user:

Administrative User
Login: admin
Password: admin
Staff Member User
Login: staff
Password: staff
Student User
Login: student
Password: student

You can create further users when the program is running under the menu for the administrative user.

There are two inheritance hierarchies used within the provided code, a hierarchy representing different types of users, and a separate hierarchy representing different catalog items.

The MenuBuilder class defines what items will appear in each of the menus presented to the user. The User Management allows for different user objects to be created: an Admin user, a Student user, and a Staff user. Describe, step by step, how the application creates these objects and obtains the information required for their attributes.

#region Catalogue Management Menu

public static void CatalogueManagementMenu(Users.User usr, out ExtendedResult result)
{
    SortedList<string, MenuOption> menu = new SortedList<string, MenuOption>();
    menu.Add("B", new MenuOption("B", "Add new book", new MenuHandler(AddBookHandler)));
    menu.Add("P", new MenuOption("P", "Add new periodical", new MenuHandler(AddPeriodicalHandler)));
    menu.Add("X", new MenuOption("X", "Exit", new MenuHandler(LogoutHandler)));
    result = new ExtendedResult(ResultCode.SubMenu, menu);
}

#endregion

#region User Management Handlers

private static void AddUserHandler(Users.User usr, out ExtendedResult result)
{
    SortedList<string, MenuOption> menu = new SortedList<string, MenuOption>();
    menu.Add("A", new MenuOption("A", "Administrator", new MenuHandler(AdministratorHandler)));
    menu.Add("M", new MenuOption("M", "Staff Member", new MenuHandler(StaffMemberHandler)));
    menu.Add("S", new MenuOption("S", "Student", new MenuHandler(StudentHandler)));
    menu.Add("X", new MenuOption("X", "Go Back", new MenuHandler(GoBackHandler)));

    result = new ExtendedResult(ResultCode.SubMenu, menu);
}

The creation of a user is done by invoking the AutoPrompt.Create<>() generic function, with the particular user indicated inside the <> (Users.Admin, Users.Staff, or Users.Student).

The Create<>() function creates one of the new objects (represented by 'datatype') then passes this new object by reference to the CreateWorker<>() function along with an indication of its datatype (typeof(datatype)).

public static datatype Create<datatype>() where datatype : new()
{
    datatype dt = new datatype();
    CreateWorker<datatype>(ref dt, typeof(datatype));
    return dt;
}

The CreateWorker<>() function

  1. Immediately returns if the data type indicated is type object, otherwise recursively calls itself, passing the same object through but instead indicating the next ancestor class type (base type). Thus, in the hierarchy of types passed to CreateWorker, the first type that will actually be processed is the data type that is directly derived from type object, then the next child class, and so on, until the original type is reached.
  2. For the current type, a foreach loop is used to examine each of the NonPublic (i.e., private or protected), Insance members (i.e., non-static members)
    1. If the type is a Field (i.e., attribute)
      • Attempt to retrieve the AutoPrompt code attribute, if there is no such code attribute then move onto the next member identified by the loop at (b) above.
      • Retrieve the prompt specified in the AutoPrompt code attribute
      • Depending on the data type (the large if/else-if statement), prompt the user for the appropriate data and then store it into the object (fi.SetValue stores the value).
private static void CreateWorker<datatype>(ref datatype obj, Type t)
{
    if (t == typeof(object))
    return;
        CreateWorker<datatype>(ref obj, t.BaseType);

        foreach (MemberInfo mi in t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance))
        {
          if (mi.MemberType == MemberTypes.Field)
            {
              AutoPrompt ap = Attribute.GetCustomAttribute(mi, typeof(AutoPrompt)) as AutoPrompt;
              if (ap == null)
                continue;

        FieldInfo fi = mi as FieldInfo;
        string fieldPrompt = ap.PromptText;

Points of Interest

There are two inheritance hierarchies used within the provided code, a hierarchy representing different types of users, and a separate hierarchy representing different catalog items.

History

  • November 16, 2007

License

This article, along with any associated source code and files, is licensed under Microsoft Reciprocal License


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat license is this code using? Pin
Mehran_M25-Aug-16 7:40
Mehran_M25-Aug-16 7:40 
Questiondatabase Pin
Member 1061689122-Feb-14 2:36
Member 1061689122-Feb-14 2:36 
Questiondatabase Pin
JYLong1-May-12 11:28
JYLong1-May-12 11:28 
Questionlibrary management system Pin
palakash2-Dec-11 20:55
palakash2-Dec-11 20:55 
please give me location of database with password
id: classicakash1990@gmail.com
Questionlibrary catalog system in c# Pin
palakash2-Dec-11 20:52
palakash2-Dec-11 20:52 
QuestionDatabase Pin
jerome_ash0829-Nov-11 3:15
jerome_ash0829-Nov-11 3:15 
GeneralLMS Pin
vishal3007865-Jul-10 0:49
vishal3007865-Jul-10 0:49 
GeneralMy vote of 1 Pin
asim hanif15-May-10 23:59
asim hanif15-May-10 23:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.