Love Calculator


This is an application Used to calculate the Degree of Love.

1. LoveServer.asmx

<%@ Webservice Language="C#" class="LoveCalculator" %>
using System ;
using System.Web.Services ;
using System.Data;
using System.IO ;
//Only Public classes are exposed to the clients. All the classes that
//you want to expose to the Web Service should derive from
//"System.Web.Services.WebService"
//In my example the class "LoveCalculator" derives from the class
//"System.Web.Services.WebService"
public class LoveCalculator : WebService
{
//All the methods you want to expose to the Web Service should
//also be public methods.
//To make the method available to the Clients you have to mark the
//method as "[WebMethod]"
//only the marked methods will be available to the clients
//This method has two Input parameters as a String containing the name1,name2
// Sign for which the client wants the Love Result.
[WebMethod]
public string GetResults(string nm1,string nm2)
{
//We give a call to our private method which does all the data collection and sends We return the data to the client
return getCount(nm1,nm2) ;
}
//A Private method which takes the "Name1","Name2" as input and returns the Result
private string getCount(string fname,string sname)
{
try
{
string first = fname.ToUpperCase();
int firstlength = fname.Length();
string second = sname.ToUpperCase();
int secondlength = sname.Length();
int LoveCount=0;
for (int Count=0; Count < firstlength; Count++)
{
string letter1=first.substring(Count,Count+1);
if (letter1.Equals("A")) LoveCount+=2;
if (letter1.Equals("E")) LoveCount+=2;
if (letter1.Equals("I")) LoveCount+=2;
if (letter1.Equals("O")) LoveCount+=2;
if (letter1.Equals("U")) LoveCount+=3;
if (letter1.Equals("A")) LoveCount+=1;
if (letter1.Equals("E")) LoveCount+=3;
}
for (int Count=0; Count < secondlength; Count++)
{
string letter2=second.substring(Count,Count+1);
if (letter2.Equals("A")) LoveCount+=2;
if (letter2.Equals("E")) LoveCount+=2;
if (letter2.Equals("I")) LoveCount+=2;
if (letter2.Equals("O")) LoveCount+=2;
if (letter2.Equals("U")) LoveCount+=3;
if (letter2.Equals("A")) LoveCount+=1;
if (letter2.Equals("E")) LoveCount+=3;
}
int amount=0;
if (LoveCount> 0) amount= 5-((firstlength+secondlength)/2);
if (LoveCount> 2) amount= 10-((firstlength+secondlength)/2);
if (LoveCount> 4) amount= 20-((firstlength+secondlength)/2);
if (LoveCount> 6) amount= 30-((firstlength+secondlength)/2);
if (LoveCount> 8) amount= 40-((firstlength+secondlength)/2);
if (LoveCount>10) amount= 50-((firstlength+secondlength)/2);
if (LoveCount>12) amount= 60-((firstlength+secondlength)/2);
if (LoveCount>14) amount= 70-((firstlength+secondlength)/2);
if (LoveCount>16) amount= 80-((firstlength+secondlength)/2);
if (LoveCount>18) amount= 90-((firstlength+secondlength)/2);
if (LoveCount>20) amount=100-((firstlength+secondlength)/2);
if (LoveCount>22) amount=110-((firstlength+secondlength)/2);
if (firstlength==0 || secondlength==0) amount= 0;
if (amount < 0) amount= 0;
if (amount >99) amount=99;
return amount.ToString();
}
catch(Exception ex)
{
//catch any error and inform the user a error has occurred
//in the Web Service
return "Read Error!!"+ex.ToString() ;
}
}
}

2. ClientLove.aspx

<%@ Import namespace="System" %>
<%@ Import namespace="LoveCalc" %>
<%@ Page Language="C#" %>
<html>
<
head>
<
script language="C#" runat="server">
private void Calculate_Click(object sender, EventArgs e)
{
//Get the Selected Item from the Text
string name1 = love1.Text ;
string name2= love2.Text;
//Create a Instance of the Proxy Class
LoveCalculator lc = new LoveCalculator() ;
//Call the "GetResults" method of the Web Service on the Proxy Object
//The Proxy object inturn communicates to the Web Service
//Remember the Proxy cannot do anything except act as a bridge between
//your Web Service and the client. It cannot replace the Web Service.
string result =lc.GetResults(name1,name2) ;
show.InnerHtml="<b> Result for Love Pairs("+name1+","+name2+") ="+result+"%</b>" ;
}
</script>
<
title>Love Calculator</title>
</
head>
<
body>
<
center>
<
form runat="server" bgcolor='black' ID="Form2">
<
table border="1" width="60%" cellpadding="1" cellspacing="2" bgcolor="#FFCAFF">
<
tr>
<
td colspan=2 align='center' bgcolor="#000000"> <b><font color="#FFCAFF"> Love Calculator</font></b></th>
</
tr>
<
tr>
<
td>
Name #1 <input type='Text' id="love1" runat="server" NAME="love1">
</
td>
<
td>
Name #2 <input type='Text' id="love2" runat="server" NAME="love2">
</
td>
</
tr>
<
tr>
<
td colspan="2" bgcolor="#000000" align='right'>
<
input type='Button' onClick="Calculate()" value="Results" runat="server" ID="Button1" NAME="Button1"/></td>
</
tr>
<tr>
<
td colspan="2"><div id="show" runat="server" /></td>
</
tr>
</
table>
</
form>
</
center>
</
body>
</
html>

3. SharpClient.Cs

/* the Love Calculator Console Client
Compilation
csc /r:System.dll;System.Web.Services.dll;LoveCalc.dll SharpClient.cs
*/
using System ;
using LoveCalculator ;
//A class which consumes the Web Service
public class SharpClient
{
public static void Main(string[] argv)
{
Console.WriteLine("Love Calculator") ;
Console.Write("Enter Name #1:") ;
//Read the Input from the user
string nm1 = Console.ReadLine() ;
Console.Write("Enter Name #2:") ;
//Read the Input from the user
string nm2 = Console.ReadLine() ;
//Create a instance of the Proxy Class
LoveCalculator lc = new LoveCalculator() ;
//Make a Call on the Web Service Method "GetResults" and
//pass the names to it
string result = lc.GetResults(nm1,nm2) ;
Console.WriteLine("Love Result :"+result) ;
Console.WriteLine("Press Enter to Exit") ;
Console.ReadLine() ;
}


Compile Section

:/> WebServiceUtil /c:proxy /pa:http://localhost/myservice/LoveServer.asmx?SDL /n:LoveCalc
:/>csc /target:library /r:System.dll;System.Web.Service.dll;Serialization.dll  LoveCalculator.cs
:/>csc /r:System.dll;System.Web.Services.dll;LoveCalculator.dll  SharpClient.cs

Deploy the Web Application

To deploy the above Web Page is similar to deploying any ASP.NET Page. Copy the File ClientLove.aspx to your Virtual Directory. Also remember to copy the Proxy Dll to your "Bin" directory located in the Virtual Directory. If a "/bin" directory does not exist then create it in your Virtual Directory . Wasn't that easy to consume the Web Service. Also since the Input is returned in the form of a string you can format that data to suit to your needs.


Similar Articles