Friday 31 May 2013

How to make simple cricket application in C#

How to make simple cricket application in C# 

Create a class that keeps the updated cricket score in terms of runs, wickets and overs, and has a method that calculates the run-rate and saves it in a separate field. Serialize this class but do not serialize the run-rate field.
Create an application that allows user to display the current score and the run-rate and also allows updating the serializable fields. If running for the first time, when no score is available, the user should be able to input the current score.
Test your application using both types of Formatters, Binary and SOAP for serializing the object. Observe and attach the generated .bin and .xml files



Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;

namespace lab10
{

    public partial class Form1 : Form
    {
        cricket obj = new cricket(0, 0, 0.00);

        string file_loc = "c:\\myfile.soap";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            if (File.Exists(file_loc))
            {
                Stream file_read = File.OpenRead(file_loc);
                SoapFormatter deser = new SoapFormatter();
               obj= (cricket)deser.Deserialize(file_read);
                file_read.Close();
            }

            tb_runs.Text = obj.runs.ToString();
            tb_overs.Text = obj.overs.ToString();
            tb_wicket.Text = obj.wicket.ToString();
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            obj.runs = Convert.ToInt32(tb_runs.Text);
            obj.overs = Convert.ToInt32(tb_overs.Text);
            obj.wicket = Convert.ToInt32(tb_wicket.Text);

            FileStream fs = File.Create(file_loc);
            SoapFormatter ser = new SoapFormatter();
            ser.Serialize(fs, obj);
            fs.Close();
          
        }

        private void cal_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(tb_runs.Text);
            double b = Convert.ToDouble(tb_overs.Text);


            double s= obj.runrate(a,b);
            MessageBox.Show(s.ToString());
            tb_run_rate.Text = s.ToString();
        }
    }
    [Serializable()]
    public class cricket
    {
        public int runs;
        public int wicket;
        public double overs;
        public double runrate(int r,double o)
        {
           if(o==0)
           {
               return 0;
               }

           
            return ((double)r / o);
        }
        public cricket(int rr, int ww, double ov)
        {
            runs = rr;
            wicket = ww;
            overs = ov;
        }
    }

}

Screen Shot

No comments:

Post a Comment