Код для Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnyaTask11
{
public partial class Form1 : Form
{
public List<Product> products = new List<Product>();
public List<Ingridient> Ingridients = new List<Ingridient>();
private double b = 0;
private double j = 0;
private double u = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK )
{
string[] lines = File.ReadAllLines(ofd.FileName);
Product product = new Product();
product.Name = lines[0];
product.Jiri = double.Parse(lines[1]);
product.Belki = double.Parse(lines[2]);
product.Uglevodi = double.Parse(lines[3]);
products.Add(product);
listBox1.Items.Clear();
listBox1.Items.AddRange(products.ToArray());
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem is Product product)
{
label2.Text = product.Name;
textBox1.Text = product.Belki.ToString();
textBox2.Text = product.Jiri.ToString();
textBox3.Text = product.Uglevodi.ToString();
}
}
private void button3_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem is Product product)
{
if (double.TryParse(textBox1.Text, out double belki))
{
product.Belki = belki;
}
if (double.TryParse(textBox2.Text, out double jiri))
{
product.Jiri = jiri;
}
if (double.TryParse(textBox3.Text, out double uglevodi))
{
product.Uglevodi = uglevodi;
}
listBox1.Items.Clear();
listBox1.Items.AddRange(products.ToArray());
}
}
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem is Product product)
{
products.Remove(product);
listBox1.Items.Clear();
listBox1.Items.AddRange(products.ToArray());
}
}
private void button4_Click(object sender, EventArgs e)
{
if (double.TryParse(textBox4.Text, out double gramm) && gramm > 0 && listBox1.SelectedItem is Product product)
{
Ingridient ingridient = new Ingridient();
ingridient.Product = product;
ingridient.Grami = gramm;
Ingridients.Add(ingridient);
listBox2.Items.Clear();
listBox2.Items.AddRange(Ingridients.ToArray());
ObnovitBJU();
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox2.SelectedItem is Ingridient i)
{
label7.Text = i.Product.ToString();
textBox5.Text = i.Grami.ToString();
}
}
private void button5_Click(object sender, EventArgs e)
{
if (listBox2.SelectedItem is Ingridient i)
{
Ingridients.Remove(i);
listBox2.Items.Clear();
listBox2.Items.AddRange(Ingridients.ToArray());
ObnovitBJU();
}
}
private void button6_Click(object sender, EventArgs e)
{
if (listBox2.SelectedItem is Ingridient i)
{
if (double.TryParse(textBox5.Text, out double grami))
{
i.Grami = grami;
}
listBox2.Items.Clear();
listBox2.Items.AddRange(Ingridients.ToArray());
ObnovitBJU();
}
}
public void ObnovitBJU()
{
b = 0;
j = 0;
u = 0;
foreach (var item in Ingridients)
{
b += item.Product.Belki * item.Grami / 100;
j += item.Product.Jiri * item.Grami / 100;
u += item.Product.Uglevodi * item.Grami / 100;
}
label14.Text = b.ToString();
label13.Text = j.ToString();
label12.Text = u.ToString();
}
private void button7_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName))
{
sw.WriteLine(textBox6.Text);
foreach (var item in Ingridients)
{
sw.WriteLine($"{item.Product.Name}={item.Grami}");
}
sw.WriteLine(b);
sw.WriteLine(j);
sw.WriteLine(u);
}
}
}
}
}
Код для Form1.Designer.cs
namespace AnyaTask11
{
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()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.button5 = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.button6 = new System.Windows.Forms.Button();
this.label8 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.button7 = new System.Windows.Forms.Button();
this.label15 = new System.Windows.Forms.Label();
this.textBox6 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 16;
this.listBox1.Location = new System.Drawing.Point(12, 12);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(218, 260);
this.listBox1.TabIndex = 0;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// listBox2
//
this.listBox2.FormattingEnabled = true;
this.listBox2.ItemHeight = 16;
this.listBox2.Location = new System.Drawing.Point(429, 44);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(217, 260);
this.listBox2.TabIndex = 1;
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(11, 371);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(218, 23);
this.button1.TabIndex = 2;
this.button1.Text = "Загрузить из файла";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(11, 400);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(218, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Удалить";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(313, 41);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 22);
this.textBox1.TabIndex = 4;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(236, 44);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(47, 16);
this.label1.TabIndex = 5;
this.label1.Text = "Белки";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(239, 12);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(44, 16);
this.label2.TabIndex = 6;
this.label2.Text = "label2";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(236, 82);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(45, 16);
this.label3.TabIndex = 8;
this.label3.Text = "Жиры";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(313, 79);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 22);
this.textBox2.TabIndex = 7;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(236, 121);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(71, 16);
this.label4.TabIndex = 10;
this.label4.Text = "Углеводы";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(313, 115);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(100, 22);
this.textBox3.TabIndex = 9;
//
// button3
//
this.button3.Location = new System.Drawing.Point(237, 154);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(176, 23);
this.button3.TabIndex = 11;
this.button3.Text = "Сохранить";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(12, 278);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(217, 23);
this.button4.TabIndex = 12;
this.button4.Text = "Добавить в блюдо";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(74, 307);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(155, 22);
this.textBox4.TabIndex = 13;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(11, 310);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(57, 16);
this.label5.TabIndex = 14;
this.label5.Text = "Граммы";
//
// button5
//
this.button5.Location = new System.Drawing.Point(429, 310);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(217, 23);
this.button5.TabIndex = 15;
this.button5.Text = "Удалить ингридиент";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(652, 41);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(57, 16);
this.label6.TabIndex = 17;
this.label6.Text = "Граммы";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(715, 38);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(155, 22);
this.textBox5.TabIndex = 16;
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(652, 12);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(44, 16);
this.label7.TabIndex = 18;
this.label7.Text = "label7";
//
// button6
//
this.button6.Location = new System.Drawing.Point(652, 75);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(176, 23);
this.button6.TabIndex = 19;
this.button6.Text = "Сохранить";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(662, 249);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(71, 16);
this.label8.TabIndex = 22;
this.label8.Text = "Углеводы";
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(662, 210);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(45, 16);
this.label9.TabIndex = 21;
this.label9.Text = "Жиры";
//
// label10
//
this.label10.AutoSize = true;
this.label10.Location = new System.Drawing.Point(662, 172);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(47, 16);
this.label10.TabIndex = 20;
this.label10.Text = "Белки";
//
// label11
//
this.label11.AutoSize = true;
this.label11.Location = new System.Drawing.Point(662, 137);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(94, 16);
this.label11.TabIndex = 23;
this.label11.Text = "БЖУ в блюде";
//
// label12
//
this.label12.AutoSize = true;
this.label12.Location = new System.Drawing.Point(742, 249);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(0, 16);
this.label12.TabIndex = 26;
//
// label13
//
this.label13.AutoSize = true;
this.label13.Location = new System.Drawing.Point(742, 210);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(0, 16);
this.label13.TabIndex = 25;
//
// label14
//
this.label14.AutoSize = true;
this.label14.Location = new System.Drawing.Point(742, 172);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(0, 16);
this.label14.TabIndex = 24;
//
// button7
//
this.button7.Location = new System.Drawing.Point(429, 342);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(217, 23);
this.button7.TabIndex = 27;
this.button7.Text = "Сохранить блюдо";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// label15
//
this.label15.AutoSize = true;
this.label15.Location = new System.Drawing.Point(430, 15);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(118, 16);
this.label15.TabIndex = 29;
this.label15.Text = "Название блюда";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(554, 12);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(92, 22);
this.textBox6.TabIndex = 28;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(932, 450);
this.Controls.Add(this.label15);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.button7);
this.Controls.Add(this.label12);
this.Controls.Add(this.label13);
this.Controls.Add(this.label14);
this.Controls.Add(this.label11);
this.Controls.Add(this.label8);
this.Controls.Add(this.label9);
this.Controls.Add(this.label10);
this.Controls.Add(this.button6);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.button5);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.TextBox textBox6;
}
}
Код для Ingridient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnyaTask11
{
public class Ingridient
{
public Product Product;
public double Grami;
public override string ToString()
{
return $"{Product.Name} = {Grami} гр";
}
}
}
Код для Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnyaTask11
{
public class Product
{
public string Name;
public double Jiri;
public double Belki;
public double Uglevodi;
public override string ToString()
{
return $"{Name} - Б {Belki}, Ж {Jiri}, У {Uglevodi}";
}
}
}