Perform the task by structure. It seems to have executed correctly, but the code was pretty curved. Please give comments on the code and check the correctness of my assignment.
The task:
1. The task assumes that the source information is stored in a text file input.txt, each line of which contains complete information about a certain object, the resulting information must be written to the file output.txt.
2. To store data within the program to organize an array of structures.
3. In the type of structure, the CompareTo method of the IComparable interface is implemented, the ToString method of the base object class is overloaded and consider the necessary relation operations, data fields and additional methods independently.
On the basis of the data of the input file, make a baggage statement of the storage chamber, including the following data: the passenger's full name, the number of things, the total weight of things. Print in a new file information about those passengers whose average weight of baggage exceeds the specified one, sorting them out by the number of items handed over to the luggage room
Input file:
Anton, 6, 42 Nina, 5, 11 Vasa, 2, 34 Lusa, 3, 48
The first number is the number of items, the last average weight of each passenger’s baggage.
Output file:
ФИО Vasa, количество вещей в багаже 2, вес багажа 34 ФИО Lusa, количество вещей в багаже 3, вес багажа 48 ФИО Anton, количество вещей в багаже 6, вес багажа 42 namespace упражнения_2.Интерфейсы { struct BagageList : IComparable { string Name; int CountOfThings; public static double Weight; double AverageWeight; public BagageList(string Name, int CountOfThings, double AverageWeight) : this() { this.Name = Name; this.CountOfThings = CountOfThings; this.AverageWeight = AverageWeight; } public bool checkWeight() { if (AverageWeight > Weight) return true; else return false; } public int CompareTo(object obj) { BagageList person = (BagageList) obj; return this.CountOfThings.CompareTo(person.CountOfThings); } public override string ToString() { return String.Format("ФИО {0}, количество вещей в багаже {1}, вес багажа {2}\n", Name, CountOfThings, AverageWeight); } } class Program { static void Main(string[] args) { BagageList[] passanger = new BagageList[4]; string[] fileData = new string[3]; string[] fileData2 = new string[4]; int i = 0; int newSize = 0; string data = string.Empty; StreamReader file = new StreamReader(@"c:\passanger.txt"); Console.WriteLine("Считывание из файла."); while ((data = file.ReadLine()) != null) { string name = string.Empty; int things = 0; double weight = 0; fileData = data.Split(", ".ToCharArray()); name = fileData[0]; things = Convert.ToInt32(fileData[2]); weight = Convert.ToDouble(fileData[4]); passanger[i] = new BagageList(name, things, weight); i++; } foreach (BagageList p in passanger) { Console.Write(p.ToString()); } Console.WriteLine(); Array.Sort(passanger); BagageList.Weight = 30; for (int j = 0; j < fileData2.Length; j++) { if (!passanger[j].checkWeight()) { continue; } fileData2[j] = passanger[j].ToString(); if (!(fileData2[j] == null)) { newSize++; } } for (int j = 0; j < fileData2.Length - 1; j++) { string temp; if (fileData2[j] == null) { temp = fileData2[j + 1]; fileData2[j + 1] = fileData2[j]; fileData2[j] = temp; } } Array.Resize(ref fileData2, newSize); File.WriteAllLines(@"c:\passanger2.txt", fileData2); Console.WriteLine("Запись в файл завершенна."); } } }