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.
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();
}
}
Comments
Leave a comment