Код для Form1.cs
namespace Task_1
{
public partial class Form1 : Form
{
public Dictionary<string, Zayavka> spisok;
private Dictionary<string, int> globalItems = new Dictionary<string, int>();
public Form1()
{
spisok = new Dictionary<string, Zayavka>();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
DialogResult dialogResult = openFileDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
string[] line = File.ReadAllLines(openFileDialog.FileName);
string NameOtdel = line[0];
if (spisok.ContainsKey(NameOtdel))
{
MessageBox.Show("Заявка уже была обработана");
return;
}
Zayavka zayavka = new Zayavka();
zayavka.NameOtdel = NameOtdel;
for (int i = 1; i < line.Length; i++)
{
string[] parts = line[i].Split("-");
string NameTovar = parts[0];
int TovarCount = int.Parse(parts[1]);
zayavka.tovari[NameTovar] = TovarCount;
if (globalItems.ContainsKey(NameTovar))
globalItems[NameTovar] += TovarCount;
else
globalItems[NameTovar] = TovarCount;
}
spisok[zayavka.NameOtdel] = zayavka;
RefreshOtdelList();
RefreshGlobalList();
}
}
private void RefreshOtdelList()
{
listBox1.Items.Clear();
listBox1.Items.AddRange(spisok.Keys.ToArray());
}
private void RefreshGlobalList()
{
listBox3.Items.Clear();
foreach (var item in globalItems)
{
listBox3.Items.Add($"{item.Key} - {item.Value}");
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox2.Items.Clear();
if (listBox1.SelectedItem is string otdel)
{
var items = spisok[otdel].tovari;
foreach (var item in items)
{
listBox2.Items.Add($"{item.Key} - {item.Value}");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(sfd.FileName))
{
foreach (var item in globalItems)
{
sw.WriteLine($"{item.Key} - {item.Value}");
}
}
}
}
}
}
Код для Form1.Designer.cs
namespace Task_1
{
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()
{
button1 = new Button();
listBox1 = new ListBox();
listBox2 = new ListBox();
listBox3 = new ListBox();
button2 = new Button();
label1 = new Label();
label2 = new Label();
label3 = new Label();
SuspendLayout();
//
// button1
//
button1.Location = new Point(356, 267);
button1.Margin = new Padding(3, 2, 3, 2);
button1.Name = "button1";
button1.Size = new Size(173, 22);
button1.TabIndex = 0;
button1.Text = "Добавить заявку";
button1.UseVisualStyleBackColor = true;
button1.Click += button1_Click;
//
// listBox1
//
listBox1.FormattingEnabled = true;
listBox1.ItemHeight = 15;
listBox1.Location = new Point(22, 55);
listBox1.Margin = new Padding(3, 2, 3, 2);
listBox1.Name = "listBox1";
listBox1.Size = new Size(204, 184);
listBox1.TabIndex = 1;
listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
//
// listBox2
//
listBox2.FormattingEnabled = true;
listBox2.ItemHeight = 15;
listBox2.Location = new Point(245, 55);
listBox2.Margin = new Padding(3, 2, 3, 2);
listBox2.Name = "listBox2";
listBox2.Size = new Size(193, 169);
listBox2.TabIndex = 2;
//
// listBox3
//
listBox3.FormattingEnabled = true;
listBox3.ItemHeight = 15;
listBox3.Location = new Point(457, 55);
listBox3.Margin = new Padding(3, 2, 3, 2);
listBox3.Name = "listBox3";
listBox3.Size = new Size(193, 169);
listBox3.TabIndex = 3;
//
// button2
//
button2.Location = new Point(535, 267);
button2.Margin = new Padding(3, 2, 3, 2);
button2.Name = "button2";
button2.Size = new Size(153, 22);
button2.TabIndex = 4;
button2.Text = "Сохранить заявки";
button2.UseVisualStyleBackColor = true;
button2.Click += button2_Click;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(22, 28);
label1.Name = "label1";
label1.Size = new Size(49, 15);
label1.TabIndex = 5;
label1.Text = "Отделы";
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(245, 28);
label2.Name = "label2";
label2.Size = new Size(100, 15);
label2.TabIndex = 6;
label2.Text = "Заявки от отдела";
//
// label3
//
label3.AutoSize = true;
label3.Location = new Point(457, 28);
label3.Name = "label3";
label3.Size = new Size(65, 15);
label3.TabIndex = 7;
label3.Text = "Все заявки";
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(700, 338);
Controls.Add(label3);
Controls.Add(label2);
Controls.Add(label1);
Controls.Add(button2);
Controls.Add(listBox3);
Controls.Add(listBox2);
Controls.Add(listBox1);
Controls.Add(button1);
Margin = new Padding(3, 2, 3, 2);
Name = "Form1";
Text = "Form1";
Load += Form1_Load;
ResumeLayout(false);
PerformLayout();
}
#endregion
private Button button1;
private ListBox listBox1;
private ListBox listBox2;
private ListBox listBox3;
private Button button2;
private Label label1;
private Label label2;
private Label label3;
}
}
Код для Zayavka.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Task_1
{
public class Zayavka
{
public string NameOtdel { get; set; }
public Dictionary<string, int> tovari = new Dictionary<string, int>();
}
}