AviaTickets Booking

mail@pastecode.io avatar
unknown
csharp
2 months ago
7.1 kB
2
Indexable
Never
int selectedIndex = 0;

string[] menu = new string[]
{
    "Book",
    "Settings",
    "Exit"
};

Seat[,] aviaTickets = new Seat[6, 4]
{
    { new Seat("A1", 50), new Seat("A2", 50), new Seat("A3", 50), new Seat("A4", 50) },
    { new Seat("B1", 60), new Seat("B2", 60), new Seat("B3", 60), new Seat("B4", 60) },
    { new Seat("C1", 70), new Seat("C2", 70), new Seat("C3", 70), new Seat("C4", 70) },
    { new Seat("D1", 80), new Seat("D2", 80), new Seat("D3", 80), new Seat("D4", 80) },
    { new Seat("E1", 90), new Seat("E2", 90), new Seat("E3", 90), new Seat("E4", 90) },
    { new Seat("F1", 99), new Seat("F2", 99), new Seat("F3", 99), new Seat("F4", 99) },
};

while (true)
{
    Console.Clear();

    Console.WriteLine("Menu");

    for (int i = 0; i < menu.GetLength(0); i++)
    {
        if (selectedIndex == i)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine($"{i + 1}. {menu[i]} <<<");
        }
        else
        {
            Console.ResetColor();
            Console.WriteLine($"{i + 1}. {menu[i]}");
        }
    }

    

    ConsoleKeyInfo keyInfo = Console.ReadKey();

    switch (keyInfo.Key)
    {
        case ConsoleKey.UpArrow:
            if (selectedIndex > 0)
            {
                selectedIndex--;
            }
            break;

        case ConsoleKey.DownArrow:
            if (selectedIndex < menu.GetLength(0) - 1)
            {
                selectedIndex++;
            }
            break;

        case ConsoleKey.Enter:
            if (selectedIndex == 0)
            {
                Console.Clear();

                ShowSeats();

                Console.ReadKey();
            }
            else if (selectedIndex == 1)
            {
                Console.Clear();
                Console.WriteLine("Here are some settings");
                Console.ReadKey();
            }
            else if (selectedIndex == 2)
            {
                Console.Clear();
                Console.ResetColor();
                return;
            }
            break;
    }

    Console.ResetColor();

}

void ShowSeats()
{
    int selectedIndexY = 0;
    int selectedIndexX = 0;

    while (true)
    {
        Console.Clear();

        Console.WriteLine("Book");

        for (int i = 0; i < aviaTickets.GetLength(0); i++)
        {
            for (int j = 0; j < aviaTickets.GetLength(1); j++)
            {
                if (i == selectedIndexY && j == selectedIndexX)
                {
                    if (aviaTickets[i, j].IsBooked)
                    {
                        Console.ForegroundColor = ConsoleColor.Magenta;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                }
                else if (aviaTickets[i, j].IsBooked)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }
                else
                if (aviaTickets[i, j].IsSelected)
                {
                    Console.ForegroundColor= ConsoleColor.Yellow;
                }
                else
                {
                    Console.ResetColor();
                }

                Console.Write($" [{aviaTickets[i, j].Name}] ");
            }
            Console.WriteLine();
        }

        Console.WriteLine($"Price: {aviaTickets[selectedIndexY, selectedIndexX].Price}");
        
        Console.ResetColor();

        ConsoleKeyInfo keyInfo = Console.ReadKey();
        switch (keyInfo.Key)
        {
            case ConsoleKey.UpArrow:
                {
                    if (selectedIndexY > 0)
                    {
                        selectedIndexY--;
                    }
                }
                break;
            case ConsoleKey.DownArrow:
                {
                    if (selectedIndexY < aviaTickets.GetLength(0) - 1)
                    {
                        selectedIndexY++;
                    }
                }
                break;

            case ConsoleKey.LeftArrow:
                {
                    if (selectedIndexX > 0)
                    {
                        selectedIndexX--;
                    }
                }
                break;
            case ConsoleKey.RightArrow:
                {
                    if (selectedIndexX < aviaTickets.GetLength(1) - 1)
                    {
                        selectedIndexX++;
                    }
                }
                break;
            case ConsoleKey.E:
                    aviaTickets[selectedIndexY, selectedIndexX].IsSelected = !aviaTickets[selectedIndexY, selectedIndexX].IsSelected;
                break;
            case ConsoleKey.Enter:
                {                    
                    while (true)
                    {
                        if (aviaTickets[selectedIndexY, selectedIndexX].IsBooked)
                        {
                            Console.WriteLine("Занято");
                            Console.WriteLine("Нажмите любую клавишу для продолжения");
                            Console.ReadKey();
                            break;
                        }

                        Console.WriteLine("Для подтверждения нажмите ENTER клавишу");

                        if (Console.ReadKey().Key == ConsoleKey.Enter)
                        {
                            aviaTickets[selectedIndexY, selectedIndexX].IsSelected = true;

                            foreach (var ticket in aviaTickets)
                            {
                                if (ticket.IsSelected)
                                {
                                    ticket.IsBooked = true;
                                }                                
                            }                            
                            break;
                        }
                    }

                }
                break;
        }
        Console.ResetColor();
    }
}

class Seat
{
    public string Name;
    public bool IsBooked;
    public bool IsSelected;
    public int Price;

    public Seat(string name, int price)
    {
        Name = name;
        Price = price;
    }
}

/*string ConfirmationSeat()
{
    while (true)
    {
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Are you sure you want to book that seat?");
        Console.ResetColor();

        string confirm = Console.ReadLine();

        switch (confirm)
        {
            case "yes":
                return confirm;
            case "no":
                return
            default:
                Console.WriteLine();
                break;
        }
    }
}
*/
Leave a Comment