Object-Oriented Programming ideas from the production of a public opinion survey (I)

Source: Internet
Author: User

When a general web programmer just switched to. net or jsp, the idea of programming often failed.

From the object-oriented perspective, the business logic is mixed with the page html code. Once the page prototype changes

The program must also be modified, resulting in low reusability of the Code. The biggest improvement of asp.net or jsp over asp is object-oriented,

The code is highly reusable. As a typical web program, it is generally divided into three layers: Ideal, business layer, and data layer.

And page display layer. The following is an example of public opinion survey.
Let's take a look at what a public opinion survey should consider from the object-oriented perspective. First, in terms of data

A public opinion survey should have a subject, then there should be several items, and finally there should be users who participate in the survey and

Check the start time and end time. Secondly, the public opinion survey method is very simple. A voting method is followed by the parties that display the survey results.

Method. With the above knowledge, we can construct a survey class like this:

Namespace MyClass. Util
{
Using System;
Using System. Collections;
Using System. Drawing;
Using MyClass. Util;

/// <Summary>
/// A common survey class
/// </Summary>
Public class Survey: object
{
/// <Summary>
/// Survey No.
/// </Summary>
/// <Remarks>
/// Varchar type, 20 bytes in the database
/// </Remakrs>
Protected string m_strID;

/// <Summary>
/// Survey title
/// </Summary>
Protected string m_strTitle;

/// <Summary>
/// Survey Start Time
/// </Summary>
Protected DateTime m_datBeginTime;

/// <Summary>
/// Investigation deadline
/// </Summary>
Protected DateTime m_datEndTime;

/// <Summary>
/// Clicks
/// </Summary>
/// <Remarks>
/// Number of visitors
/// </Remarks>
Protected int m_intHits;

/// <Summary>
/// Survey items
/// </Summary>
Protected ArrayList m_arrItems;

// Attributes
/// <Summary>
/// Survey title
/// </Summary>
Public string Title
{
Get
{
Return m_strTitle;
}
Set
{
M_strTitle = value;
}
}

/// <Summary>
/// Total clicks
/// </Summary>
Public int Hits
{
Get
{
Return m_intHits;
}
Set
{
M_intHits = 0;
}
}

/// <Summary>
/// Investigation start time attribute
/// </Summary>
Public DateTime BeginTime
{
Get
{
Return m_datBeginTime;
}
Set
{
M_datBeginTime = value;
}
}

/// <Summary>
/// Investigation deadline attribute
/// </Summary>
Public DateTime EndTime
{
Get
{
Return m_datEndTime;
}
Set
{
M_datEndTime = value;
}
}

/// <Summary>
/// Survey item set
/// </Summary>
/// <Remarks> is a set of SurveyItem classes. </remarks>
Public ArrayList Items
{
Get
{
Return m_arrItems;
}
Set
{
M_arrItems = value;
}
}

/// <Summary>
/// Survey No.
/// </Summary>
Public string SurveyID
{
Get
{
Return m_strID;
}
Set
{
M_strID = value;
}
}


/// <Summary>
/// Constructor
/// </Summary>
Public Survey ()
{
//
// TODO: Add Constructor Logic here
//
M_strTitle = "\";
M_arrItems = new ArrayList ();
}


/// <Summary>
/// Overload Constructor
/// </Summary>
/// <Param name = "a_strTitle"> survey title </param>
/// <Remarks> applicable to surveys without deadline </remarks>
Public Survey (string a_strTitle)
{
M_strTitle = a_strTitle;
M_datBeginTime = DateTime. Today;
M_datEndTime = DateTime. Today;
M_arrItems = new ArrayList ();
}

/// <Summary>
/// Overload Constructor
/// </Summary>
/// <Param name = "a_strTitle"> survey title </param>
/// <Param name = "a_datBeginTime"> Start time </param>
/// <Param name = "a_datEndTime"> end time </param>
/// <Remarks> applicable to surveys with a deadline </remarks>
Public Survey (string a_strTitle, DateTime a_datBeginTime, DateTime

A_datEndTime)
{
M_strTitle = a_strTitle;
M_datBeginTime = a_datBeginTime;
M_datEndTime = a_datEndTime;
}

/// <Summary>
/// Add survey items
/// </Summary>
/// <Param name = "a_objItem"> </param>
Public void AddItem (object a_objItem)
{
If (a_objItem is SurveyItem)
{
M_arrItems.Add (a_objItem );
}
}

/// <Summary>
/// Virtual function
/// </Summary>
/// <Param name = "a_intID"> id </param>
Public virtual void Vote (int a_intID)
{

}
/// <Summary>
/// Save the virtual function to the database
/// </Summary>
/// <Param name = "a_objItem"> </param>
Public virtual void SaveItem (object a_objItem)
{

}


/// <Summary>
/// Virtual function, save the investigation to the database
/// </Summary>
Public virtual void SaveToDatabase (string m_strSurveyID)
{
Throw (new Exception ("basic functions cannot be used directly "));
}

/// <Summary>
/// Obtain the survey items from the database
/// </Summary>
/// <Param name = "a_intID"> id of the corresponding database </param>
Public virtual void LoadFromDatabase (string a_strID)
{

}

/// <Summary>
/// Search for survey items
/// </Summary>
/// <Param name = "a_intID"> survey item No. </param>
Public SurveyItem GetItem (int a_intID)
{
If (a_intID> 0 & a_intID <m_arrItems.Count)
{
Return (SurveyItem) (m_arrItems [a_intID]);
}
Else
{
Throw (new Exception ("Inspector subscript out of bounds "));
}
}


/// <Summary>
/// Search for survey items
/// </Summary>
/// <Param name = "a_strText"> survey item title </param>
/// <Remarks> locate the survey item based on the title and return its serial number. Otherwise,-1 is returned. </remarks>
Public int IndexOf (string a_strText)
{
For (int I = 0; I <m_arrItems.Count; I ++)
{
If (a_strText = (SurveyItem) m_arrItems [I]). Text)
{
Return I;
}
}

Return-1;
}

/// <Summary>
/// Search for survey items
/// </Summary>
/// <Param name = "a_intID"> survey item No. </param>
/// <Remarks>
/// Search for the survey item based on the survey item number. If no. is found,-1 is returned. </remarks>
Public int IndexOf (int a_intID)
{
For (int I = 0; I <m_arrItems.Count; I ++)
{
If (a_intID = (SurveyItem) m_arrItems [I]). ID)
{
Return I;
}
}

Return-1;
}


