【问题标题】:update quantity and save array to a text file更新数量并将数组保存到文本文件
【发布时间】:2018-11-23 23:10:12
【问题描述】:

假设有给定的可用项目数量数组。

int [,] AvailableQuantity= new int[3,4]
{
    { 4, 5, 2, 3 },
    { 2, 7, 3, 4 },
    { 9, 3, 5, 6 }
};

如果用户购买了 [0, 0] 的 3 件商品,则 AvailableQuantity 数组必须更新为当前可用数量,如下所示:

{
    { 1, 5, 2, 3 },
    { 2, 7, 3, 4 },
    { 9, 3, 5, 6 }
};

同样,如果购买了 2 件商品,例如 [0, 1],则数组应如下所示:

{
    { 1, 3, 2, 3 },
    { 2, 7, 3, 4 },
    { 9, 3, 5, 6 }
};

每次购买和更新任何商品时,都必须更新数量。然后,当应用程序关闭时,数组必须保存在文本文件中,如下所示。

1 3 2 3
2 7 3 4
9 3 5 6

如何执行?

【问题讨论】:

  • 问题已更新。有 4 件商品以 3 种不同的包装尺寸和不同的价格出售。 price 数组显示了商品的所有价格。
  • 您可以定义一个名为 ProcessPurchase(int itemId, int qty) 的方法,该方法将执行以下操作:AvailableQuantity[itemId]-=qty。要打印到文件,您需要打开文件并向其中写入文本。有很多课程可以做到这一点。例如:stackoverflow.com/questions/13023147/…
  • 问题进一步修改,希望澄清!
  • 这样好多了。我对问题进行了进一步编辑,使其更易于阅读。

标签: c#


【解决方案1】:

答案似乎过于明显,所以我可能遗漏了一些东西。如果用户选择以 1,3 的价格购买商品,那么您相应地减少 1,3 的数量。这就是为什么你有两个数组,所以你可以重复使用索引 - 或者不?

然而,这种设计似乎也有缺陷。通常,您不会像那样放置两个并行数组。相反,您创建了一个自定义类“产品”,其中包含价格、数量、名称以及您以后可能需要的所有其他字段(例如用于显示它的名称)。然后你制作了Product[] Products = new Products[10]; 的数组。但是,您可能还没有学习如何编写类。

至于将整个内容保存到文本文件:XML 和 CSV 是此类相当简单数据的常用格式。对于少量数据,CSV 甚至可能更具可读性。但通常这应该是一个单独的问题。

【讨论】:

    【解决方案2】:
    using System;
    using System.IO;
    using System.Text;
    
    class Program
    
    {
        /// <summary>
        /// Static Multidimensional Array what have products quantity.
        /// </summary>
        static int [,] AvailableQuantity = new int[3,4]
        {
            { 4, 5, 2, 3 },
            { 2, 7, 3, 4 },
            { 9, 3, 5, 6 }
        };
    
        /// <summary>
        /// Main Console Call.
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
    
            string path = Environment.CurrentDirectory + "\\file.txt";
    
            // TEST
            Purchase_Item(3, 0, 0);
            Purchase_Item(2, 0, 1);
    
            Save_File(path, Generate_String_File());
    
            System.Diagnostics.Process.Start("notepad.exe", path);
    
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    
        /// <summary>
        /// Edit static int Multidimensional Array. (M.A)
        /// </summary>
        /// <param name="cuantity">The cuantity of the item what has ben purchased.</param>
        /// <param name="row">Row that you want to edit in the M.A.</param>
        /// <param name="column">Column that you want to edit in the M.A.</param>
        private static void Purchase_Item(int cuantity, int row, int column){
    
            try {
    
                if(row > AvailableQuantity.GetLength(0) || row < 0){
                    Console.WriteLine("Row - Out of Range");
                    return;
                }
    
                if(column > AvailableQuantity.GetLength(1) || column < 0){
                    Console.WriteLine("Column - Out of Range");
                    return;
                }
    
                int cuantity_in_row = AvailableQuantity[row, column];
    
                if(cuantity > cuantity_in_row){
                    Console.WriteLine("Not enough items!");
                    return;
                }
    
                AvailableQuantity[row, column] = cuantity_in_row - cuantity;
    
            } catch (Exception eX) {
    
                Console.WriteLine(eX.ToString());
            }
    
        }
    
        /// <summary>
        /// Generate string file, with the format (worst format ever).
        /// </summary>
        /// <returns>text that will be saved.</returns>
        private static string Generate_String_File(){
    
            string line = string.Empty;
            string full_text = string.Empty;
    
            for (int row = 0; row < AvailableQuantity.GetLength(0); row++) {
    
                line = string.Empty;
    
                for (int column = 0; column < AvailableQuantity.GetLength(1); column++) {
    
                    line += AvailableQuantity[row, column].ToString() + " ";
                }
    
                line = line.Remove(line.Length-1);
    
                full_text += line + Environment.NewLine;
            }
    
            full_text = full_text.Remove(full_text.Length-1);
    
            return full_text;
        }
    
        /// <summary>
        /// Save the file at the asing path.
        /// </summary>
        /// <param name="path">location where will go the file.</param>
        /// <param name="text">text that will be included at the file.</param>
        private static void Save_File(string path, string text){
    
            try {
    
                File.WriteAllText(path, text);
    
            } catch (Exception eX) {
    
                Console.WriteLine(eX.ToString());
            }
    
    
        }
    
    }
    

    我不是专家,但我认为它必须是这样的...... 请不要因为这样做而杀了我,这只是你想要的最糟糕的实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 2017-10-17
      • 2016-01-26
      • 2018-06-25
      相关资源
      最近更新 更多