0

Код для DeviceData.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyaTask4
{
    public class DeviceData
    {
        public DateTime Time { get; set; }
        public double Temperature { get; set; }
        public int Pulse { get; set; }
        public string Pressure { get; set; }

        public override string ToString()
        {
            return $"Время: {Time}, Темп: {Temperature}, Пульс: {Pulse}, Давление: {Pressure}";
        }
    }

}

Код для Form1.cs


namespace AnyaTask4
{
    public partial class Form1 : Form
    {
        private List<Patient> patients = new List<Patient>();
        private string dataDirectory = "";
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        public Form1()
        {
            InitializeComponent();

            timer.Interval = 1000;
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void UpdatePatientList()
        {
            lstPatients.Items.Clear();
            foreach (var p in patients)
            {
                string status = p.CurrentData != null ? p.CurrentData.ToString() : "Нет данных";
                lstPatients.Items.Add($"{p.FullName} [Device number: {p.DeviceId}] - {status}");
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            foreach (var p in patients)
            {
                string filePath = Path.Combine(dataDirectory, $"{p.DeviceId}.dat");
                if (File.Exists(filePath))
                {
                    var lines = File.ReadAllLines(filePath);
                    var data = new DeviceData();

                    foreach (var line in lines)
                    {
                        var parts = line.Split('=');
                        if (parts.Length != 2)
                        {
                            continue;
                        }
                        if (parts[0].Equals("Время"))
                        {
                            if (DateTime.TryParse(parts[1], out var time))
                            {
                                data.Time = time;
                            }
                        }
                        else if (parts[0].Equals("Температура"))
                        {
                            if (double.TryParse(parts[1], out var temp))
                            {
                                data.Temperature = temp;
                            }
                        }
                        else if (parts[0].Equals("Пульс"))
                        {
                            if (int.TryParse(parts[1], out var pulse))
                            {
                                data.Pulse = pulse;
                            }
                        }
                        else if (parts[0].Equals("Давление"))
                        {
                            data.Pressure = parts[1];
                        }
                    }

                    p.CurrentData = data;
                }
            }

            UpdatePatientList();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (int.TryParse(txtDeviceId.Text, out int id))
            {
                patients.Add(new Patient { FullName = txtName.Text, DeviceId = id });
                UpdatePatientList();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            foreach (Patient pat in patients)
            {
                if (pat.FullName == txtName.Text && int.TryParse(txtDeviceId.Text, out int id))
                {
                    pat.DeviceId = id;
                    pat.CurrentData = null;
                    UpdatePatientList();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            patients.RemoveAll(p => p.FullName == txtName.Text);
            UpdatePatientList();
        }

        private void btnSelectDir_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dlg = new FolderBrowserDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                    dataDirectory = dlg.SelectedPath;
            }
        }

        private void btnSaveToFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter sw = new StreamWriter(dlg.FileName))
                {
                    foreach (var p in patients)
                    {
                        sw.WriteLine($"{p.FullName};{p.DeviceId};{p.CurrentData?.Time};{p.CurrentData?.Temperature};{p.CurrentData?.Pulse};{p.CurrentData?.Pressure}");
                    }
                }
            }
        }
    }
}

Код для Form1.Designer.cs


