Дайте совет по С#. Выбор лучшего из вариантов

Dmitriy Ka

Client
Регистрация
03.05.2016
Сообщения
716
Благодарностей
461
Баллы
63
Есть задача выбрать лучший вариант.
Есть такой приоритет:
1 Яблоки
2 Груши
3 Апельсины
4 Бананы
5 Вишня

Получаем данные от сервера (всегда рандом)
C#:
string _lvl = "705";
string strIdQuest1 = "id123";
string strIdQuest2 = "id456";
string strIdQuest3 = "id789";
string strMenu1 = "Имя 1";
string strMenu2 = "Имя 2";
string strMenu3 = "Имя 3";
string strTypeQuest1 = "Бананы";
string strTypeQuest2 = "Груши";
string strTypeQuest3 = "Яблоки";
Теперь надо получить лучшее меню из 3ех.
Сделал такую конструкцию, с задачей справляется, но выглядит не очень.

C#:
string strGoodMenu = String.Empty;
if (strTypeQuest1 == "Яблоки") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Яблоки") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Яблоки") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

else if (strTypeQuest1 == "Груши") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Груши") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Груши") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

else if (strTypeQuest1 == "Апельсины") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Апельсины") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Апельсины") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

else if (strTypeQuest1 == "Бананы") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Бананы") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Бананы") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

else if (strTypeQuest1 == "Вишня") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Вишня") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Вишня") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

return strGoodMenu;
Подскажите, какие еще можно использовать конструкции для решения задачи.
 

radv

Client
Регистрация
11.05.2015
Сообщения
3 778
Благодарностей
1 944
Баллы
113
Подскажите, какие еще можно использовать конструкции для решения задачи.
Сделать массив из вариантов по приоритетам, и потом через foreach перебирать значения из массива и сравнивать.
 
  • Спасибо
Реакции: Dmitriy Ka

BAZAg

Client
Регистрация
08.11.2015
Сообщения
1 781
Благодарностей
2 442
Баллы
113
Есть задача выбрать лучший вариант.
Есть такой приоритет:
1 Яблоки
2 Груши
3 Апельсины
4 Бананы
5 Вишня

Получаем данные от сервера (всегда рандом)
C#:
string _lvl = "705";
string strIdQuest1 = "id123";
string strIdQuest2 = "id456";
string strIdQuest3 = "id789";
string strMenu1 = "Имя 1";
string strMenu2 = "Имя 2";
string strMenu3 = "Имя 3";
string strTypeQuest1 = "Бананы";
string strTypeQuest2 = "Груши";
string strTypeQuest3 = "Яблоки";
Теперь надо получить лучшее меню из 3ех.
Сделал такую конструкцию, с задачей справляется, но выглядит не очень.

C#:
string strGoodMenu = String.Empty;
if (strTypeQuest1 == "Яблоки") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Яблоки") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Яблоки") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

else if (strTypeQuest1 == "Груши") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Груши") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Груши") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

else if (strTypeQuest1 == "Апельсины") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Апельсины") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Апельсины") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

else if (strTypeQuest1 == "Бананы") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Бананы") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Бананы") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

else if (strTypeQuest1 == "Вишня") strGoodMenu = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
else if (strTypeQuest2 == "Вишня") strGoodMenu = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
else if (strTypeQuest3 == "Вишня") strGoodMenu = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

return strGoodMenu;
Подскажите, какие еще можно использовать конструкции для решения задачи.
Общий код:
public class Req {
        public string _lvl;
        public List<Fruit> fruit;
        public Req (string lvl, List<string[]> list){
            _lvl = lvl;
            fruit = new List<Fruit>();
            fruit = list.Where(x=>x.Length == 3).Select(x=> new Fruit(x[0],x[1],x[2])).ToList();
        }
        public string GetGoodMenu (string[] data){
            for(int i=0;i<data.Length;i++) {
                string f = data[i]; // Яблоки              
                for(int j=0;j<fruit.Count;j++) {
                    string ff = fruit[j].TypeQuest;
                    if(f == ff) return string.Join(":", new[]{_lvl,fruit[j].Menu, fruit[j].IdQuest });
                }
            }
            return "Рецепт не найден";
        }
    }
    public class Fruit {
        public string IdQuest;
        public string Menu;
        public string TypeQuest;
        public Fruit(){
            IdQuest = string.Empty;
            Menu = string.Empty;
            TypeQuest = string.Empty;
        }
        public Fruit(string a, string b, string c){
            IdQuest = a;
            Menu =  b;
            TypeQuest =c;
        }      
    }
