Очень смущает "зависимость" методов:
Scoreboard(Train[] train) <- SortArray(train) <- Show(train)
Как от этого избавиться?
Условие:
Класс Program:
using System;
namespace Train
{
class Program
{
static void Main()
{
Train[] trains = new Train[2];
TrainManager.Scoreboard(trains);
}
}
}
Класс Train:
using System;
namespace Train
{
struct Train
{
private string destination_name;
public string Destination_Name
{
get { return destination_name; }
}
private int train_number;
public int Train_Number
{
get { return train_number; }
}
private string departure_time;
public string Departure_Time
{
get { return departure_time; }
}
public Train(string destination_name, int train_number, string departure_time)
{
this.destination_name = destination_name;
this.train_number = train_number;
this.departure_time = departure_time;
}
}
}
Класс TrainManager:
using System;
namespace Train
{
static class TrainManager
{
public static void Scoreboard(Train[] train)
{
string destination_name;
int train_number;
string departure_time;
for (int i = 0; i < train.Length; i++)
{
Console.Write("Enter train number: ");
train_number = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter destination time: ");
destination_name = Convert.ToString(Console.ReadLine());
Console.Write("Enter departure time: ");
departure_time = Convert.ToString(Console.ReadLine());
train[i] = new Train(destination_name, train_number, departure_time);
Console.WriteLine(new string('-', 30));
}
SortArray(train);
}
private static void SortArray(Train[] train)
{
for (int i = 0; i < train.Length; i++)
{
for (int q = 0; q < train.Length; q++)
{
if (train[i].Train_Number <= train[q].Train_Number)
{
Train g = train[i];
train[i] = train[q];
train[q] = g;
}
}
}
Show(train);
}
private static void Show(Train[] train)
{
Console.Clear();
Console.WriteLine("////////////////////TRAIN SCHEDULE////////////////////");
for(int i = 0; i < train.Length; i++)
{
Console.WriteLine();
Console.WriteLine(new string('-', 30));
Console.WriteLine("Train number {0}:", train[i].Train_Number);
Console.WriteLine(new string('-', 30));
Console.WriteLine("Destination name: {0}", train[i].Destination_Name);
Console.WriteLine("Departure time: {0}", train[i].Departure_Time);
Console.WriteLine();
}
}
}
}
Ксли говорить про код, то что тут неправильно:
Как избавиться: разделить ответственности, выкинуть статику, не забыть про юнит тесты.
void Main()
{
var logic = new MainLogic(new UserInteractor(), new ScheduleLogic());
logic.Run();
}
struct Train
{
private string destination_name;
public string Destination_Name
{
get { return destination_name; }
}
private int train_number;
public int Train_Number
{
get { return train_number; }
}
private string departure_time;
public string Departure_Time
{
get { return departure_time; }
}
public Train(string destination_name, int train_number, string departure_time)
{
this.destination_name = destination_name;
this.train_number = train_number;
this.departure_time = departure_time;
}
}
class UserInteractor
{
public Train[] ReadTrains (int trainCount)
{
var trains = new Train[trainCount];
string destination_name;
int train_number;
string departure_time;
for (int i = 0; i < trainCount; i++)
{
Console.Write("Enter train number: ");
train_number = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter destination time: ");
destination_name = Convert.ToString(Console.ReadLine());
Console.Write("Enter departure time: ");
departure_time = Convert.ToString(Console.ReadLine());
trains[i] = new Train(destination_name, train_number, departure_time);
Console.WriteLine(new string('-', 30));
}
return trains;
}
public void Show(Train[] train)
{
Console.Clear();
Console.WriteLine("////////////////////TRAIN SCHEDULE////////////////////");
for (int i = 0; i < train.Length; i++)
{
Console.WriteLine();
Console.WriteLine(new string('-', 30));
Console.WriteLine("Train number {0}:", train[i].Train_Number);
Console.WriteLine(new string('-', 30));
Console.WriteLine("Destination name: {0}", train[i].Destination_Name);
Console.WriteLine("Departure time: {0}", train[i].Departure_Time);
Console.WriteLine();
}
}
}
class ScheduleLogic
{
public Train[] Schedule(Train[] trains)
{
return trains?.OrderBy(x=>x.Train_Number).ToArray();
}
}
class MainLogic
{
private UserInteractor _interactor;
private ScheduleLogic _scheduleLogic;
public MainLogic(UserInteractor interactor, ScheduleLogic scheduleLogic)
{
_interactor = interactor;
_scheduleLogic = scheduleLogic;
}
public void Run()
{
var trains = _interactor.ReadTrains(2);
trains = _scheduleLogic.Schedule(trains);
_interactor.Show(trains);
}
}
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
Здравствуйте! Я хочу создать приложение для ПК на C# я сам пока новичокДля графического интерфейса я выбрал windows forms Приложение основывается...
Господа, помогите реализовать грамотное обновление данных
Открываю из программы картинку в стандартном "Просмотр фотографий Windows" таким образом: