Answer the question
In order to leave comments, you need to log in
How to properly refactor the following C# code so that it looks beautiful?
Good day.
Tell me how to refactor the following methods correctly and is it possible to simplify them for further use?
We have the following "abstract base" class:
public class BaseCar
{
public int Position { get; set; } // номер стояночного места
public int Box {get; set;} //номер гаража
}
//Самосвалы
public class Tipper : BaseCar
{
public int Capacity { get; set; }
}
// Автобусы
public class Autobus : BaseCar
{
public int MaxPeople {get;set;}
}
public class Garage
{
public List<Autobus> autobus_list {get; set;};
public List<Tipper> tipper_list {get; set;};
public Garage()
{
autobus_list = getAutobusList();
tipper_list = getTippetList();
}
}
public class Logic
{
public Garage garage = new Garage();
....
// Теперь сама суть вопроса
// Требуется вернуть отсортированный по стояночному месту список
// или список определенной техники в каком либо гараже
// В данный момент я делаю это такими вот методами
public List<Autobus> getListAutobusInBox(int box)
{
return garage.autobus_list.FindAll(x => x.Box == box);
}
public List<Tipper> getListTipperInBox(int box)
{
return garage.tipper_list.FindAll(x => x.Box == box);
}
public List<Autobus> SortListAutobus(List<Autobus> list)
{
list.Sort(delegate (Autobus x, Autobus y)
{
if (object.Equals(x, y)) return 0;
return x.Position.CompareTo(y.Position);
});
return list;
}
public List<Tipper> SortListTipper(List<Tipper> list)
{
list.Sort(delegate (Tipper x, Tipper y)
{
if (object.Equals(x, y))
return 0;
return x.Position.CompareTo(y.Position);
});
return list;
}
}
Answer the question
In order to leave comments, you need to log in
What interfaces? Generic Types and Methods - metanit.com/sharp/tutorial/3.12.php
using System.IO;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
public class BaseCar
{
public int Position { get; set; } // номер стояночного места
public int Box { get; set; } //номер гаража
}
public class Tipper : BaseCar
{
public int Capacity { get; set; }
}
// Автобусы
public class Autobus : BaseCar
{
public int MaxPeople { get; set; }
}
public class Garage
{
public List<Autobus> autobus_list { get; set; }
public List<Tipper> tipper_list { get; set; }
public Garage()
{
autobus_list = new List<Autobus>();
tipper_list = new List<Tipper>();
}
}
public class Logic
{
public Garage garage = new Garage();
public List<Autobus> getListAutobusInBox(int box)
{
return garage.autobus_list.FindAll(x => x.Box == box);
}
public List<Tipper> getListTipperInBox(int box)
{
return garage.tipper_list.FindAll(x => x.Box == box);
}
public List<T> SortList<T>(List<T> list) where T : BaseCar
{
return list.Sort((x, y) => object.Equals(x, y) ? 0 : x.Position.CompareTo(y.Position)).ToList();
}
}
}
You yourself write that BaseCar is abstract.
Then
you can add a method to the base class to determine the type of car.
Which will have to be implemented in each child class
public class Autobus : BaseCar
{
public override CarType GetCarType()
{
return CarType.Autobus;
}
public int MaxPeople {get;set;}
}
car.Type == typeof(Autobus)
, but it will be slower. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question