2017-05-15 26 views
-1

さて、私は自分で作成したAutomobileクラスを使用しています。 List<Automobile>を使用しようとしています。 List<Automobile>を使用し、ユーザーの入力をAutomobileクラスに格納するこのプログラムを作成しようとしています。コードを実行してList<Automobile>に複数の車を入れようとすると、ユーザーが入力した前の車が上書きされます。私は自分のコードが混乱していることを知っています。私はList<>を使い、ファイルを書き込んだり読んだりするのは全く新しいです。List <>ユーザー入力が上書きされています

私の場合、私の話題はあまり明確ではありませんでした。私はなぜList<Automobile>が複数の車がList<Automobile>に書き込まれたときに書かれたままになっているのか理解しようとしています。

この問題を解決するには誰もが助けてくれてありがとう。

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

namespace Exercise6_DealerVehicleInventory 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
#region Variables/Constructors 

      var answer = ""; 
      Automobile car = new Automobile(); 
      List<Automobile> vehicle = new List<Automobile>(); 

#endregion 

#region User Car Input 

      /* I might be able to put all of this in one big while loop that way I 
      * can have more than one vehicle wrote to this file at a time. */ 

      Console.Write("Do you want to add a car?\nY for yes or N for no: "); 
      answer = Console.ReadLine(); 

      while (answer == "Y" || answer == "y") 
      { 
       Console.Write("\nEnter the make of the car: "); 
       car.Make = Console.ReadLine(); 

       Console.Write("Enter the model of the car: "); 
       car.Model = Console.ReadLine(); 

       Console.Write("Enter the color of the car: "); 
       car.Color = Console.ReadLine(); 

       Console.Write("Enter the year of the car: "); 
       car.Year = Convert.ToInt32(Console.ReadLine()); 

       Console.Write("Enter the mileage of the car: "); 
       car.Mileage = Convert.ToInt32(Console.ReadLine()); 

       vehicle.Add(car); 

       Console.Write("Do you want to add a car?\nY for yes or N for no: "); 
       answer = Console.ReadLine(); 
      } 

#endregion 

#region Delete From In Memory 

      Console.Write("\nDo you want to delete a car?\nY for yes or N for no: "); 
      answer = Console.ReadLine(); 

      if (answer == "Y" || answer == "y") 
      { 
       int i = 0; 
       int delEntry = 0; 
       foreach(Automobile automobile in vehicle) 
       { 
        Console.Write("#" + i + " = " + automobile.Make + " " + automobile.Model + " " + automobile.Color + " " + automobile.Year + " " + automobile.Mileage + "\n"); 
        i++; 
       } 
       Console.WriteLine("\nWhat item number would you like to delete: "); 
       delEntry = Convert.ToInt32(Console.ReadLine()); 
       vehicle.RemoveAt(delEntry); 
      } 

#endregion 

#region Write Data To File 

      Console.Write("\nDo you want to write this to a text file?\nY for yes or N for no: "); 
      answer = Console.ReadLine(); 

      string mydocpath = 
       Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 

      if (answer == "Y" || answer == "y") //(string.Equals("y", answer, StringComparison.CurrentCultureIgnoreCase)) 
      { 
       using (StreamWriter sw = new StreamWriter(mydocpath + @"\Vehicle.txt")) 
       { 
        foreach (Automobile automobile in vehicle) 
        { 
         sw.WriteLine(automobile.Make + " " + automobile.Model + " " + automobile.Color + " " + automobile.Year + " " + automobile.Mileage); 
        } 
       } 

      } 
      else 
      { 
       Console.WriteLine("No data wrote to file: "); 
      } 

#endregion 