/// <Summary>
/// Display the result
/// </Summary>
/// <Param name = "a_strFileName"> Save location of the generated image </param>
Public void CreateResultImage (string a_strFileName, MyChart. ChartType

A_intChartType,
Int a_intWidth, int

A_intHeight, Color a_objBackColor)
{

// Define a color array
ArrayList ItemColors = new ArrayList ();
ItemColors. Add (Color. Red );
ItemColors. Add (Color. Black );
ItemColors. Add (Color. Blue );
ItemColors. Add (Color. DeepSkyBlue );
ItemColors. Add (Color. Firebrick );
ItemColors. Add (Color. Orange );
ItemColors. Add (Color. Green );
ItemColors. Add (Color. WhiteSmoke );
ItemColors. Add (Color. Tan );
ItemColors. Add (Color. DarkSeaGreen );
ItemColors. Add (Color. Fuchsia );
ItemColors. Add (Color. Goldenrod );

MyChart myChart = new MyChart (m_strTitle, a_objBackColor,

A_intWidth,
A_intHeight, a_intChartType

, "Persons ");
For (int I = 0; I <m_arrItems.Count; I ++)
{
// Survey item
SurveyItem item = (SurveyItem) m_arrItems [I];

// Chart items
MyChart. AddItem (new ChartItem (item. Text, item. Count,

(Color) ItemColors [I]);

}

Try
{
MyChart. Create (a_strFileName );
}
Catch (Exception e)
{
Throw (new Exception (e. ToString ()));
}

}

}

/// <Summary>
/// Survey item category
/// </Summary>
Public class SurveyItem: object
{

/// <Summary>
/// Call

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.