项目多次添加到列表中

时间:2016-05-03 19:47:25

标签: c# list class

我正在尝试仅使用c#构建一个药房项目 我写了一个代码来诊断患者的疾病并打印出合适的治疗药物,我想检查一下这种药是否已经在我的药房(可用的药物在另一个类别中定义) 如果它存在,那么程序会将它传递给sell方法,如果它不存在,我想添加到列表中,以便我可以稍后订购 问题是,每次运行代码时,它都会多次添加该器官系统中所有疾病的所有药物

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pharmacy
{
 class OrganSystems
  {
    protected string[,] GastSystem;

   public void GastDefine()
    {
         GastSystem = new string[3, 4];
        GastSystem[0, 0] = "Gastroparesis";
        GastSystem[0, 1] = "-Nausea \n-Vomiting \n-An early feeling of fullness when eating \n-Weight loss " +
            "\n-Abdominal bloating \n-Abdominal discomfort";
        GastSystem[0, 2] = "Premperan";
        GastSystem[0, 3] = "--Follow the doctor's instructions--";

        GastSystem[1, 0] = "Heartburn";
        GastSystem[1, 1] = "-Persistent sore throat \n-Hoarseness \n-Chronic cough \n-Asthma \n-Chest pain " +
            "\n-Feeling like there is a lump in your throat";
        GastSystem[1, 2] = "Salbutamol";
        GastSystem[1, 3] = "--Follow the doctor's instructions--";

        GastSystem[2, 0] = "Acute pancreatitis";
        GastSystem[2, 1] = "-Constant pain in the upper abdomen, in the back and other areas \n-" +
             "Pain may be sudden and intense or may begin as a mild pain that is aggravated by eating and drinking \n-" +
             "Elevated pulse \n-Fever \n-Nausea and vomiting \n-Swollen and tender abdomen";

        GastSystem[2, 2] = "Ganton";
        GastSystem[2, 3] = "--Follow the doctor's instructions--";

    }
}

}
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

      namespace pharmacy
    {
 class Diagnose:Drugs
 {

  public int systemChoice;
    public Diagnose(int SysCh)
    {
        systemChoice = SysCh;
    }



   private int DetermineD;

    public int dDicease
    {
        get { return DetermineD; }
        set { DetermineD = value; }
    }


    public void Symptoms()
    {

        if (systemChoice == 1)
        {
            for (int j = 0; j <= 2; j++)
            {
                Console.WriteLine("{0}:\n {1}\n", j + 1, GastSystem[j, 1]);

            }

            Console.WriteLine("Are you having any of the following? (Type the number)");

            dDicease = int.Parse(Console.ReadLine());
            GastDiagnose();

        }

        else if (systemChoice == 2)
        {
            for (int j = 0; j <= 2; j++)
            {
                Console.WriteLine("{0}:\n {1}\n", j + 1, RespSystem[j, 1]);

            }

            Console.WriteLine("Are you having any of the following? (Type the number)");

            dDicease = int.Parse(Console.ReadLine());
            RespDiagnose();

        }

        else if (systemChoice == 3)
        {
            for (int j = 0; j < 2; j++)
            {
                Console.WriteLine("{0}:\n {1}\n", j + 1, CardSystem[j, 1]);

            }
            Console.WriteLine("Are you having any of the following? (Type the number)");

            dDicease = int.Parse(Console.ReadLine());
            CardDiagnose();


        }

        else if (systemChoice == 4)
        {
            for (int j = 0; j < 2; j++)
            {
                Console.WriteLine("{0}:\n {1}\n", j + 1, EyeSystem[j, 1]);

            }

            Console.WriteLine("Are you having any of the following? (Type the number)");

            dDicease = int.Parse(Console.ReadLine());
            EyeDiagnose();
        }

        else if (systemChoice == 5)
        {
            for (int j = 0; j <= 2; j++)
            {
                Console.WriteLine("{0}:\n {1}\n", j + 1, ENTSystem[j, 1]);

            }

            Console.WriteLine("Are you having any of the following? (Type the number)");

            dDicease = int.Parse(Console.ReadLine());
            ENTDiagnose();
        }
    }




    public void GastDiagnose()
    {

        switch (dDicease)
        {

            case 1:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        GastSystem[0, 0], GastSystem[0, 2], GastSystem[0, 3]); break;
            case 2:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        GastSystem[1, 0], GastSystem[1, 2], GastSystem[1, 3]); break;

            case 3:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        GastSystem[2, 0], GastSystem[2, 2], GastSystem[2, 3]); break;

            default:
                Console.WriteLine("\n\nNOT A VALID ENTRY!\n\nPlease choose the signs that match your state the best\n----------------");

                break;

        }

    }

    public void RespDiagnose()
    {

        switch (dDicease)
        {

            case 1:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        RespSystem[0, 0], RespSystem[0, 2], RespSystem[0, 3]); break;
            case 2:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        RespSystem[1, 0], RespSystem[1, 2], RespSystem[1, 3]); break;

            case 3:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        RespSystem[2, 0], RespSystem[2, 2], RespSystem[2, 3]); break;

            default:
                Console.WriteLine("\n\nNOT A VALID ENTRY!\n\nPlease choose the signs that match your state the best\n----------------");
                Symptoms();
                break;

        }

    }


    public void CardDiagnose()
    {

        switch (dDicease)
        {

            case 1:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        CardSystem[0, 0], CardSystem[0, 2], CardSystem[0, 3]); break;
            case 2:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        CardSystem[1, 0], CardSystem[1, 2], CardSystem[1, 3]); break;

            default:
                Console.WriteLine("\n\nNOT A VALID ENTRY!\n\nPlease choose the signs that match your state the best\n----------------");
                Symptoms();
                break;

        }

    }




    public void ENTDiagnose()
    {

        switch (dDicease)
        {

            case 1:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        ENTSystem[0, 0], ENTSystem[0, 2], ENTSystem[0, 3]); break;
            case 2:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        ENTSystem[1, 0], ENTSystem[1, 2], ENTSystem[1, 3]); break;

            case 3:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        ENTSystem[2, 0], ENTSystem[2, 2], ENTSystem[2, 3]); break;

            default:
                Console.WriteLine("\n\nNOT A VALID ENTRY!\n\nPlease choose the signs that match your state the best\n----------------");
                Symptoms();
                break;

        }

    }

     public void EyeDiagnose()
    {

        switch (dDicease)
        {

            case 1:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        EyeSystem[0, 0], EyeSystem[0, 2], EyeSystem[0, 3]); break;
            case 2:
                Console.WriteLine("Dicease: {0}\nTreatment: {1}\nDrug: {2}\n\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-",
                        EyeSystem[1, 0], EyeSystem[1, 2], EyeSystem[1, 3]); break;


            default:
                Console.WriteLine("\n\nNOT A VALID ENTRY!\n\nPlease choose the signs that match your state the best\n----------------");
                Symptoms();
                break;

        }

    }

     public List<string> ToBeOrdered = new List<string>();

     Drugsoperation ON;

     public void MatchingMed()
     {
         getdrugs();
         foreach (Druginformation drugInfo in Definnewdrugs)
         {
             for (int i = 0; i < 3; i++) 
             {
                 if (systemChoice == 1 && GastSystem[i, 2].Contains(drugInfo.Drugname))
                 {
                     ON = new Drugsoperation(GastSystem[i, 2]);
                     ON.order();
                     break;
                 }
                 else if (systemChoice == 1 && !GastSystem[i, 2].Contains(drugInfo.Drugname))

                     Console.WriteLine("This medicine is not available now, but we will make sure to have it soon.");
                 if (ToBeOrdered.Count < 2)
                 {
                     ToBeOrdered.Add(GastSystem[i, 2]);
                 }
             }

             for (int i = 0; i < 3; i++)
             {

                 if (systemChoice == 2 && RespSystem[i, 2].Contains(drugInfo.Drugname))
                 {
                     ON = new Drugsoperation(RespSystem[i, 2]);
                     ON.order();

                 }
                 else if (systemChoice == 2 && !RespSystem[i, 2].Contains(drugInfo.Drugname))
                 {
                     Console.WriteLine("This medicine is not available now, but we will make sure to have it soon.");
                     ToBeOrdered.Add(RespSystem[i, 2]);
                 }

             }

             for (int i = 0; i < 2; i++)
             {


                 if (systemChoice == 3 && CardSystem[i, 2].Contains(drugInfo.Drugname))
                 {
                     ON = new Drugsoperation(CardSystem[i, 2]);
                     ON.order();
                 }
                 else if (systemChoice == 3 && !CardSystem[i, 2].Contains(drugInfo.Drugname))
                 {
                     Console.WriteLine("This medicine is not available now, but we will make sure to have it soon.");
                     ToBeOrdered.Add(CardSystem[i, 2]);
                 }
             }


             for (int i = 0; i < 2; i++)
             {

                 if (systemChoice == 4 && EyeSystem[i, 2].Contains(drugInfo.Drugname))
                 {
                     ON = new Drugsoperation(EyeSystem[i, 2]);
                     ON.order();

                 }
                 else if (systemChoice == 4 && !EyeSystem[i, 2].Contains(drugInfo.Drugname))
                 {
                     Console.WriteLine("This medicine is not available now, but we will make sure to have it soon.");
                     ToBeOrdered.Add(EyeSystem[i, 2]);
                 }

             }

             for (int i = 0; i < 3; i++)
             {


                 if (systemChoice == 5 && ENTSystem[i, 2].Contains(drugInfo.Drugname))
                 {
                     ON = new Drugsoperation(ENTSystem[i, 2]);
                     ON.order();

                 }
                 else if (systemChoice == 5 && !ENTSystem[i, 2].Contains(drugInfo.Drugname))
                 {
                     Console.WriteLine("This medicine is not available now, but we will make sure to have it soon.");
                     ToBeOrdered.Add(ENTSystem[i, 2]);
                 }
             }
         }    

     }


     public void PrintOrders()
     {

         Console.WriteLine("Medicines to be ordered:");
         foreach (string OMed in ToBeOrdered)
         {
             Console.WriteLine(OMed);
         }

     }
  }
}

输出为enter image description here

2 个答案:

答案 0 :(得分:1)

你想要的是检查列表以查看它是否已包含字符串。这很容易完成:

// If the list of drugs to be ordered does not contain this drug name
if(!ToBeOrdered.Contains("DrugNameGoesHere")
{
    // Then, add this drug name to the list.
    ToBeOrdered.Add("DrugNameGoesHere");
}

您可以在此处详细了解Contains:http://www.dotnetperls.com/list-contains

答案 1 :(得分:0)

这段代码太乱了。用某种黑魔法来理解这个项目。 如果您有一个列表,请使用linq只获取唯一的名称。

list = list.GroupBy(x => x.DrugName).Select(x => x.First()).ToList();