Загрузка данных


using System;
using System.Drawing;
using System.Windows.Forms;

namespace RocketApp
{
    // ====================== ИНТЕРФЕЙС ======================
    public interface IRocket
    {
        void DisplayInfo();
    }

    // ====================== БАЗОВЫЙ КЛАСС ======================
    public abstract class Rocket : IRocket
    {
        public string Name { get; protected set; }
        public string Manufacturer { get; protected set; }
        public double PayloadToLEO { get; protected set; }
        public string Sound { get; protected set; }
        public string FlightDescription { get; protected set; }

        // ASCII-рисунок ракеты (будет переопределяться в наследниках)
        public virtual string GetRocketArt()
        {
            return "";
        }

        public virtual void DisplayInfo()
        {
            string info = GetRocketArt() + "\n" +
                $"Ракета: {Name}\n" +
                $"Производитель: {Manufacturer}\n" +
                $"Полезная нагрузка: {PayloadToLEO} тонн\n" +
                $"Ракета издаёт звук: {Sound}\n" +
                $"Ракета летит: {FlightDescription}\n\n";

            AppendText(info);
        }

        protected void AppendText(string text)
        {
            if (Application.OpenForms.Count > 0 && Application.OpenForms[0] is Form1 form)
            {
                if (form.Controls["txtOutput"] is TextBox txt)
                {
                    txt.AppendText(text);
                    txt.ScrollToCaret();
                }
            }
        }
    }

    // ====================== FALCON 9 ======================
    public class Falcon9 : Rocket
    {
        public Falcon9()
        {
            Name = "Falcon 9";
            Manufacturer = "SpaceX";
            PayloadToLEO = 22.8;
            Sound = "РРРРРРРРР!!! (рев двигателей Merlin)";
            FlightDescription = "мощный вертикальный старт с возвращаемой первой ступенью";
        }

        public override string GetRocketArt()
        {
            return
                "       ▲\n" +
                "      / \\\n" +
                "     /   \\\n" +
                "    /  F9 \\\n" +
                "   /_______\\\n" +
                "   |  □  |\n" +
                "   |_____|\n" +
                "     |||||\n" +
                "     |||||\n\n";
        }
    }

    // ====================== STARSHIP ======================
    public class Starship : Rocket
    {
        public Starship()
        {
            Name = "Starship";
            Manufacturer = "SpaceX";
            PayloadToLEO = 150;
            Sound = "ГРОМОВОЙ РЁВ Raptor-ов!!!";
            FlightDescription = "полностью многоразовая сверхтяжёлая ракета к Марсу";
        }

        public override string GetRocketArt()
        {
            return
                "       ▲\n" +
                "      / \\\n" +
                "     / S \\\n" +
                "    /  T  \\\n" +
                "   /   A   \\\n" +
                "  /    R    \\\n" +
                " /     S     \\\n" +
                "/      H      \\\n" +
                "  |    I    |\n" +
                "  |    P    |\n" +
                "  |_________|\n" +
                "     |||||\n" +
                "     |||||\n\n";
        }
    }

    // ====================== ПРОТОН-М ======================
    public class ProtonM : Rocket
    {
        public ProtonM()
        {
            Name = "Протон-М";
            Manufacturer = "Роскосмос";
            PayloadToLEO = 23;
            Sound = "МОЩНЫЙ ГУЛ РД-276!!!";
            FlightDescription = "классическая тяжёлая ракета на гиперголе";
        }

        public override string GetRocketArt()
        {
            return
                "       ▲\n" +
                "      / \\\n" +
                "     / П \\\n" +
                "    /  Р  \\\n" +
                "   /   О   \\\n" +
                "  /    Т    \\\n" +
                " /     О     \\\n" +
                "/      Н      \\\n" +
                "  |    М    |\n" +
                "  |_________|\n" +
                "     |||||\n" +
                "     |||||\n\n";
        }
    }

    // ====================== ГЛАВНАЯ ФОРМА ======================
    public partial class Form1 : Form
    {
        private Falcon9 falcon;
        private Starship starship;
        private ProtonM proton;

        public Form1()
        {
            this.Text = "Лабораторная работа - Полиморфизм";
            this.Size = new Size(920, 720);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.BackColor = Color.WhiteSmoke;

            CreateControls();

            falcon = new Falcon9();
            starship = new Starship();
            proton = new ProtonM();
        }

        private void CreateControls()
        {
            // Кнопки
            Button btnFalcon = new Button
            {
                Text = "Falcon 9",
                Location = new Point(30, 40),
                Size = new Size(220, 50),
                Font = new Font("Segoe UI", 11, FontStyle.Bold)
            };

            Button btnStarship = new Button
            {
                Text = "Starship",
                Location = new Point(30, 110),
                Size = new Size(220, 50),
                Font = new Font("Segoe UI", 11, FontStyle.Bold)
            };

            Button btnProton = new Button
            {
                Text = "Протон-М",
                Location = new Point(30, 180),
                Size = new Size(220, 50),
                Font = new Font("Segoe UI", 11, FontStyle.Bold)
            };

            Button btnPolymorphism = new Button
            {
                Text = "Демонстрация полиморфизма",
                Location = new Point(30, 270),
                Size = new Size(220, 55),
                Font = new Font("Segoe UI", 11, FontStyle.Bold),
                BackColor = Color.LightSkyBlue
            };

            // Поле вывода
            TextBox txtOutput = new TextBox
            {
                Name = "txtOutput",
                Multiline = true,
                ScrollBars = ScrollBars.Vertical,
                Location = new Point(280, 30),
                Size = new Size(600, 630),
                Font = new Font("Consolas", 10.5f),
                BackColor = Color.Black,
                ForeColor = Color.LimeGreen,
                ReadOnly = true
            };

            // События кнопок
            btnFalcon.Click += (s, e) => { ClearAndShowHeader("Falcon 9"); falcon.DisplayInfo(); };
            btnStarship.Click += (s, e) => { ClearAndShowHeader("Starship"); starship.DisplayInfo(); };
            btnProton.Click += (s, e) => { ClearAndShowHeader("Протон-М"); proton.DisplayInfo(); };
            btnPolymorphism.Click += (s, e) => DemonstratePolymorphism();

            this.Controls.Add(btnFalcon);
            this.Controls.Add(btnStarship);
            this.Controls.Add(btnProton);
            this.Controls.Add(btnPolymorphism);
            this.Controls.Add(txtOutput);
        }

        private void ClearAndShowHeader(string rocketName)
        {
            if (this.Controls["txtOutput"] is TextBox txt)
            {
                txt.Clear();
                txt.AppendText($"=== ЗАПУСК РАКЕТЫ {rocketName.ToUpper()} ===\n\n");
            }
        }

        private void DemonstratePolymorphism()
        {
            if (this.Controls["txtOutput"] is TextBox txt)
            {
                txt.Clear();
                txt.AppendText("=== ДЕМОНСТРАЦИЯ ПОЛИМОРФИЗМА ===\n");
                txt.AppendText(new string('=', 55) + "\n\n");

                Rocket[] rockets = { falcon, starship, proton };

                foreach (var rocket in rockets)
                {
                    rocket.DisplayInfo();
                    txt.AppendText(new string('-', 55) + "\n\n");
                }
            }
        }
    }

    // ====================== ЗАПУСК ======================
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}