111
using System;
using System.Collections.Generic;
using System.IO;
namespace _LB5
{
class MainClass
{
static List<Public_transport> public_transport = new List<Public_transport>();
static bool issetSession = true;
static List<Log> logs = new List<Log>();
enum Command
{
ShowTable = 1,
Add,
Delete,
Update,
Search,
ViewLog,
Exit,
Sort,
}
struct Log
{
public DateTime time;
public string action;
public string desc;
public Log(string a, string d)
{
time = DateTime.Now;
action = a;
desc = d;
}
public void ShowTable()
{
Console.WriteLine($"{time:T} - {action} \"{desc}\"");
}
}
static void ViewLog(List<Public_transport> public_transport)
{
var maxTimeout = TimeSpan.Zero;
for (int i = 0; i < logs.Count - 1; maxTimeout = (TimeSpan.Compare(maxTimeout, logs[i + 1].time.Subtract(logs[i].time)) == -1) ? logs[i + 1].time.Subtract(logs[i].time) : maxTimeout, i++)
if (logs.Count != 0)
{
byte counter = 0;
foreach (var l in logs)
{
if (counter > 50) break;
l.ShowTable();
counter++;
}
Console.WriteLine("\n{0} – Самый долгий период бездействия пользователя", maxTimeout.ToString(@"hh\:mm\:ss"));
}
else
Console.WriteLine("Вы не ввели данные!");
Console.ReadKey(true);
}
struct Public_transport
{
public string Type_of_transport;
public int Route_number;
public int The_length_of_the_route;
public int Time_on_the_road;
public Public_transport(string Type_of_transport, int Route_number, int The_length_of_the_route, int Time_on_the_road)
{
this.Type_of_transport = Type_of_transport;
this.Route_number = Route_number;
this.The_length_of_the_route = The_length_of_the_route;
this.Time_on_the_road = Time_on_the_road;
}
public void ShowTable()
{
Console.WriteLine($"\n {Type_of_transport,-27} {Route_number,-27} {The_length_of_the_route,-35} {Time_on_the_road,-27} ");
}
}
static void ShowTable(List<Public_transport> public_transport)
{
const string transport = "ОБЩЕСТВЕННЫЙ ТРАНСПОРТ";
const string type_of_transport = "Вид транспорта";
const string route_number = "№ маршрута";
const string the_length_of_the_route = "Протяженность маршрута (км)";
const string time_on_the_road = "Время в дороге (мин)";
const string type = "Перечисляемый тип: Тр - трамвай, Тс - троллейбус, А - автобус";
Console.WriteLine(" {0,-96} \n\n" + "\n" + " {1,-27} {2,-27} {3,-35} {4,-27} ", transport, type_of_transport, route_number, the_length_of_the_route, time_on_the_road);
if (public_transport.Count != 0)
foreach (var g in public_transport)
{
g.ShowTable();
}
else
Console.WriteLine("\n {0, -96} ", "Вы не ввели данные! ");
Console.WriteLine("\n" + " {0,-96} \n" + "", type);
Console.ReadKey(true);
}
static void Add(List<Public_transport> public_transport)
{
Console.Write("Вид транспорта: ");
string Type_of_transport = Console.ReadLine();
Console.Write("№ маршрута: ");
int Route_number = int.Parse(Console.ReadLine());
Console.Write("Протяженность маршрута (км): ");
int The_length_of_the_route = int.Parse(Console.ReadLine());
Console.Write("Время в дороге (мин): ");
int Time_on_the_road = int.Parse(Console.ReadLine());
addLog("Добавлен общественный транспорт ", Type_of_transport);
public_transport.Add(new Public_transport(Type_of_transport, Route_number, The_length_of_the_route, Time_on_the_road));
}
static void addLog(string action, string description)
{
logs.Add(new Log(action, description));
}
static void add(string Type_of_transport, int Route_number, int The_length_of_the_route, int Time_on_the_road)
{
addLog("Добавлен транспорт ", Type_of_transport);
public_transport.Add(new Public_transport(Type_of_transport, Route_number, The_length_of_the_route, Time_on_the_road));
}
static void Delete(List<Public_transport> public_transport)
{
Console.Write("Номер записи для удаления: ");
if (public_transport.Count > 0)
{
byte c = byte.Parse(Console.ReadLine());
if (c > public_transport.Count)
{
Console.Write("Такого номера не существует! ");
}
else
{
addLog("Общественный транспорт удален! ", public_transport[c - 1].Type_of_transport);
public_transport.Remove(public_transport[c - 1]);
}
}
}
static void Update(List<Public_transport> public_transport)
{
if (public_transport.Count > 0)
{
Console.Write("Номер записи для обновления: ");
byte c = byte.Parse(Console.ReadLine());
Console.Write("Вид транспорта: ");
string Type_of_transport = Console.ReadLine();
Console.Write("№ маршрута: ");
int Route_number = int.Parse(Console.ReadLine());
Console.Write("Протяженность маршрута (км): ");
int The_length_of_the_route = int.Parse(Console.ReadLine());
Console.Write("Время в дороге (мин): ");
int Time_on_the_road = int.Parse(Console.ReadLine());
addLog("Общественный транспорт заменен! ", public_transport[c - 1].Type_of_transport);
public_transport.Remove(public_transport[c - 1]);
public_transport.Insert(c - 1, new Public_transport(Type_of_transport, Route_number, The_length_of_the_route, Time_on_the_road));
}
}
static void Sort(List<Public_transport> public_transport)
{
{
for (int i = 1; i < public_transport.Count; i++)
{
var cur = public_transport[i];
int j = i;
while (j > 0 && cur.Route_number < public_transport[j - 1].Route_number)
{
public_transport[j] = public_transport[j - 1];
j--;
}
public_transport[j] = cur;
}
}
}
//public_transport[c - 1].Type_of_transport
////public void InsertionSort(int[] array)
// //{
//for (int i = 1; i < public_transport.Route_number; i++)
// {
//int cur = public_transport[i];
// int j = i;
//while (j > 0 && cur < public_transport[j - 1])
// {
// public_transport[j] = public_transport[j - 1];
// j--;
// }
//public_transport[j] = cur;
//if (public_transport.Count > 0)
//{
//Console.Write("Номер записи для обновления: ");
//byte c = byte.Parse(Console.ReadLine());
//Console.Write("Вид транспорта: ");
//string Type_of_transport = Console.ReadLine();
//Console.Write("№ маршрута: ");
//int Route_number = int.Parse(Console.ReadLine());
//Console.Write("Протяженность маршрута (км): ");
//int The_length_of_the_route = int.Parse(Console.ReadLine());
//Console.Write("Время в дороге (мин): ");
//int Time_on_the_road = int.Parse(Console.ReadLine());
//addLog("Общественный транспорт заменен! ", public_transport[c - 1].Type_of_transport);
//public_transport.Remove(public_transport[c - 1]);
//public_transport.Insert(c - 1, new Public_transport(Type_of_transport, Route_number, The_length_of_the_route, Time_on_the_road));
// }
//}
//static void Search(List<Public_transport> public_transport)
//{
// for (int i = 0; i < public_transport.Count; i++)
// {
// var min = i;
// for (int j = i; j < public_transport.Count; min = public_transport[min].The_length_of_the_route > public_transport[j].The_length_of_the_route ? j : min, j++) ;
// var tmp = public_transport[i];
// public_transport[i] = public_transport[min];
// public_transport[min] = tmp;
// }
//}
static void Search(List<Public_transport> public_transport)
{
const string transport = "ОБЩЕСТВЕННЫЙ ТРАНСПОРТ";
const string type_of_transport = "Вид транспорта";
const string route_number = "№ маршрута";
const string the_length_of_the_route = "Протяженность маршрута (км)";
const string time_on_the_road = "Время в дороге (мин)";
const string type = "Перечисляемый тип: Тр - трамвай, Тс - троллейбус, А - автобус";
Console.WriteLine(" {0,-96} \n\n" + "\n" + " {1,-27} {2,-27} {3,-35} {4,-27} ", transport, type_of_transport, route_number, the_length_of_the_route, time_on_the_road);
if (public_transport.Count != 0)
{
foreach (var g in public_transport)
if (g.The_length_of_the_route > 1)
g.ShowTable();
}
else
Console.WriteLine("\n {0, -96} ", "Вы не ввели данные! ");
Console.WriteLine("\n" + " {0,-96} \n" + "", type);
Console.ReadKey(true);
}
static void Exit(out bool issetSession)
{
issetSession = false;
}
static void ReadData()
{
string path = @"lab.dat";
using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.OpenOrCreate)))
{
while (reader.PeekChar() > -1)
{
string Type_of_transport = reader.ReadString();
int Route_number = reader.ReadInt32();
int The_length_of_the_route = reader.ReadInt32();
int Time_on_the_road = reader.ReadInt32();
add(Type_of_transport, Route_number, The_length_of_the_route, Time_on_the_road);
}
}
}
static void WriteData()
{
string path = @"lab.dat";
using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.OpenOrCreate)))
{
foreach (Public_transport g in public_transport)
{
writer.Write(g.Type_of_transport);
writer.Write(g.Route_number);
writer.Write(g.The_length_of_the_route);
writer.Write(g.Time_on_the_road);
}
}
}
static void Main(string[] args)
{
ReadData();
while (issetSession)
{
Console.Clear();
Console.WriteLine("1 – Просмотр таблицы");
Console.WriteLine("2 – Добавить запись");
Console.WriteLine("3 – Удалить запись");
Console.WriteLine("4 – Обновить запись");
Console.WriteLine("5 – Поиск записей");
Console.WriteLine("6 – Просмотреть лог");
Console.WriteLine("7 - Выход");
Console.WriteLine("\n8 - Сортировка вставкой!");
Console.Write("\n\nВведите номер команды: ");
int Number = int.Parse(Console.ReadLine());
Console.Clear();
switch (Number)
{
case (byte)Command.ShowTable:
ShowTable(public_transport);
break;
case (byte)Command.Add:
Add(public_transport);
break;
case (byte)Command.Delete:
Delete(public_transport);
break;
case (byte)Command.Update:
Update(public_transport);
break;
case (byte)Command.Search:
Search(public_transport);
break;
case (byte)Command.ViewLog:
ViewLog(public_transport);
break;
case (byte)Command.Exit:
Exit(out issetSession);
break;
case (byte)Command.Sort:
Sort(public_transport);
break;
default:
break;
}
//Console.ReadKey();
//1 – Просмотр таблицы
//2 – Добавить запись
//3 – Удалить запись
//4 – Обновить запись
//5 – Поиск записей
//6 – Просмотреть лог
//7 - Выход
// 1 - Viewing a table
// 2 - Add record
// 3 - Delete the record
// 4 - Update Record
// 5 - Search for records
// 6 - View the log
// 7 - Exit
}
WriteData();
}
}
}