Thursday, November 6, 2014

Exception Class _Lab_11_6_2014


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



namespace Zau_ExceptionHandling_Lab_11_5_14
{
    class CapacityWarningException : Exception
    {
        public CapacityWarningException() : base("You are either trying to add water than its max capacity or drop than its min capacity") { }
        public CapacityWarningException(string message) : base(message) { }
        public CapacityWarningException(string message, Exception innerException) : base(message, innerException) { }
    }
}
//===========================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zau_ExceptionHandling_Lab_11_5_14
{
    public class WaterTank
    {
        private int _currentAmount;
        private static int _maxCapacity;
        private static int _minCapacity;

        public int CurrentAmount
        { get { return _currentAmount; } }

        public static int MaxCapacity
        { get { return _maxCapacity; } }

        public static int MinCapacity
        { get { return _minCapacity; } }

         //constructor
        public WaterTank(int currentGal)
        {
            _currentAmount = currentGal;
        }

        static WaterTank()
        {
            _maxCapacity = 3000;
            _minCapacity = 10;
        }

        public int AddWater(int waterGal)
        {
            if (_currentAmount + waterGal <= _maxCapacity)
            {
                _currentAmount += waterGal;
            }
            else
                throw new CapacityWarningException("Do not add water more!");

            return waterGal;
        }

        public int TakeWater(int waterGal)
        {
            if (_currentAmount + waterGal <= _minCapacity)
            {
                _currentAmount -= waterGal;
            }
            else
                throw new CapacityWarningException("Do not try to take more water!");

            return waterGal;
        }
    }
}
//Lab Assignment:
//New Project
//Define a class WaterTank with the following fields:
//      _maxCapacity,
//      _currentAmount,
//      _minCapacity,
//These fields hold the amount in gallons
//the _maxCapacity and _minCapacity should be static and set to some values
//
//Define a method : AddWater that takes the amount to be added to the tank
//this method should throw a custom exception if you attempt to add more water than its maximum capacity

//Define a method: TakeWater that takes the amount of water taken from the tank
//this method should throw a custome exception if you the current level drops may drop below mincapacity.

No comments:

Post a Comment