Вызов с кубика:
string _lvl = "705";
string strIdQuest1 = "id123";
string strIdQuest2 = "id456";
string strIdQuest3 = "id789";
string strMenu1 = "Имя 1";
string strMenu2 = "Имя 2";
string strMenu3 = "Имя 3";
string strTypeQuest1 = "Бананы";
string strTypeQuest2 = "Груши";
string strTypeQuest3 = "Яблоки";


string[] priority = new[]{ "Яблоки", "Груши", "Апельсины", "Бананы", "Вишня"};

List<string[]> list = new List<string[]>();
list.Add(new[]{strIdQuest1, strMenu1, strTypeQuest1});
list.Add(new[]{strIdQuest2, strMenu2, strTypeQuest2});
list.Add(new[]{strIdQuest3, strMenu3, strTypeQuest3});
Req r = new Req(_lvl, list);

return r.GetGoodMenu(priority);
 
  • Спасибо
Реакции: Dmitriy Ka

porileenvej

Client
Регистрация
09.05.2020
Сообщения
99
Благодарностей
131
Баллы
33
Я бы через словарь сделал
C#:
string _lvl = "705";
string strIdQuest1 = "id123";
string strIdQuest2 = "id456";
string strIdQuest3 = "id789";
string strMenu1 = "Имя 1";
string strMenu2 = "Имя 2";
string strMenu3 = "Имя 3";
string strTypeQuest1 = "Бананы";
string strTypeQuest2 = "Груши";
string strTypeQuest3 = "Яблоки";

Dictionary<string, string> res = new Dictionary<string, string>();
res[strTypeQuest1] = _lvl + ":" + strMenu1 + ":" + strIdQuest1;
res[strTypeQuest2] = _lvl + ":" + strMenu2 + ":" + strIdQuest2;
res[strTypeQuest3] = _lvl + ":" + strMenu3 + ":" + strIdQuest3;

if(res.Keys.Contains("Яблоки")) return res["Яблоки"];
else if(res.Keys.Contains("Груши")) return res["Груши"];
else if(res.Keys.Contains("Апельсины")) return res["Апельсины"];
else if(res.Keys.Contains("Бананы")) return res["Бананы"];
else if(res.Keys.Contains("Вишня")) return res["Вишня"];
 
Последнее редактирование:
  • Спасибо
Реакции: Dmitriy Ka

doc

Client
Регистрация
30.03.2012
Сообщения
8 684
Благодарностей
4 641
Баллы
113
не знаю, правильно ли понял задачу, но так можно отсортировать
C#:
string _lvl = "705";
string strIdQuest1 = "id123";
string strIdQuest2 = "id456";
string strIdQuest3 = "id789";
string strMenu1 = "Имя 1";
string strMenu2 = "Имя 2";
string strMenu3 = "Имя 3";
string strTypeQuest1 = "Бананы";
string strTypeQuest2 = "Груши";
string strTypeQuest3 = "Яблоки";


var priority = new List<string>{ "Яблоки", "Груши", "Апельсины", "Бананы", "Вишня"};

List<string[]> list = new List<string[]>();
list.Add(new[]{strIdQuest1, strMenu1, strTypeQuest1});
list.Add(new[]{strIdQuest2, strMenu2, strTypeQuest2});
list.Add(new[]{strIdQuest3, strMenu3, strTypeQuest3});



list = list.OrderBy(x => priority.IndexOf(x[2])).ToList();
return string.Join("\r\n", list.Select(x => _lvl + ";" + string.Join(";", x)));
 
  • Спасибо
Реакции: Dmitriy Ka

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)