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("Запись в файл завершенна."); } } } 

    2 answers 2

    • use Cyrillic - terrible practice
    • BagageList - not the most successful name of the structure, does not correctly display its essence
    • if you used .NET 4.0, you could use the default parameters in the constructor
    • Instead of fields it is better to use properties (Properties)
    • The checkWeight method is implemented and the following notation is used (I don’t remember its exact name) name:

       public bool СheckWeight() { retrun AverageWeight > Weight } 
    • and you did not overload the relationship operations

    • one
      The problem itself is solved correctly? - ArniLand
    • 2
      it's up to your teacher to decide - Specter
    • 2
      if the task is educational, it does not mean that the teacher asked me. Maybe I study by the textbook myself and find tasks on the Internet for myself. - ArniLand
    • one
      and really, did not take into account this situation, I apologize, but at the expense of> the last average weight of each passenger's baggage. in more detail: the average weight of all things? what for? why not total weight? - Specter
    • one
      I assumed it was just the weight of each passenger’s baggage. Although I have doubts about the bill. do not tell me what is implied in the condition. In the condition for saving to the output file, is it necessary to compare the average weight of the baggage of each individual passenger or the average weight of the baggage of all passengers? - ArniLand

    Allow with two pennies. The field "Average Weight" to what? If this is the total weight of the passenger's baggage, divided by the number of things, then it should be calculated and not be a field. At the same time, it should be calculated in the structure of the Baggage List. IMHO.

    • one
      I assumed it was just the weight of each passenger’s baggage. Although I have doubts about the bill. do not tell me what is implied in the condition. In the condition for saving to the output file, is it necessary to compare the average weight of the baggage of each individual passenger or the average weight of the baggage of all passengers? - ArniLand
    • one
      Regardless of the options, listen to the advice of Comrade. @BuilderC - Specter