简体   繁体   English

项目多次添加到列表中

[英]Items added to a list multiple times

I'm trying to build a pharmacy project using only c# I wrote a code to diagnose a patient's disease and print the suitible treatment drug for it ,and I want to check if that drug is already in my pharmacy or not(the available drugs are defined in another class) if it exists then the program will pass it to the selling method ,and if it didn't exist I want to add to a list so I can order it later the problem is that every time I run the code it will add all the drugs of all the diseases in that organ system multiple times 我正在尝试仅使用c#建立一个药房项目。我编写了一个代码来诊断患者的疾病并为其打印合适的治疗药物,我想检查该药物是否已经在我的药房中(可用的药物是如果存在,则程序会将其传递给Selling方法,如果不存在,我想添加到列表中,以便稍后再订购,问题是每次运行代码时将多次添加该器官系统中所有疾病的所有药物

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);
         }

     }
  }
}

the output is 输出是 在此处输入图片说明

What you want is to check the list to see if it contains the string already. 您想要的是检查列表以查看其是否已包含字符串。 This is easily done with: 这很容易做到:

// 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");
}

You can read more on Contains here: http://www.dotnetperls.com/list-contains 您可以在此处包含更多内容: http//www.dotnetperls.com/list-contains

This code is such a mess. 这段代码太乱了。 With some kind of dark magic make sense of this project. 用某种黑暗的魔法使这个项目有意义。 If you have a list, use linq to get only unique names. 如果有列表,请使用linq仅获得唯一的名称。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM