Код для Form1.cs
using System.Text.Json;
using System.Xml.Linq;
namespace AnyaTask5
{
public partial class Form1 : Form
{
private List<Medicine> medicines = new List<Medicine>();
private string filePath = "medicines.txt";
private string departmentName = "Отделение терапии";
public Form1()
{
InitializeComponent();
LoadMedicines();
UpdateMedicineList();
lblDepartment.Text = departmentName;
}
private void LoadMedicines()
{
if (File.Exists(filePath))
{
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{
string[] med = line.Split("-", StringSplitOptions.TrimEntries);
Medicine me = new Medicine();
me.Name = med[0];
me.Quantity = int.Parse(med[1]);
medicines.Add(me);
}
UpdateMedicineList();
}
}
private void SaveMedicines()
{
using (StreamWriter writer = new StreamWriter(filePath))
{
foreach (var med in medicines)
{
writer.WriteLine(med);
}
}
MessageBox.Show("Список лекарств сохранён");
}
private void UpdateMedicineList()
{
listBoxAll.Items.Clear();
listBoxZero.Items.Clear();
foreach (var med in medicines)
{
listBoxAll.Items.Add(med);
if (med.Quantity == 0)
listBoxZero.Items.Add(med);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
if (!int.TryParse(txtQuantity.Text, out int quantity)) return;
var existing = medicines.FirstOrDefault(m => m.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (existing != null)
existing.Quantity += quantity;
else
medicines.Add(new Medicine { Name = name, Quantity = quantity });
UpdateMedicineList();
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (listBoxAll.SelectedItem is Medicine selected)
{
medicines.Remove(selected);
UpdateMedicineList();
}
}
private void btnDecrement_Click(object sender, EventArgs e)
{
if (listBoxAll.SelectedItem is Medicine selected &&
int.TryParse(txtQuantity.Text, out int amount))
{
if (selected.Quantity < amount)
{
MessageBox.Show("Недостаточно лекарств на складе");
return;
}
selected.Quantity -= amount;
UpdateMedicineList();
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (listBoxAll.SelectedItem is Medicine selected)
{
string name = txtName.Text.Trim();
if (int.TryParse(txtQuantity.Text, out int qty))
{
selected.Name = name;
selected.Quantity = qty;
UpdateMedicineList();
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveMedicines();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
SaveMedicines();
}
private void btnChangeDept_Click(object sender, EventArgs e)
{
string newName = Microsoft.VisualBasic.Interaction.InputBox("Введите название отделения", "Изменение", departmentName);
if (!string.IsNullOrWhiteSpace(newName))
{
departmentName = newName;
lblDepartment.Text = departmentName;
}
}
}
}
Код для Form1.Designer.cs
namespace AnyaTask5
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private Label lblDepartment;
private TextBox txtName;
private TextBox txtQuantity;
private ListBox listBoxAll;
private ListBox listBoxZero;
private Button btnAdd;
private Button btnRemove;
private Button btnDecrement;
private Button btnEdit;
private Button btnChangeDept;
private Button btnSave;
private Label lblAll;
private Label lblZero;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
lblDepartment = new Label();
txtName = new TextBox();
txtQuantity = new TextBox();
listBoxAll = new ListBox();
listBoxZero = new ListBox();
btnAdd = new Button();
btnRemove = new Button();
btnDecrement = new Button();
btnEdit = new Button();
btnChangeDept = new Button();
btnSave = new Button();
lblAll = new Label();
lblZero = new Label();
SuspendLayout();
//
// lblDepartment
//
lblDepartment.AutoSize = true;
lblDepartment.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 204);
lblDepartment.Location = new Point(20, 15);
lblDepartment.Name = "lblDepartment";
lblDepartment.Size = new Size(154, 20);
lblDepartment.TabIndex = 0;
lblDepartment.Text = "Название отделения";
//
// txtName
//
txtName.Location = new Point(20, 50);
txtName.Name = "txtName";
txtName.PlaceholderText = "Наименование";
txtName.Size = new Size(150, 27);
txtName.TabIndex = 1;
//
// txtQuantity
//
txtQuantity.Location = new Point(180, 50);
txtQuantity.Name = "txtQuantity";
txtQuantity.PlaceholderText = "Кол-во";
txtQuantity.Size = new Size(80, 27);
txtQuantity.TabIndex = 2;
//
// listBoxAll
//
listBoxAll.FormattingEnabled = true;
listBoxAll.Location = new Point(20, 100);
listBoxAll.Name = "listBoxAll";
listBoxAll.Size = new Size(399, 84);
listBoxAll.TabIndex = 9;
//
// listBoxZero
//
listBoxZero.FormattingEnabled = true;
listBoxZero.Location = new Point(20, 220);
listBoxZero.Name = "listBoxZero";
listBoxZero.Size = new Size(399, 64);
listBoxZero.TabIndex = 10;
//
// btnAdd
//
btnAdd.Location = new Point(270, 50);
btnAdd.Name = "btnAdd";
btnAdd.Size = new Size(149, 27);
btnAdd.TabIndex = 3;
btnAdd.Text = "Добавить";
btnAdd.Click += btnAdd_Click;
//
// btnRemove
//
btnRemove.Location = new Point(20, 310);
btnRemove.Name = "btnRemove";
btnRemove.Size = new Size(91, 34);
btnRemove.TabIndex = 4;
btnRemove.Text = "Удалить";
btnRemove.Click += btnRemove_Click;
//
// btnDecrement
//
btnDecrement.Location = new Point(117, 310);
btnDecrement.Name = "btnDecrement";
btnDecrement.Size = new Size(89, 34);
btnDecrement.TabIndex = 5;
btnDecrement.Text = "Списать";
btnDecrement.Click += btnDecrement_Click;
//
// btnEdit
//
btnEdit.Location = new Point(212, 310);
btnEdit.Name = "btnEdit";
btnEdit.Size = new Size(130, 34);
btnEdit.TabIndex = 6;
btnEdit.Text = "Редактировать";
btnEdit.Click += btnEdit_Click;
//
// btnChangeDept
//
btnChangeDept.Location = new Point(20, 356);
btnChangeDept.Name = "btnChangeDept";
btnChangeDept.Size = new Size(186, 27);
btnChangeDept.TabIndex = 7;
btnChangeDept.Text = "Изменить отделение";
btnChangeDept.Click += btnChangeDept_Click;
//
// btnSave
//
btnSave.Location = new Point(212, 356);
btnSave.Name = "btnSave";
btnSave.Size = new Size(130, 27);
btnSave.TabIndex = 8;
btnSave.Text = "Сохранить";
btnSave.Click += btnSave_Click;
//
// lblAll
//
lblAll.AutoSize = true;
lblAll.Location = new Point(20, 80);
lblAll.Name = "lblAll";
lblAll.Size = new Size(109, 20);
lblAll.TabIndex = 11;
lblAll.Text = "Все лекарства:";
//
// lblZero
//
lblZero.AutoSize = true;
lblZero.Location = new Point(20, 200);
lblZero.Name = "lblZero";
lblZero.Size = new Size(102, 20);
lblZero.TabIndex = 12;
lblZero.Text = "Закончились:";
//
// Form1
//
ClientSize = new Size(438, 400);
Controls.Add(lblDepartment);
Controls.Add(txtName);
Controls.Add(txtQuantity);
Controls.Add(btnAdd);
Controls.Add(btnRemove);
Controls.Add(btnDecrement);
Controls.Add(btnEdit);
Controls.Add(btnChangeDept);
Controls.Add(btnSave);
Controls.Add(listBoxAll);
Controls.Add(listBoxZero);
Controls.Add(lblAll);
Controls.Add(lblZero);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
Name = "Form1";
Text = "Учёт лекарств";
ResumeLayout(false);
PerformLayout();
}
}
}
Код для Medicine.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnyaTask5
{
public class Medicine
{
public string Name { get; set; }
public int Quantity { get; set; }
public override string ToString()
{
return $"{Name} - {Quantity}";
}
}
}