update quantity and save array to a text file












-1














Let's say, there are arrays of available item quantity given.



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


If a user purchases 3 items of [0, 0], then the AvailableQuantity array must be updated with the current available quantity as below:



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


Again, if 2 items of, for example, [0, 1] are purchased, then the array should look like below:



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


The quantity has to be updated each time when any item is purchased and updated. Then, the array has to be saved in the text file as shown below when the application is closed.



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


How can this be performed?










share|improve this question
























  • The Question is updated. There are 4 items are sold in 3 different packet size with different prices. The price array shows all the prices for the items.
    – Newbee
    Nov 23 '18 at 23:26










  • You could define a method called ProcessPurchase(int itemId, int qty), this method would do something like: AvailableQuantity[itemId]-=qty. To print to a file, you need to open the file and write text to it. There are many classes to do that. For example:stackoverflow.com/questions/13023147/…
    – NoChance
    Nov 23 '18 at 23:33












  • The question is further modified, Hope this clarifies!
    – Newbee
    Nov 23 '18 at 23:42










  • That's much better. I've edited the question further to make it easier to read.
    – Camilo Terevinto
    Nov 23 '18 at 23:46
















-1














Let's say, there are arrays of available item quantity given.



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


If a user purchases 3 items of [0, 0], then the AvailableQuantity array must be updated with the current available quantity as below:



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


Again, if 2 items of, for example, [0, 1] are purchased, then the array should look like below:



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


The quantity has to be updated each time when any item is purchased and updated. Then, the array has to be saved in the text file as shown below when the application is closed.



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


How can this be performed?










share|improve this question
























  • The Question is updated. There are 4 items are sold in 3 different packet size with different prices. The price array shows all the prices for the items.
    – Newbee
    Nov 23 '18 at 23:26










  • You could define a method called ProcessPurchase(int itemId, int qty), this method would do something like: AvailableQuantity[itemId]-=qty. To print to a file, you need to open the file and write text to it. There are many classes to do that. For example:stackoverflow.com/questions/13023147/…
    – NoChance
    Nov 23 '18 at 23:33












  • The question is further modified, Hope this clarifies!
    – Newbee
    Nov 23 '18 at 23:42










  • That's much better. I've edited the question further to make it easier to read.
    – Camilo Terevinto
    Nov 23 '18 at 23:46














-1












-1








-1







Let's say, there are arrays of available item quantity given.



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


If a user purchases 3 items of [0, 0], then the AvailableQuantity array must be updated with the current available quantity as below:



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


Again, if 2 items of, for example, [0, 1] are purchased, then the array should look like below:



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


The quantity has to be updated each time when any item is purchased and updated. Then, the array has to be saved in the text file as shown below when the application is closed.



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


How can this be performed?










share|improve this question















Let's say, there are arrays of available item quantity given.



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


If a user purchases 3 items of [0, 0], then the AvailableQuantity array must be updated with the current available quantity as below:



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


Again, if 2 items of, for example, [0, 1] are purchased, then the array should look like below:



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


The quantity has to be updated each time when any item is purchased and updated. Then, the array has to be saved in the text file as shown below when the application is closed.



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


How can this be performed?







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 23:46









Camilo Terevinto

18k63465




18k63465










asked Nov 23 '18 at 23:10









NewbeeNewbee

154




154












  • The Question is updated. There are 4 items are sold in 3 different packet size with different prices. The price array shows all the prices for the items.
    – Newbee
    Nov 23 '18 at 23:26










  • You could define a method called ProcessPurchase(int itemId, int qty), this method would do something like: AvailableQuantity[itemId]-=qty. To print to a file, you need to open the file and write text to it. There are many classes to do that. For example:stackoverflow.com/questions/13023147/…
    – NoChance
    Nov 23 '18 at 23:33












  • The question is further modified, Hope this clarifies!
    – Newbee
    Nov 23 '18 at 23:42










  • That's much better. I've edited the question further to make it easier to read.
    – Camilo Terevinto
    Nov 23 '18 at 23:46


















  • The Question is updated. There are 4 items are sold in 3 different packet size with different prices. The price array shows all the prices for the items.
    – Newbee
    Nov 23 '18 at 23:26










  • You could define a method called ProcessPurchase(int itemId, int qty), this method would do something like: AvailableQuantity[itemId]-=qty. To print to a file, you need to open the file and write text to it. There are many classes to do that. For example:stackoverflow.com/questions/13023147/…
    – NoChance
    Nov 23 '18 at 23:33












  • The question is further modified, Hope this clarifies!
    – Newbee
    Nov 23 '18 at 23:42










  • That's much better. I've edited the question further to make it easier to read.
    – Camilo Terevinto
    Nov 23 '18 at 23:46
















The Question is updated. There are 4 items are sold in 3 different packet size with different prices. The price array shows all the prices for the items.
– Newbee
Nov 23 '18 at 23:26




The Question is updated. There are 4 items are sold in 3 different packet size with different prices. The price array shows all the prices for the items.
– Newbee
Nov 23 '18 at 23:26












You could define a method called ProcessPurchase(int itemId, int qty), this method would do something like: AvailableQuantity[itemId]-=qty. To print to a file, you need to open the file and write text to it. There are many classes to do that. For example:stackoverflow.com/questions/13023147/…
– NoChance
Nov 23 '18 at 23:33






