Загрузка данных
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Лаба_4
{
internal class Program
{
enum TypeOfPack
{
Unknown,
Коробка,
Банка,
Бутылка
}
struct Product
{
public string name;
public TypeOfPack pack;
public double price;
public int period;
}
static List<Product> MyProducts = new List<Product>();
static List<string> MyProductsName = new List<string>();
static void AddToList()
{
Product N = new Product();
while (true)
{
Console.WriteLine("Введите имя:");
N.name = ValString(Console.ReadLine());
if (N.name == null)
{
Console.WriteLine("Неправильное имя");
continue;
}
if (MyProductsName.Contains(N.name))
{
Console.WriteLine("Такой товар уже существует. Напишите другое имя");
}
else
{
MyProductsName.Add(N.name);
break;
}
}
while (true)
{
Console.WriteLine("Введите вид упаковки: \n 1 - Коробка \n 2 - Банка \n 3 - Бутылка");
try
{
N.pack = (TypeOfPack)IntInRange("", 1, 3);
break;
}
catch
{
Console.WriteLine("Ошибка, введите заново вид упаковки");
}
}
while (true)
{
Console.WriteLine("Введите цену:");
try
{
N.price = Convert.ToInt32(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("Ошибка, введите новую цену");
}
}
while (true)
{
Console.WriteLine("Введите срок реализации в днях");
try
{
N.period = Convert.ToInt32(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("Ошибка, введите новый срок реализации");
}
}
MyProducts.Add(N);
Console.WriteLine();
}
static void ShowList()
{
EmptyList();
for (int i = 0; i < MyProducts.Count; i++)
{
Show(MyProducts[i]);
}
Console.WriteLine();
}
static void Show(Product product)
{
Console.WriteLine();
Console.WriteLine("Имя: " + product.name);
Console.WriteLine("Вид упаковки: " + product.pack);
Console.WriteLine("Цена: " + product.price);
Console.WriteLine("Срок реализации: " + product.period);
Console.WriteLine("---------------------------------------");
Console.WriteLine();
}
static void findPeriodIsBad(int periodIsGood)
{
EmptyList();
for (int i = 0; i < MyProducts.Count; i++)
{
findPeriod(MyProducts[i], periodIsGood);
}
}
static void findPeriod(Product product, int periodIsGood)
{
if (product.period <= periodIsGood)
{
Show(product);
}
}
static void findMaxSum()
{
EmptyList();
double max_value = 0.0;
for (int i = 0; i < MyProducts.Count; i++)
{
max_value = findSum(MyProducts[i], max_value);
}
for (int i = 0; i < MyProducts.Count; i++)
{
showWithMaxSum(MyProducts[i], max_value);
}
}
static double findSum(Product product, double max_value)
{
if (product.price > max_value)
{
max_value = product.price;
}
return max_value;
}
static void showWithMaxSum(Product product, double max_value)
{
if (product.price == max_value)
{
Show(product);
}
}
static int PositiveInt(string str)
{
while (true)
{
Console.Write(str);
if (int.TryParse(Console.ReadLine(), out int value) && value > 0)
{
return value;
}
Console.WriteLine("Ошибка: Введите целое положительное число больше 0 до перегрева компа");
}
}
static int IntInRange(string str, int minValue, int maxValue)
{
while (true)
{
Console.Write(str);
if (int.TryParse(Console.ReadLine(), out int value) && value >= minValue && value <= maxValue)
{
return value;
}
Console.WriteLine("Ошибка: Введите целое положительное число больше 0 до перегрева компа");
}
}
static string ValString(string str)
{
if (string.IsNullOrWhiteSpace(str))
{
Console.WriteLine("Ошибка: строка не должна быть пустой");
return null;
}
if (str.Length >= 25)
{
Console.WriteLine("Ошибка: слишком длинное название");
return null;
}
if (str.All(char.IsDigit))
{
Console.WriteLine("Ошибка: название содержит только цифры");
return null;
}
return str;
}
static void EmptyList()
{
if (!MyProducts.Any())
{
Console.WriteLine("Список пуст\n");
return;
}
}
static void listNames()
{
for (int i = 0; i < MyProducts.Count; i++)
{
Console.WriteLine(i + 1 + " - " + MyProducts[i].name);
}
}
static void deleteProduct()
{
EmptyList();
Console.WriteLine("Что удалить?");
Console.WriteLine();
listNames();
int choice = IntInRange("", 1, MyProducts.Count);
int index = choice - 1;
string nameRemove = MyProducts[index].name;
MyProductsName.Remove(nameRemove);
MyProducts.RemoveAt(index);
Console.WriteLine("Удален");
}
static void correktirovkaZapisei()
{
EmptyList();
Console.WriteLine("Какую запись поменять?");
Console.WriteLine();
listNames();
int choice = IntInRange("", 1, MyProducts.Count);
}
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Введите цифру для действия:");
Console.WriteLine("1 - Создать товар");
Console.WriteLine("2 - Показать все товары");
Console.WriteLine("3 - Найти в списке продукты, срок реализации которых истекает");
Console.WriteLine("4 - Найти товары с наибольшей ценой");
Console.WriteLine("5 - Удалить продукт");
Console.WriteLine("6 - Корректировка записей");
Console.WriteLine("7 - Отбор записей по содержимому");
Console.WriteLine("8 - Закрыть ПРОГРАММНОЕ ОБЕСПЕЧЕНИЕ!!!!!!!!!!!!!");
int vibor = PositiveInt("");
int periodIsGood = 2;
switch (vibor)
{
case 1:
AddToList();
break;
case 2:
ShowList();
break;
case 3:
findPeriodIsBad(periodIsGood);
break;
case 4:
findMaxSum();
break;
case 5:
deleteProduct();
break;
case 6:
return;
default:
Console.WriteLine("Нет такой цифры в выборе. Попробуйте снова");
break;
}
}
}
}
}