Többszörös kivétel fogadás

Filed Under (kód) by nameless on 02-08-2009

Többszörös kivétel fogadás

using System;
 
namespace proba
{
 
    #region abstract radio osztaly
 
    class Radio
    {
        public void TurnRadio(bool on)
        {
            if (on)
                Console.WriteLine("Megy a zene");
            else
                Console.WriteLine("nem megy a zene");
        }
    }
 
    #endregion
 
 
    #region Car osztaly
 
    class Car
    {
        int maxspeed;
        int currspeed;
 
 
        public Car() { }
        public Car(int max, int curr, bool radioOnOff)
        {
            maxspeed = max;
            currspeed = curr;
            var radio = new Radio();
            radio.TurnRadio(radioOnOff);
        }
 
        public void Acceleration(int acc)
        {
            if (currspeed < maxspeed)
            {
                currspeed += acc;
                Console.WriteLine("Acceleration: {0}", acc);
            }
            else
            {
 
                throw new ArgumentOutOfRangeException("Elerte az auto a maximalis sebesseget! Nem mehetsz gyorsabban!");
                currspeed = maxspeed;
            }
 
        }
 
        public void PrintStateInfo()
        {
            Console.WriteLine("Max speed: {0}", maxspeed);
            Console.WriteLine("Current speed: {0}", currspeed);
 
        }
 
    }
 
    #endregion
 
 
    #region
 
    class MainClass
    {
        public static void Main()
        {
            var rnd = new Random();
            var myCar = new Car(180, 50, true);
 
            try
            {
                for (int i = 0; i < 10; i++)
                {
                    myCar.Acceleration(rnd.Next(19, 24));
                    myCar.PrintStateInfo();
                }
            }
            catch (ArgumentOutOfRangeException ex) //Előszor a pontosabb kivételfogást kell létrehoznunk, és csak utána azt ami mindetn megfog
            {                                                                            //ugyanis ha azt előreraknánk akkor semmi esély nem lene lefutni a prontosabb megfogalmazásnak
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadKey();
        }
    }
 
    #endregion
}

Post a comment