Загрузка данных
using System;
namespace Stolovaya
{
class Program
{
// Названия блюд
static string[] names = { "Борщ", "Пельмени", "Салат" };
// Количество порций каждого блюда
static int[] quantity = { 5, 0, 10 };
// Цены
static int[] price = { 80, 120, 60 };
static void Main()
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
// Показываем меню
Console.WriteLine("=== МЕНЮ СТОЛОВОЙ ===");
for (int i = 0; i < names.Length; i++)
{
string status = quantity[i] == 0 ? "нет в наличии" : quantity[i] + " шт.";
Console.WriteLine("ID: " + i + " | " + names[i] + " | " + price[i] + " лей | " + status);
}
int id = 0;
int amount = 0;
// ===== ИСКЛЮЧЕНИЕ №1: FormatException =====
// Если пользователь введёт буквы вместо числа
try
{
Console.Write("\nВведите ID блюда: ");
string buf = Console.ReadLine();
id = int.Parse(buf);
}
catch (FormatException e)
{
Console.WriteLine("\n=== FormatException ===");
Console.WriteLine("Message: " + e.Message);
Console.WriteLine("Source: " + e.Source);
Console.WriteLine("TargetSite: " + e.TargetSite);
Console.WriteLine("\nEnter для выхода...");
Console.ReadLine();
return;
}
// ===== ИСКЛЮЧЕНИЕ №2: IndexOutOfRangeException =====
// Если ввести ID, которого нет в массиве (например, 99)
try
{
// Проверяем существование блюда через обращение к массиву
string dishName = names[id]; // ← бросит IndexOutOfRangeException
int available = quantity[id];
int dishPrice = price[id];
if (available == 0)
{
Console.WriteLine("\nБлюдо «" + dishName + "» закончилось!");
Console.ReadLine();
return;
}
Console.Write("Сколько порций? ");
amount = int.Parse(Console.ReadLine()); // ← снова FormatException
if (amount <= 0)
{
Console.WriteLine("\nКоличество должно быть больше нуля!");
Console.ReadLine();
return;
}
if (amount > available)
{
Console.WriteLine("\nВ наличии только " + available + " шт.!");
Console.ReadLine();
return;
}
quantity[id] = available - amount;
int total = amount * dishPrice;
Console.WriteLine("\nЗаказ принят: " + dishName + " x" + amount + " = " + total + " лей");
Console.WriteLine("Осталось: " + quantity[id] + " шт.");
}
catch (FormatException e)
{
Console.WriteLine("\n=== FormatException ===");
Console.WriteLine("Message: " + e.Message);
Console.WriteLine("Source: " + e.Source);
Console.WriteLine("TargetSite: " + e.TargetSite);
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("\n=== IndexOutOfRangeException ===");
Console.WriteLine("Message: " + e.Message);
Console.WriteLine("Source: " + e.Source);
Console.WriteLine("TargetSite: " + e.TargetSite);
Console.WriteLine("StackTrace: " + e.StackTrace);
}
Console.WriteLine("\nEnter для выхода...");
Console.ReadLine();
}
}
}