#region Update Vehicle File 

      Console.Write("\nWould you like to add another vehicle?\nY for yes or N for no: "); 
      answer = Console.ReadLine(); 

      if (answer == "Y" || answer == "y") 
      { 
       StreamReader sr = new StreamReader(mydocpath + @"\Vehicle.txt"); 

       String line = sr.ReadLine(); 
       Console.Write("\n" + line); 

       while (answer == "Y" || answer == "y") 
       { 
        Console.Write("Enter the make of the car: "); 
        car.Make = Console.ReadLine(); 

        Console.Write("Enter the model of the car: "); 
        car.Model = Console.ReadLine(); 

        Console.Write("Enter the color of the car: "); 
        car.Color = Console.ReadLine(); 

        Console.Write("Enter the year of the car: "); 
        car.Year = Convert.ToInt32(Console.ReadLine()); 

        Console.Write("Enter the mileage of the car: "); 
        car.Mileage = Convert.ToInt32(Console.ReadLine()); 

        vehicle.Add(car); 

        Console.Write("\nDo you want to delete a car?\nY for yes or N for no: "); 
        answer = Console.ReadLine(); 

        if (answer == "Y" || answer == "y") 
        { 
         int i = 0; 
         int delEntry = 0; 
         foreach (Automobile automobile in vehicle) 
         { 
          Console.Write("#" + i + " = " + automobile.Make + " " + automobile.Model + " " + automobile.Color + " " + automobile.Year + " " + automobile.Mileage + "\n"); 
          i++; 
         } 
         Console.WriteLine("\nWhat item number would you like to delete: "); 
         delEntry = Convert.ToInt32(Console.ReadLine()); 
         vehicle.RemoveAt(delEntry); 
        } 

        Console.Write("\nDo you want to write this to a text file?\nY for yes or N for no: "); 
        answer = Console.ReadLine(); 

        if (answer == "Y" || answer == "y") //(string.Equals("y", answer, StringComparison.CurrentCultureIgnoreCase)) 
        { 
         using (StreamWriter sw = new StreamWriter(mydocpath + @"\Vehicle.txt", true)) 
         { 
          foreach (Automobile automobile in vehicle) 
          { 
           sw.WriteLine(automobile.Make + " " + automobile.Model + " " + automobile.Color + " " + automobile.Year + " " + automobile.Mileage); 
          } 
         } 

        } 
       } 
      } 
      else 
      { 
       Console.WriteLine("Nothing else was added to the file."); 
      } 
#endregion 

      Console.ReadLine(); 
     } 
    } 
} 

以下は私のAutomobileクラスです。

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

namespace Exercise6_DealerVehicleInventory 
{ 
    class Automobile 
    { 
     private string _make; 

     public string Make 
     { 
      get { return _make; } 
      set { _make = value; } 
     } 

     private string _model; 

     public string Model 
     { 
      get { return _model; } 
      set { _model = value; } 
     } 

     private string _color; 

     public string Color 
     { 
      get { return _color; } 
      set { _color = value; } 
     } 

     private int _year; 

     public int Year 
     { 
      get { return _year; } 
      set { _year = value; } 
     } 

     private int _mileage; 

     public int Mileage 
     { 
      get { return _mileage; } 
      set { _mileage = value; } 
     } 

    } 
} 
+0

はしばらくループ内で次の行を追加します。 '車は=新しい自動車();' –

答えて

1

あなたはそうあなただけの同じインスタンス上で動作し、それを上書きしている、あなたはwhileループを繰り返す新しいAutomobileたびにインスタンス化する必要があります。

この行を移動:ここに

Automobile car = new Automobile(); 

while (answer == "Y" || answer == "y") 
{ 
    Automobile car = new Automobile(); // create a new Automobile each time 

    Console.Write("\nEnter the make of the car: "); 
    car.Make = Console.ReadLine(); 
+0

ありがとうございました、それは私の問題を修正しました。とてもシンプルなので、私は捕まえたほうがいいと思うが、私はしなかった。再度、お手伝いいただきありがとうございます。今私は別のバグがあります。 – Daniel

+0

それはあなたのためのプログラミングです、それはしばしば他のバグとシンプルで、幸運なことです! – Matt

+0

あなたはこれまでに決して扱ったことのない問題を見つけようとしているときに、なぜ投票が下りますか? 私の質問に答えてくれて、あなたのシステムに多くのポイントがあるので、私はあなたに尋ねています。 – Daniel

1

それがリスト内Automobileオブジェクトのように見えるかもしれませんが上書き取得されますが、実際には一度だけだ、それを上書きするたびに、属性よりも同じオブジェクトの詳細を追加しています。

新しいオブジェクト、前Automobileオブジェクトの独立しを作るために、whileループ内の行Automobile car = new Automobile();を挿入する必要があります。

while (answer == "Y" || answer == "y") 
{ 
    Automobile car = new Automobile(); 
    ... 
    ... 
    ... 
+0

は、あなたの助けのためにあまりにもありがとうございます。これは私の問題を解決し、私は別の部分のデバッグに取り組んでいません。 – Daniel

関連する問題