You could define a method called ProcessPurchase(int itemId, int qty), this method would do something like: AvailableQuantity[itemId]-=qty. To print to a file, you need to open the file and write text to it. There are many classes to do that. For example:stackoverflow.com/questions/13023147/…
– NoChance
Nov 23 '18 at 23:33














The question is further modified, Hope this clarifies!
– Newbee
Nov 23 '18 at 23:42




The question is further modified, Hope this clarifies!
– Newbee
Nov 23 '18 at 23:42












That's much better. I've edited the question further to make it easier to read.
– Camilo Terevinto
Nov 23 '18 at 23:46




That's much better. I've edited the question further to make it easier to read.
– Camilo Terevinto
Nov 23 '18 at 23:46












2 Answers
2






active

oldest

votes


















3














The answer seems overly obvious, so I might be missing something. If the user selects to buy the item at 1,3, then you reduce the quantity at 1,3 accordingly. That is why you have two arrays, so you can re-use the indexes - or not?



However this design also seems flawed. Generally you do not put up two paralell arrays like that. Instead you make a custom class "Product" that has fields for price, quantity, name and everything else you may need later (like a name to display it with). And then you make array of that Product Products = new Products[10];. However you might still be before learning how to write classes.



As for saving the whole thing to a textfile: XML and CSV are common formats for such rather simple data. For little amounts of data, CSV might even be slightly more readable. But generally this should be a seperate question.






share|improve this answer































    0














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


    }

    }


    I'm not an expert, but i supouse that it has to be something like that...
    Please do not kill me for do it, it's just the worst implemention for what do you want.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453755%2fupdate-quantity-and-save-array-to-a-text-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      The answer seems overly obvious, so I might be missing something. If the user selects to buy the item at 1,3, then you reduce the quantity at 1,3 accordingly. That is why you have two arrays, so you can re-use the indexes - or not?



      However this design also seems flawed. Generally you do not put up two paralell arrays like that. Instead you make a custom class "Product" that has fields for price, quantity, name and everything else you may need later (like a name to display it with). And then you make array of that Product Products = new Products[10];. However you might still be before learning how to write classes.



      As for saving the whole thing to a textfile: XML and CSV are common formats for such rather simple data. For little amounts of data, CSV might even be slightly more readable. But generally this should be a seperate question.






      share|improve this answer




























        3














        The answer seems overly obvious, so I might be missing something. If the user selects to buy the item at 1,3, then you reduce the quantity at 1,3 accordingly. That is why you have two arrays, so you can re-use the indexes - or not?



        However this design also seems flawed. Generally you do not put up two paralell arrays like that. Instead you make a custom class "Product" that has fields for price, quantity, name and everything else you may need later (like a name to display it with). And then you make array of that Product Products = new Products[10];. However you might still be before learning how to write classes.



        As for saving the whole thing to a textfile: XML and CSV are common formats for such rather simple data. For little amounts of data, CSV might even be slightly more readable. But generally this should be a seperate question.






        share|improve this answer


























          3












          3








          3






          The answer seems overly obvious, so I might be missing something. If the user selects to buy the item at 1,3, then you reduce the quantity at 1,3 accordingly. That is why you have two arrays, so you can re-use the indexes - or not?



          However this design also seems flawed. Generally you do not put up two paralell arrays like that. Instead you make a custom class "Product" that has fields for price, quantity, name and everything else you may need later (like a name to display it with). And then you make array of that Product Products = new Products[10];. However you might still be before learning how to write classes.



          As for saving the whole thing to a textfile: XML and CSV are common formats for such rather simple data. For little amounts of data, CSV might even be slightly more readable. But generally this should be a seperate question.






          share|improve this answer














          The answer seems overly obvious, so I might be missing something. If the user selects to buy the item at 1,3, then you reduce the quantity at 1,3 accordingly. That is why you have two arrays, so you can re-use the indexes - or not?



          However this design also seems flawed. Generally you do not put up two paralell arrays like that. Instead you make a custom class "Product" that has fields for price, quantity, name and everything else you may need later (like a name to display it with). And then you make array of that Product Products = new Products[10];. However you might still be before learning how to write classes.



          As for saving the whole thing to a textfile: XML and CSV are common formats for such rather simple data. For little amounts of data, CSV might even be slightly more readable. But generally this should be a seperate question.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 23:46

























          answered Nov 23 '18 at 23:41









          ChristopherChristopher

          2,6601622




          2,6601622

























              0














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


              }

              }


              I'm not an expert, but i supouse that it has to be something like that...
              Please do not kill me for do it, it's just the worst implemention for what do you want.






              share|improve this answer


























                0














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


                }

                }


                I'm not an expert, but i supouse that it has to be something like that...
                Please do not kill me for do it, it's just the worst implemention for what do you want.






                share|improve this answer
























                  0












                  0








                  0






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


                  }

                  }


                  I'm not an expert, but i supouse that it has to be something like that...
                  Please do not kill me for do it, it's just the worst implemention for what do you want.






                  share|improve this answer












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


                  }

                  }


                  I'm not an expert, but i supouse that it has to be something like that...
                  Please do not kill me for do it, it's just the worst implemention for what do you want.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 1:48









                  Th3r1p3rTh3r1p3r

                  11




                  11






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453755%2fupdate-quantity-and-save-array-to-a-text-file%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks