stackoverflow1453

Programming&&Games&&Life

Archive for October, 2016

Flower Stock Control v2 (Correct one)- Events and Delegates

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StokTakip
{
    public class Product
    {
        public string name;
        private int quantity;

        public Product(string a, int m)
        {
            name = a;
            quantity = m;
        }

        public int Quantity
        {
            get { return quantity; }
            set
            {
                quantity = value;
                if (quantity < 20 & StockDecreased != null)
                {
                    StockDecreased();
                }
            }
        }

        public override string ToString()
        {
            return "Name: " + this.name + " Stock Quantity: " + this.quantity;
        }

        public delegate void FlowerEventHandler();
        public event FlowerEventHandler StockDecreased;
    }
}

///////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StokTakip
{
    class Program
    {
        static void FlowerStockDecreased()
        {
            Console.WriteLine("Flower stock decreased");
        }

        static void Main(string[] args)
        {
            Product flower = new Product("Flower", 50);
            Console.WriteLine(flower);

            flower.StockDecreased += new Product.FlowerEventHandler(FlowerStockDecreased);

            while (true)
            {
                Console.WriteLine("What is your choice");
                Console.WriteLine("[1] Stock entry quantity ");
                Console.WriteLine("[2] Stock exit quantity: ");

                int choice = Convert.ToInt32(Console.ReadLine());

                if (choice == 1)
                {
                    Console.Write("Enter stock entry quantity: ");
                    flower.Quantity += Convert.ToInt32(Console.ReadLine());
                }

                else if (choice == 2)
                {
                    Console.Write("Enter stock exit quantity: ");
                    flower.Quantity -= Convert.ToInt32(Console.ReadLine());
                }

                Console.WriteLine(flower);
            }
        }
    }
}

stoxxxx

Flower Stock Control v1- Events and Delegates

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StokTakip
{
    public class Product
    {
        public string name;
        public int quantity;

        public Product(string a, int m)
        {
            name = a;
            quantity = m;
        }       

        public override string ToString()
        {
            return "Name: "+ this.name + " Stock Quantity: " + this.quantity;
        }

        public delegate void FlowerEventHandler();
        public event FlowerEventHandler StockDecreased;

        public void RaiseStockDecreased()
        {
            if (StockDecreased != null)
                StockDecreased();
        }        
    }
}


///////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StokTakip
{
    class Program
    {
        static void FlowerStockDecreased()
        {
            Console.WriteLine("Flower stock decreased");
        }

        static void Main(string[] args)
        {
            Product flower = new Product("Flower", 50);
            Console.WriteLine(flower);

            flower.StockDecreased += new Product.FlowerEventHandler(FlowerStockDecreased);

            while (true)
            {
                Console.WriteLine("What is your choice");
                Console.WriteLine("[1] Stock entry quantity ");
                Console.WriteLine("[2] Stock exit quantity: ");

                int choice = Convert.ToInt32(Console.ReadLine());

                if (choice == 1)
                {
                    Console.Write("Enter stock entry quantity: ");
                    flower.quantity += Convert.ToInt32(Console.ReadLine());

                }

                else if (choice == 2)
                {
                    Console.Write("Enter stock exit quantity: ");
                    flower.quantity -= Convert.ToInt32(Console.ReadLine());
                }

                Console.WriteLine(flower);

                if (flower.quantity < 20)
                {
                    flower.RaiseStockDecreased();  //????             
                }
            }
        }
    }
}

Static Event

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication63
{
    class Program
    {
        public delegate void eventhandler();
        public static event eventhandler hayvanlar;  //static event!

        static void Main(string[] args)
        {
            hayvanlar += new eventhandler(Cat);
            hayvanlar += new eventhandler(Dog);

            //p.hayvanlar();
            hayvanlar();
        }

        static void Cat()
        {
            Console.WriteLine("Cat");
        }

        static void Dog()
        {
            Console.WriteLine("DOg");
        }

    }
}

delegate 6-4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateTest4
{
    public class Cihaz
    {
        private delegate void Temsilci();
        Temsilci t;

        public Cihaz()
        {
            t = new Temsilci(Ac);
        }

        public void Kontrol()
        {
            t();
        }       

        public void Ac()
        {
            Console.WriteLine("Cihaz acildi.");
            t = Kapa;
        }

        public void Kapa()
        {
            Console.WriteLine("Cihaz kapandi.");
            t = Ac;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace DelegateTest4
{
    class Program
    {
        static void Main(string[] args)
        {
            Cihaz ch = new Cihaz();
            for (int i = 0; i < 7; i++)
            {
                ch.Kontrol();
                Thread.Sleep(1000);
            }
            Console.ReadLine();
        }
    }
}

delegate 6-3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateTest3
{
    class Methods
    {
        public int Topla(int x, int y) { return x + y; }
        public int Fark(int x, int y) { return x - y; }
    }

    class Program
    {
        public delegate int Temsilcim(int x, int y);

        static void Main(string[] args)
        {
            Methods m = new Methods();
            Temsilcim t = new Temsilcim(m.Topla);
            Console.WriteLine("Sonuc: " + t(9, 4));
            //assignment in runtime
            t = m.Fark;

            Console.WriteLine("Sonuc: " + t(9, 4));
        }
    }
}

delegates 6-2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateTest2
{
    class Program
    {
        private delegate int Temsilcim(int sayi1, int sayi2);

        public static int Topla(int x, int y) { return x + y; }
        public static int Fark(int x, int y) { return x - y; }

        static void Main(string[] args)
        {
            Temsilcim isTemsil = new Temsilcim(Topla);
            int toplamSonuc = isTemsil(8, 5);
            Console.WriteLine("Sonuc:{0}", toplamSonuc);

            isTemsil = Fark;
            int farkSonuc = isTemsil(8, 5);
            Console.WriteLine("Sonuc:{0}", farkSonuc);
        }
    }
}

Delegate 6-1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateTest1
{
    class Program
    {
        //declare delegate (tanımlama)
        public delegate void TestHandler();

        public static void Metod1()
        {
            Console.WriteLine("Temsilci ile çağrıldım");
        }

        static void Main(string[] args)
        {
            //instantiate delegate (örnekleme)
            TestHandler test = new TestHandler(Metod1);
            //Invocate delegate (çağırma)
            test();
        }
    }
}