A Simple Online Voting System

only for RuBoard

Online polls have become popular and are fun, so in this section, you're going to create an online voting system.

Design Objectives

Because of the need to support both an XHTML and a WML front-end to the system, and because the system's clients will be a mix of desktop- and mobile phone-based browsers, you'll use XML and XSLT to provide a common UI and a common experience.

The actual vote questions need to be stored in an easy-to-maintain file format that non-technical people can use. For this reason, XML will serve as the storage mechanism.

The voting system UI must support the following functionalities:

  • Easy update of questions used for polls.

  • The user must be able to select any vote question that he or she wants to answer.

  • The system must record and display the total number of votes per question and per vote item.

  • Votes are in the following format:

    • Question text

    • Response text 1

    • Response text 2

For example:

Which is your favorite Swiss Army product?

Pocket knife

Watch

Perfume

Apparel

Pens

Now that you have a set of system requirements and made a few choices about the implementation, look at the diagram of the application's UI flowchart (see Figure 13.1).

Figure 13.1. The application flowchart.
graphics/13fig01.gif

In Figure 13.1, you can see that the UI starts by showing a list of available vote polls and allows the user to select one. After a selection is made, the user is presented with the actual poll question and the items on which to vote. The user can also return to the vote list if they desire .

If the user decides to vote by selecting a vote option, the system records the vote data and displays a brief message that thanks the user for his vote. It then allows the user to view the current statistics for the vote question on which they just voted.

The Vote Statistics screen simply shows the vote question, the total number of votes made for all options, and a breakdown of each vote item. The user also has the opportunity to return to the list of votes from this card.

How can you do this by using XML? First, you must define the XML data file and how you're going to use it.

The XML File

What do you know about the data? It's a poll (or there can be a list of polls), each poll has a question, and each question has a list of items on which to vote.

The following list shows the general data hierarchy:

  • Polls

    • Question

      • Item 1

      • Item 2

      • Question

From here, it is simple to see a basic XML structure, as follows :

 <polls>       <question>            <item> </item>       </question>  </polls> 

For your needs, however, you must add some data. Listing 13.5 shows the appropriate XML structure. For clarity, a sample poll question's data is shown in the code.

Listing 13.5 polls.xml
 <polls>    <poll text="What is your favourite Swiss Army product">      <item text="Pocket knife" votes="4" />      <item text="Watch" votes="0" />      <item text="Perfume" votes="2" />      <item text="Apparel" votes="0" />      <item text="Pens" votes="1" />    </poll>    <poll text="What size computer monitor are you using">      <item text="15 inch" votes="1" />      <item text="17 inch" votes="0" />      <item text="20+ inch" votes="0" />      <item text="Less than 15 inch." votes="0" />    </poll>    <poll text="Where do you get most of your world news">      <item text="Television" votes="1" />      <item text="Radio" votes="0" />      <item text="Daily newspaper" votes="0" />      <item text="Internet sites" votes="0" />      <item text="Word of mouth" votes="0" />      <item text="None of the above" votes="1" />    </poll>    <poll text="Do you think computer hacking should be considered terrorism">      <item text="Yes, hacking is a form of terrorism." votes="0" />      <item text="No, hacking is a crime but not terrorism." votes="0" />      <item text="No, hacking is just a prank that tests vulnerabilities." votes="1" />    </poll>    <poll text="When shopping for blue jeans are you more likely to">      <item text="Buy the name brand even though it is more expensive." votes="0" />      <item text="Buy a generic/unknown brand that is cheaper." votes="0" />      <item text="Wait for the name brand to go on sale." votes="0" />    </poll>    <poll text="How do you get to work">      <item text="Personal automobile" votes="0" />      <item text="Public transit" votes="0" />      <item text="Bicycle" votes="0" />      <item text="Motorcycle" votes="0" />      <item text="Walk" votes="0" />      <item text="Don't work/work at home." votes="0" />    </poll>    <poll text="What do you read first in a newspaper">      <item text="Front page stories" votes="0" />      <item text="Entertainment" votes="0" />      <item text="Comics" votes="0" />      <item text="Sports" votes="0" />      <item text="Business" votes="0" />      <item text="Classified" votes="0" />      <item text="Births/Deaths" votes="0" />    </poll>    <poll text="Do you own or use a Global Positioning System (GPS) receiver">      <item text="Yes, I have a GPS receiver." votes="0" />      <item text="No, I don't have one." votes="0" />      <item text="I don't have one and don't know what it is." votes="0" />    </poll>    <poll text="When was the last time you played a 33 RPM record">      <item text="Have never played one." votes="1" />      <item text="Played one this week." votes="0" />      <item text="It has been months." votes="0" />      <item text="Last played a vinyl record years ago." votes="0" />      <item text="Don't have one to play." votes="0" />    </poll>    <poll text="How many TV do you own">      <item text="None. I don't watch TV." votes="0" />      <item text="One." votes="1" />      <item text="Two." votes="1" />      <item text="Three." votes="1" />      <item text="Too many." votes="0" />    </poll>    <poll text="Which PDA is most appealing to you">      <item text="Palm" votes="0" />      <item text="Pocket PC" votes="1" />    </poll>  </polls> 

If you look at the code in Listing 13.5, you can see that there's a root element of <polls> , which contains a collection of <poll> elements. A <poll> element has a text attribute that holds the question text to be displayed. Inside the <poll> attribute are a collection of <item> elements, each with two attributes ( text and votes ) . The text attribute holds the text label of a reply to a vote question, while the votes attribute holds a value that represents the total amount of votes for a vote reply.

This XML is used as both the basis of the voting system's UI and as its storage mechanism.

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net