如何在不再使用之前继续询问新ID

时间:2016-12-06 21:29:01

标签: c# loops file-handling

我当前的代码存在问题,我不知道如何在ID长度!= 8且ID号已被占用的情况下继续循环访问我的代码。这可能就像添加新程序一样简单,但我不确定。

static void GetIDInput(ref int ID)
{

    int tempID = 0;

    while (tempID.ToString().Length != 8)
    {
        Console.WriteLine("Please enter your desired ID number");
        tempID = Convert.ToInt32(Console.ReadLine());
    }

    ID = tempID;
}

static void AddStock()
{

    int stockQuantity = 0;
    double stockPrice = 0.00;
    string stockName = "";
    string s = ""; // Being Lazy here, to convert to when needed.
    int tempID = 0;
    int IDNumber = 0;
    string lineValues;
    bool taken = false;

    GetIDInput(ref tempID);

    using (StreamReader sr = new StreamReader("Stockfile.txt"))
    {
        while (sr.EndOfStream == false && taken != true)
        {
            lineValues = sr.ReadLine();

            if (lineValues.Contains(tempID.ToString()))
            {
                taken = true;
            }
            else
            {
                taken = false;
            }
        }

        if (taken == false)
        {
            IDNumber = tempID;
        }
        else if (taken == true)
        {
            Console.WriteLine("Sorry this ID is already taken, try again");
            // Want to re-loop  here but not sure how...
        }
    }


    using (StreamWriter sw = new StreamWriter("Stockfile.txt", true))
    {
        s = IDNumber.ToString();
        sw.Write(s + "\t"); // Will only accept an 8 figure digit so is safe to have a single value here.

        using (StreamWriter sw = new StreamWriter("Stockfile.txt", true))
        {
            s = IDNumber.ToString();
            sw.Write(s + "\t"); // Will only accept an 8 figure digit so is safe to have a single value here.

            while (stockName.Length <= 2) // No fancy brands here......
            {
                Console.Write("Please enter the name of the stock: ");
                stockName = Console.ReadLine();
            }

            s = stockName;
            sw.Write(s + "\t");

            while (stockQuantity < 1) // Running a small shop here...
            {
                Console.Write("Please enter the quanity of stock: ");
                stockQuantity = Convert.ToInt32(Console.ReadLine());
            }

            s = stockQuantity.ToString();
            sw.Write(s + "\t");

            while (stockPrice < 0.01) // Running a very small shop....
            {
                Console.Write("Please enter the price of the stock: ");
                stockPrice = Convert.ToDouble(Console.ReadLine());
            }

            s = stockPrice.ToString();
            sw.Write(s + "\t");

            sw.WriteLine(); // TO create the new line.....

        }

这样做的目的是检查文件中的ID号是否已被删除,因为用户查找时会出现错误。

3 个答案:

答案 0 :(得分:1)

想想你自己说过的话

  

在ID长度!= 8和数字的同时继续循环我的代码   采取

准确地改变你所拥有的东西。

我冒昧地放弃参考参考,随意把它放回去

static int GetIDInput()
{

    int tempID = 0;
    bool taken = true;
    bool isInputValid = false;

    while (taken == true && isInputValid == false)
    {
        Console.WriteLine("Please enter your desired ID number");
        tempID = Convert.ToInt32(Console.ReadLine());

        if (tempID.ToString().Length != 8)
        {
            isInputValid = false;
            Console.WriteLine("ID number must be 8 digits long.")
        }
        else
        {
            isInputValid = true;
        }

        if (isInputValid) // this wont run if the input wasnt 8 characters, so the loop will restart
        {
            using (StreamReader sr = new StreamReader("Stockfile.txt"))
            {
                while (sr.EndOfStream == false && taken != true)
                {
                    lineValues = sr.ReadLine();

                    if (lineValues.Contains(tempID.ToString()))
                    {
                        taken = true;
                    }
                    else
                    {
                        taken = false;
                    }
                }

                if (taken == false)
                {
                    ID = tempID;
                }
                else if (taken == true)
                {
                    Console.WriteLine("Sorry this ID is already taken, try again");
                    // statements will lead us back to the while loop and taken == true so it will run again
                }
            }
        }
    }

    return tempID;
}



static void AddStock()
{
    int stockQuantity = 0;
    double stockPrice = 0.00;
    string stockName = "";
    string s = ""; // Being Lazy here, to convert to when needed.
    int IDNumber = 0;
    string lineValues;

    IDNumber = GetIDInput();

    using (StreamWriter sw = new StreamWriter("Stockfile.txt", true))
    {
        s = IDNumber.ToString();
        sw.Write(s + "\t"); // Will only accept an 8 figure digit so is safe to have a single value here.

        using (StreamWriter sw = new StreamWriter("Stockfile.txt", true))
        {
            s = IDNumber.ToString();
            sw.Write(s + "\t"); // Will only accept an 8 figure digit so is safe to have a single value here.

            while (stockName.Length <= 2) // No fancy brands here......
            {
                Console.Write("Please enter the name of the stock: ");
                stockName = Console.ReadLine();
            }

            s = stockName;
            sw.Write(s + "\t");

            while (stockQuantity < 1) // Running a small shop here...
            {
                Console.Write("Please enter the quanity of stock: ");
                stockQuantity = Convert.ToInt32(Console.ReadLine());
            }

            s = stockQuantity.ToString();
            sw.Write(s + "\t");

            while (stockPrice < 0.01) // Running a very small shop....
            {
                Console.Write("Please enter the price of the stock: ");
                stockPrice = Convert.ToDouble(Console.ReadLine());
            }

            s = stockPrice.ToString();
            sw.Write(s + "\t");

            sw.WriteLine(); // TO create the new line.....

        }

我实际上并没有这样做,所以你可能需要进行错误检查。

答案 1 :(得分:1)

您可以使用此代码。它会一直询问ID,直到用户输入一个尚未使用的ID:

//Get the input
static int GetIDInput()
{
    int id;
    do
    {
        Console.WriteLine("Please enter your desired ID number");
        id = Convert.ToInt32(Console.ReadLine());
    }
    while (isIDAlreadyUsed(id));
    return id;
}

// Check if ID is already used
public static bool isIDAlreadyUsed(int IDToCheck)
{
    using (StreamReader sr = new StreamReader("Stockfile.txt"))
    {
        while (!sr.EndOfStream)
        {
            string lineValues = sr.ReadLine();
            if(lineValues.Contains(IDToCheck.ToString())
                return true;
        }
    }
    return false;
}

static void AddStock()
{
    // Your init
    // ...
    int id = GetIDInput(); // Get the ID

    //... Your logic to apply

答案 2 :(得分:1)

对于那些感兴趣的人,@ plast1k有原始代码,只是根据我的需要编辑。

static int GetIDInput()
    {

        int tempID = 0;
        bool taken = false;
        bool isInputValid = false;
        string lineValues;

        while (isInputValid == false)
        {
            Console.WriteLine("Please enter your desired ID number");
            tempID = Convert.ToInt32(Console.ReadLine());

            if (tempID.ToString().Length != 8)
            {
                isInputValid = false;
                Console.WriteLine("ID number must be 8 digits long.");
            }
            else if (tempID.ToString().Length == 8)
            {
                isInputValid = true;
            }

            if (isInputValid) // this wont run if the input wasnt 8 characters, so the loop will restart
            {
                using (StreamReader sr = new StreamReader("Stockfile.txt"))
                {
                    while (sr.EndOfStream == false && taken != true)
                    {
                        lineValues = sr.ReadLine();

                        if (lineValues.Contains(tempID.ToString()))
                        {
                            taken = true;
                        }
                        else
                        {
                            taken = false;
                        }


                        if (taken == false)
                        {

                        }
                        else if (taken == true)
                        {
                            Console.WriteLine("Sorry this ID is already taken, try again");

                            isInputValid = false;
                            // statements will lead us back to the while loop and taken == true so it will run again
                        }
                        }
                }
            }
        }

        return tempID;
    }