namespace AnyaTask4
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            lstPatients = new ListBox();
            txtName = new TextBox();
            txtDeviceId = new TextBox();
            btnAdd = new Button();
            btnEdit = new Button();
            btnDelete = new Button();
            btnSelectDir = new Button();
            btnSaveToFile = new Button();
            label1 = new Label();
            label2 = new Label();
            SuspendLayout();
            //
            // lstPatients
            //
            lstPatients.FormattingEnabled = true;
            lstPatients.Location = new Point(41, 51);
            lstPatients.Name = "lstPatients";
            lstPatients.Size = new Size(679, 184);
            lstPatients.TabIndex = 0;
            //
            // txtName
            //
            txtName.Location = new Point(172, 268);
            txtName.Name = "txtName";
            txtName.Size = new Size(125, 27);
            txtName.TabIndex = 1;
            //
            // txtDeviceId
            //
            txtDeviceId.Location = new Point(172, 298);
            txtDeviceId.Name = "txtDeviceId";
            txtDeviceId.Size = new Size(125, 27);
            txtDeviceId.TabIndex = 2;
            //
            // btnAdd
            //
            btnAdd.Location = new Point(50, 342);
            btnAdd.Name = "btnAdd";
            btnAdd.Size = new Size(94, 29);
            btnAdd.TabIndex = 3;
            btnAdd.Text = "Добавить";
            btnAdd.UseVisualStyleBackColor = true;
            btnAdd.Click += btnAdd_Click;
            //
            // btnEdit
            //
            btnEdit.Location = new Point(150, 342);
            btnEdit.Name = "btnEdit";
            btnEdit.Size = new Size(94, 29);
            btnEdit.TabIndex = 4;
            btnEdit.Text = "Изменить";
            btnEdit.UseVisualStyleBackColor = true;
            btnEdit.Click += btnEdit_Click;
            //
            // btnDelete
            //
            btnDelete.Location = new Point(250, 342);
            btnDelete.Name = "btnDelete";
            btnDelete.Size = new Size(94, 29);
            btnDelete.TabIndex = 5;
            btnDelete.Text = "Удалить";
            btnDelete.UseVisualStyleBackColor = true;
            btnDelete.Click += btnDelete_Click;
            //
            // btnSelectDir
            //
            btnSelectDir.Location = new Point(362, 343);
            btnSelectDir.Name = "btnSelectDir";
            btnSelectDir.Size = new Size(131, 29);
            btnSelectDir.TabIndex = 6;
            btnSelectDir.Text = "Выбрать папку";
            btnSelectDir.UseVisualStyleBackColor = true;
            btnSelectDir.Click += btnSelectDir_Click;
            //
            // btnSaveToFile
            //
            btnSaveToFile.Location = new Point(362, 378);
            btnSaveToFile.Name = "btnSaveToFile";
            btnSaveToFile.Size = new Size(131, 29);
            btnSaveToFile.TabIndex = 7;
            btnSaveToFile.Text = "Сохранить";
            btnSaveToFile.UseVisualStyleBackColor = true;
            btnSaveToFile.Click += btnSaveToFile_Click;
            //
            // label1
            //
            label1.AutoSize = true;
            label1.Location = new Point(117, 271);
            label1.Name = "label1";
            label1.Size = new Size(49, 20);
            label1.TabIndex = 8;
            label1.Text = "ФИО";
            //
            // label2
            //
            label2.AutoSize = true;
            label2.Location = new Point(54, 301);
            label2.Name = "label2";
            label2.Size = new Size(112, 20);
            label2.TabIndex = 9;
            label2.Text = "ID устройства";
            //
            // Form1
            //
            AutoScaleDimensions = new SizeF(8F, 20F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(732, 450);
            Controls.Add(label2);
            Controls.Add(label1);
            Controls.Add(btnSaveToFile);
            Controls.Add(btnSelectDir);
            Controls.Add(btnDelete);
            Controls.Add(btnEdit);
            Controls.Add(btnAdd);
            Controls.Add(txtDeviceId);
            Controls.Add(txtName);
            Controls.Add(lstPatients);
            Name = "Form1";
            Text = "Пациенты";
            ResumeLayout(false);
            PerformLayout();
        }


        #endregion

        private ListBox lstPatients;
        private TextBox txtName;
        private TextBox txtDeviceId;
        private Button btnAdd;
        private Button btnEdit;
        private Button btnDelete;
        private Button btnSelectDir;
        private Button btnSaveToFile;
        private Label label1;
        private Label label2;
    }
}

Код для Patient.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyaTask4
{
    public class Patient
    {
        public string FullName { get; set; }
        public int DeviceId { get; set; }
        public DeviceData CurrentData { get; set; }
    }

}

CC BY-SA 4.0
Новый участник
Anna — новый участник сайта. Будьте снисходительны, задавая уточняющие вопросы, комментируя и отвечая. Почитайте про нормы поведения.
1

0

Ваш ответ