Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Windows.Forms;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Collections;
- using System.Collections.Generic;
- //namespace WindowsFormsApp5
- {
- public partial class Form1 : Form
- {
- private Car[] _cars;
- private CarsData _carsData;
- private BinaryFormatter _binaryFormatter;
- private const string PATH = "CARS.dat";
- private Car _tempCar;
- public Form1()
- {
- InitializeComponent();
- _tempCar = new Car("","","","");
- _binaryFormatter = new BinaryFormatter();
- _cars = new Car[0];
- _carsData = new CarsData(_cars);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- this.Text = DateTime.Now.ToString();
- if (File.Exists(PATH))
- {
- using (FileStream fileStream = new FileStream(PATH,FileMode.OpenOrCreate))
- {
- _carsData = (CarsData)_binaryFormatter.Deserialize(fileStream);
- _cars = _carsData._cars;
- foreach (string color in _carsData._colors)
- comboBox1.Items.Add(color);
- }
- }
- else
- {
- for (int i = 0; i < 6; i++)
- comboBox1.Items.Add((Color)i);
- }
- ClearData();
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- textBox3.Text = comboBox1.Text;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- bool isFound = false;
- foreach (Car car in _cars)
- {
- if (car.StateNumber == _tempCar.StateNumber && car != _tempCar)
- {
- isFound = true;
- }
- }
- if (isFound == false)
- {
- _tempCar.UpdateElements(textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text);
- }
- string[] _colors = new string[0];
- foreach (var item in comboBox1.Items)
- Add(ref _colors,item.ToString());
- _carsData.SetData(_cars, _colors);
- using (FileStream fileStream = new FileStream(PATH, FileMode.OpenOrCreate))
- _binaryFormatter.Serialize(fileStream,_carsData);
- groupBox1.Visible = false;
- ClearData();
- }
- private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
- {
- groupBox1.Visible = true;
- foreach (Car car in _cars)
- {
- if (car.StateNumber == comboBox2.SelectedItem.ToString())
- {
- FillData(car);
- }
- }
- }
- private void FillData(Car car)
- {
- textBox1.Text = car.StateNumber;
- textBox2.Text = car.Model;
- textBox3.Text = car.Color;
- textBox4.Text = car.FullName;
- }
- private void button3_Click(object sender, EventArgs e)
- {
- foreach (Car car in _cars.ToArray())
- if (car.StateNumber.Contains(comboBox2.SelectedItem.ToString()))
- RemoveCar(ref _cars, car);
- comboBox2.Items.Remove(comboBox2.SelectedItem);
- }
- private void RemoveCar(ref Car[] cars, Car car)
- {
- Car[] tempCars = new Car[cars.Length-1];
- for (int i = 0; i < cars.Length; i++)
- if (cars[i] != car)
- tempCars[i] = cars[i];
- cars = tempCars;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- ClearData();
- }
- private void ClearData()
- {
- textBox1.Text = "";
- textBox2.Text = "";
- textBox3.Text = "";
- textBox4.Text = "";
- comboBox2.Items.Clear();
- comboBox2.Text = "";
- }
- private void comboBox2_KeyPress(object sender, KeyPressEventArgs e)
- {
- bool isFound = false;
- if (e.KeyChar == (char) Keys.Enter)
- {
- comboBox2.Items.Clear();
- foreach (Car car in _cars.ToArray())
- {
- if (car.StateNumber.Contains(comboBox2.Text))
- {
- isFound = true;
- FillData(car);
- comboBox2.Items.Add(car.StateNumber);
- }
- }
- if (isFound == false)
- {
- Add(ref _cars,new Car(comboBox2.Text,"","",""));
- _tempCar = _cars[_cars.Length-1];
- groupBox1.Visible = true;
- FillData(_cars[_cars.Length-1]);
- }
- }
- }
- private void Add<T>(ref T[] array, T value)
- {
- T[] tempCars = new T[array.Length + 1];
- for (int i = 0; i < array.Length; i++)
- tempCars[i] = array[i];
- tempCars[tempCars.Length - 1] = value;
- array = tempCars;
- }
- private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
- {
- bool isFound = false;
- if (e.KeyChar == (char)Keys.Enter)
- {
- foreach (string color in comboBox1.Items)
- {
- if (textBox3.Text == color)
- {
- isFound = true;
- comboBox1.SelectedItem = color;
- }
- }
- if (isFound == false)
- {
- comboBox1.Items.Add(textBox3.Text);
- comboBox1.Text = comboBox1.Items[comboBox1.Items.Count-1].ToString();
- }
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- using (FileStream fileStream = new FileStream(PATH,FileMode.OpenOrCreate))
- {
- _binaryFormatter.Serialize(fileStream,_carsData);
- }
- }
- }
- public enum Color
- {
- неопределенный,
- белый,
- красный,
- фиолетовый,
- серый,
- зелёный
- }
- [Serializable]
- public class CarsData
- {
- public Car[] _cars;
- public string[] _colors;
- public CarsData(Car[] cars)
- {
- _colors = new string[0];
- _cars = cars;
- }
- public void SetData(Car[] cars, string[] colors)
- {
- _cars = cars;
- _colors = colors;
- }
- }
- [Serializable]
- public class Car
- {
- private string _stateNumber;
- private string _model;
- private string _color;
- private string _fullName;
- public string StateNumber => _stateNumber;
- public string Model => _model;
- public string Color => _color;
- public string FullName => _fullName;
- public Car(string stateNumber, string model, string color, string fullName)
- {
- _stateNumber = stateNumber;
- _model = model;
- _color = color;
- _fullName = fullName;
- }
- public void UpdateElements(string stateNumber, string model, string color, string fullName)
- {
- _stateNumber = stateNumber;
- _model = model;
- _color = color;
- _fullName = fullName;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement