Thursday, April 10, 2014

Q 1 CHAPTER 8 ACTIVITY BOOK EXCEPTION

Question 1:-
David wants to create a ticket booking application for a movie theater. The application should ask the user for his choice, whether he wants to book the tickets or not.
 The application should also ask the user for the total number of tickets to be booked. While booking the tickets, if the total number of booked tickets exceeds the available tickets,
 the application should raise an exception and display an appropriate message. Hint: The total number of available tickets is 10.

Ans:-

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

namespace ConsoleApplication9
{
    class TicketLimit
    {
        static void Main(string[] args)
        {
            Summer Summer = new Summer();
            try
            {
                Summer.CalculateTicket();
            }
            catch (TicketLimt e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
    public class TicketLimt : ApplicationException
    {
        public TicketLimt(string massage)
            : base(massage)
        {

        }
    }
    public class Summer
    {
        int TotalTickets = 10;
        int count = 0;
        char choice = 'Y';
        public void CalculatetICKET()
        {
            while (true)
            {
                Console.WriteLine("dO you want to book the tickets(Y/N)");
                choice = Convert.ToChar(Console.ReadLine());
                if (choice == 'Y' || choice == 'Y')
                {
                    Console.WriteLine("Only {0} tickets are available", TotalTickets);
                    Console.WriteLine("enter the number of tickets you want to book");
                    count = Convert.ToInt32(Console.ReadLine());
                    if (count > TotalTickets)
                        throw (new TicketLimt("Tickets not available"));
                    else
                        TotalTickets -= count;
                }
                else
                    break;
            }
        }
    }
}

No comments:

Post a Comment