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


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; }

        public virtual void DisplayInfo()
        {
            string info = 
                $"Ракета: {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();
                }
            }
        }
    }

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

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

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

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

        public Form1()
        {
            this.Text = "Лабораторная работа - Полиморфизм";
            this.Size = new Size(900, 680);
            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(590, 590),
                Font = new Font("Consolas", 11),
                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('=', 50) + "\n\n");

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

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

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