さて、私は自分で作成した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; }
}
}
}
はしばらくループ内で次の行を追加します。 '車は=新しい自動車();' –