Answer to Question #318693 in C# for madhu

Question #318693

Create an console application to book train tickets. Create a Passanger class with (Name, Age) and write a function called TicketBooking(no_of_tickets) that takes no.of tickets to be booked. If the no of tickets is > 2 per booking, raise an user defined exception, and print "cannot book more than 2 tickets". Else Print "Ticket Booked Successfully". Add a Test class to call TicketBooking method by accepting all required details.


1
Expert's answer
2022-03-27T04:37:54-0400
internal class Program
    {
        class Passanger
        {
            public string Name { get; set; }
            public int Age { get; set; }
            Ticket Ticket { get; set; }
            public Passanger(string name , int age)
            {
                Name = name;
                Age = age;
                Ticket = new Ticket();
            }
            public void TicketBooking(int tickets)
            {
                Ticket.Tickets = tickets;
                Console.WriteLine("Ticket Booked Successfully");
            }
        }


        class Ticket
        {
            public int tickets;
            public int Tickets
            {
                get => tickets;
                set
                {
                    if (value > 2)
                        throw new Exception("Cannot book more than 2 tickets");
                    else
                        tickets = value;
                }
            }




        }
        static void Main(string[] args)
        {
            Passanger passanger = new Passanger("Bob", 25);
            Console.Write("Enter the number of tickets you want to book:");
            passanger.TicketBooking(int.Parse(Console.ReadLine()));
            Console.ReadKey();
        }


    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS