Advertisement
Anonim_999

blyaaYaEbal

Mar 29th, 2023
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.83 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8.  
  9. //namespace WindowsFormsApp5
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         private Car[] _cars;
  14.         private CarsData _carsData;
  15.         private BinaryFormatter _binaryFormatter;
  16.         private const string PATH = "CARS.dat";
  17.         private Car _tempCar;
  18.        
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.             _tempCar = new Car("","","","");
  23.             _binaryFormatter = new BinaryFormatter();
  24.             _cars = new Car[0];
  25.             _carsData = new CarsData(_cars);
  26.         }
  27.  
  28.         private void Form1_Load(object sender, EventArgs e)
  29.         {
  30.             this.Text = DateTime.Now.ToString();
  31.            
  32.             if (File.Exists(PATH))
  33.             {
  34.                 using (FileStream fileStream = new FileStream(PATH,FileMode.OpenOrCreate))
  35.                 {
  36.                     _carsData = (CarsData)_binaryFormatter.Deserialize(fileStream);
  37.                     _cars = _carsData._cars;
  38.  
  39.                     foreach (string color in _carsData._colors)
  40.                         comboBox1.Items.Add(color);
  41.                 }
  42.             }
  43.             else
  44.             {
  45.                 for (int i = 0; i < 6; i++)
  46.                     comboBox1.Items.Add((Color)i);
  47.             }
  48.             ClearData();
  49.         }
  50.  
  51.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  52.         {
  53.             textBox3.Text = comboBox1.Text;
  54.         }
  55.  
  56.         private void button1_Click(object sender, EventArgs e)
  57.         {
  58.             bool isFound = false;
  59.            
  60.             foreach (Car car in _cars)
  61.             {
  62.                 if (car.StateNumber == _tempCar.StateNumber && car != _tempCar)
  63.                 {
  64.                     isFound = true;
  65.                 }
  66.             }
  67.  
  68.             if (isFound == false)
  69.             {
  70.                 _tempCar.UpdateElements(textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text);
  71.             }
  72.  
  73.             string[] _colors = new string[0];
  74.            
  75.             foreach (var item in comboBox1.Items)
  76.                 Add(ref _colors,item.ToString());
  77.             _carsData.SetData(_cars, _colors);
  78.  
  79.             using (FileStream fileStream = new FileStream(PATH, FileMode.OpenOrCreate))
  80.                 _binaryFormatter.Serialize(fileStream,_carsData);
  81.             groupBox1.Visible = false;
  82.             ClearData();
  83.         }
  84.  
  85.         private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  86.         {
  87.             groupBox1.Visible = true;
  88.            
  89.             foreach (Car car in _cars)
  90.             {
  91.                 if (car.StateNumber == comboBox2.SelectedItem.ToString())
  92.                 {
  93.                     FillData(car);
  94.                 }
  95.             }
  96.         }
  97.  
  98.         private void FillData(Car car)
  99.         {
  100.             textBox1.Text = car.StateNumber;
  101.             textBox2.Text = car.Model;
  102.             textBox3.Text = car.Color;
  103.             textBox4.Text = car.FullName;
  104.         }
  105.  
  106.         private void button3_Click(object sender, EventArgs e)
  107.         {
  108.             foreach (Car car in _cars.ToArray())
  109.                 if (car.StateNumber.Contains(comboBox2.SelectedItem.ToString()))
  110.                     RemoveCar(ref _cars, car);
  111.             comboBox2.Items.Remove(comboBox2.SelectedItem);
  112.         }
  113.  
  114.         private void RemoveCar(ref Car[] cars, Car car)
  115.         {
  116.             Car[] tempCars = new Car[cars.Length-1];
  117.  
  118.             for (int i = 0; i < cars.Length; i++)
  119.                 if (cars[i] != car)
  120.                     tempCars[i] = cars[i];
  121.             cars = tempCars;
  122.         }
  123.        
  124.         private void button2_Click(object sender, EventArgs e)
  125.         {
  126.             ClearData();
  127.         }
  128.  
  129.         private void ClearData()
  130.         {
  131.             textBox1.Text = "";
  132.             textBox2.Text = "";
  133.             textBox3.Text = "";
  134.             textBox4.Text = "";
  135.             comboBox2.Items.Clear();
  136.             comboBox2.Text = "";
  137.         }
  138.  
  139.         private void comboBox2_KeyPress(object sender, KeyPressEventArgs e)
  140.         {
  141.             bool isFound = false;
  142.            
  143.             if (e.KeyChar == (char) Keys.Enter)
  144.             {
  145.                 comboBox2.Items.Clear();
  146.                
  147.                 foreach (Car car in _cars.ToArray())
  148.                 {
  149.                     if (car.StateNumber.Contains(comboBox2.Text))
  150.                     {
  151.                         isFound = true;
  152.                         FillData(car);
  153.                         comboBox2.Items.Add(car.StateNumber);
  154.                     }
  155.                 }
  156.  
  157.                 if (isFound == false)
  158.                 {
  159.                     Add(ref _cars,new Car(comboBox2.Text,"","",""));
  160.                     _tempCar = _cars[_cars.Length-1];
  161.                     groupBox1.Visible = true;
  162.                     FillData(_cars[_cars.Length-1]);
  163.                 }
  164.             }
  165.         }
  166.  
  167.         private void Add<T>(ref T[] array, T value)
  168.         {
  169.             T[] tempCars = new T[array.Length + 1];
  170.  
  171.             for (int i = 0; i < array.Length; i++)
  172.                 tempCars[i] = array[i];
  173.             tempCars[tempCars.Length - 1] = value;
  174.             array = tempCars;
  175.         }
  176.  
  177.         private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
  178.         {
  179.             bool isFound = false;
  180.            
  181.             if (e.KeyChar == (char)Keys.Enter)
  182.             {
  183.                 foreach (string color in comboBox1.Items)
  184.                 {
  185.                     if (textBox3.Text == color)
  186.                     {
  187.                         isFound = true;
  188.                         comboBox1.SelectedItem = color;
  189.                     }
  190.                 }
  191.  
  192.                 if (isFound == false)
  193.                 {
  194.                     comboBox1.Items.Add(textBox3.Text);
  195.                     comboBox1.Text = comboBox1.Items[comboBox1.Items.Count-1].ToString();
  196.                 }
  197.             }
  198.         }
  199.  
  200.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  201.         {
  202.             using (FileStream fileStream = new FileStream(PATH,FileMode.OpenOrCreate))
  203.             {
  204.                 _binaryFormatter.Serialize(fileStream,_carsData);
  205.             }
  206.         }
  207.     }
  208.  
  209.     public enum Color
  210.     {
  211.         неопределенный,
  212.         белый,
  213.         красный,
  214.         фиолетовый,
  215.         серый,
  216.         зелёный
  217.     }
  218.  
  219.     [Serializable]
  220.     public class CarsData
  221.     {
  222.         public Car[] _cars;
  223.         public string[] _colors;
  224.  
  225.         public CarsData(Car[] cars)
  226.         {
  227.             _colors = new string[0];
  228.             _cars = cars;
  229.         }
  230.  
  231.         public void SetData(Car[] cars, string[] colors)
  232.         {
  233.             _cars = cars;
  234.             _colors = colors;
  235.         }
  236.     }
  237.    
  238.     [Serializable]
  239.     public class Car
  240.     {
  241.         private string _stateNumber;
  242.         private string _model;
  243.         private string _color;
  244.         private string _fullName;
  245.  
  246.         public string StateNumber => _stateNumber;
  247.         public string Model => _model;
  248.         public string Color => _color;
  249.         public string FullName => _fullName;
  250.  
  251.         public Car(string stateNumber, string model, string color, string fullName)
  252.         {
  253.             _stateNumber = stateNumber;
  254.             _model = model;
  255.             _color = color;
  256.             _fullName = fullName;
  257.         }
  258.  
  259.         public void UpdateElements(string stateNumber, string model, string color, string fullName)
  260.         {
  261.             _stateNumber = stateNumber;
  262.             _model = model;
  263.             _color = color;
  264.             _fullName = fullName;
  265.         }
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement