我正在为项目创建一个假航行跟踪器,并且需要检查Flight.csv文件(所有单个航班)中的线之和是否与机场.csv文件中的NumOfflights变量匹配。如何从必须合并的两个文件中盘问这些数据?
这是相关的代码,如果需要更多的问题来弄清问题。
需要更多。static void Main()
{
Flight[] flight = new Flight[1000];
Airport[] airport = new Airport[1000];
int flightCount = 0;
int airportCount = 0;
ReadFlightFile(ref flight, ref flightCount);
ReadAirportFile(ref airport, ref airportCount);
int numRecords = flightCount;
OutputFlightMenu();
string userInput = "----";
while (userInput != "exit")
{
userInput = ReadLine().ToLower();
Clear();
OutputFlightMenu();
switch (userInput)
{
case "passengers":
//Stuff
break;
case "time":
//Stuff
break;
case "average":
//Stuff
break;
case "sub":
OutputSubMenu();
string userInput2 = "----";
while (userInput2 != "quit")
{
userInput2 = ReadLine().ToLower();
Clear();
OutputSubMenu();
switch (userInput2)
{
case "flight":
//Stuff
break;
case "count":
//Stuff
break;
case "airline":
//Stuff
break;
default:
WriteLine("Invalid input. Please try again.");
break;
}
}
Clear();
WriteLine("====Exiting System===");
break;
case "report":
//Stuff
break;
default:
WriteLine("Invalid input. Please try again.");
break;
}
}
Clear();
WriteLine("=====Goodbye=====");
ReadKey();
}
public static void ReadFlightFile(ref Flight[] flight, ref int flightCount)
{
string path = "Flight.csv";
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs);
while (sr.Peek() > -1)
{
string line = sr.ReadLine();
string[] flightArray = line.Split(',');
flight[flightCount] = new Flight(int.Parse(flightArray[0]), flightArray[1], int.Parse(flightArray[2]),
int.Parse(flightArray[3]), int.Parse(flightArray[4]));
flightCount++;
}
}
//Basic Read Method
public static void ReadAirportFile(ref Airport[] airport, ref int airportCount)
{
string path = "Airport.csv";
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs);
while (sr.Peek() > -1)
{
string line = sr.ReadLine();
string[] airportArray = line.Split(',');
airport[airportCount] = new Airport(int.Parse(airportArray[0]), airportArray[1], airportArray[2],
int.Parse(airportArray[3]), DateTime.Parse(airportArray[4]));
airportCount++;
}
}
这是来自相应文件的数据:
Flight.csv:
3446,三角洲,91,120,120
2789,联合,34,56,78
9078,美国,200,240,210
9079,美国,67,156,165
4567,西南,89,89,91
6799,三角洲,95,67,55
1265,联合,102,187,175
8421,美国,12,191,191
3332,西南,78,45,44
airport.csv:
9000,ATL,亚特兰大,9,12/9/2017
您可以看到,机场阵列中的NumOfflight元素(9)确实与航班的总和相匹配。
对于此项目的意图,我已经知道数据将匹配,但是我需要能够验证。
感谢任何可以提供帮助的人。
airport.Where(o=>o.numOfFlights==flightCount)
将返回具有等于FlightCount的NUMOFFLIGHT的机场。您应该使用列表而不是数组来存储航班和机场
更好的样式可以让您的读取方法接受文件名并返回列表或数组(其中集合的大小为您的Count
值):
public static IList<Flight> ReadFlightFile(string filePath)
{
var result = new List<Flight>();
foreach(var line in File.ReadLines(filePath))
{
string[] flightArray = line.Split(',');
result.Add(new Flight(int.Parse(flightArray[0]), flightArray[1], int.Parse(flightArray[2]),
int.Parse(flightArray[3]), int.Parse(flightArray[4]));
}
return result;
}
//Basic Read Method
public static IList<Airport> ReadAirportFile(string filePath)
{
var result = new List<Airport>();
foreach(string line in File.ReadLines(filePath))
{
string[] airportArray = line.Split(',');
result.Add(new Airport(int.Parse(airportArray[0]), airportArray[1], airportArray[2],
int.Parse(airportArray[3]), DateTime.Parse(airportArray[4]));
}
return result;
}
这样做,您可以将其添加到其他代码中:
static void Main()
{
var flights = ReadFlightFile("Flight.csv");
var airport = ReadAirportFile("Airport.csv");
if (airport[0].numOfFlights == flights.Count)
{
//things match
}
else
{
//things don't match
}
// rest of your code is down here:
}
这比您制作的要简单得多。
要计算flair.csv中的行数,您可以使用以下内容:
int lineCount = File.ReadAllLines("Your/Path/To/Flight.csv").Count();
如果您有标题,则应-1
要获取机场中的航班数量,只需将其打开,分成一个数组,然后抓住数字:
:string[] AirportContents = File.ReadAllText("Your/Path/To/Airport.csv").Split(',');
int flightsToday = Convert.ToInt32(AirportContents[3]);
如果有标题,则可以将文件内容放入列表中并读取第二个项目[1](但您没有在示例中放置标题)
)拥有这2个数字后,您可以对它们做任何您想做的事情。