Создание массива объектов класса - C#
Формулировка задачи:
Помогите плиз вот задача
Разработка программы для обработки сведения .
Для этого необходимо описать класс, поля которого соответствуют выходным полям сведения. Для установки значений полей должен использоваться конструктор. Вычисление значений расчетных полей сведения, получения значений выходных полей должно выполняться с помощью соответствующих не статических методов класса.
Программа должна обеспечивать создание массива объектов этого класса, ввод исходных данных с консоли и вывод на консоль исходных значений и полей, рассчитываемых каждой из записей сведения, а также итоговой информации по ведомости.
Вот код у меня получилось сделать толька для 1 объекта но не для массива, не знаю как сделать чтоб посчитало среднюю стоимость перевозки, количество всех пассажиров и расходов на рейс.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CDCollection
{
class flight
{
private string T;
private int R;
private int Z;
private int K;
public flight(string T, int R, int Z,int K)
{
this.T = T;
this.R = R;
this.Z = Z;
this.K = K;
}
public String GetT()
{ return T; }
public int GetR()
{ return R; }
public int GetZ()
{
return Z;
}
public int GetK()
{
return K;
}
public double AveragePrice (double S)
{
return S = Z / K;
}
}
public class FairyTale
{
public static void Main()
{
flight LittlePlane = new flight("Ang256",351,50000,231);
Console.WriteLine(LittlePlane.GetT() + ", " + LittlePlane.GetR() + ", " + LittlePlane.GetZ()+","+LittlePlane.GetK());
Console.Read();
}
}
}Решение задачи: «Создание массива объектов класса»
textual
Листинг программы
using System;
using System.Linq;
class Flight
{
public string T { get; private set; }
public int R { get; private set; }
public int Z { get; private set; }
public int K { get; private set; }
public double S { get { return (double)Z / K; } }
public Flight(string T, int R, int Z, int K)
{
this.T = T;
this.R = R;
this.Z = Z;
this.K = K;
}
}
class Program
{
static void Main()
{
// Массив:
var flights = new Flight[ReadInt("Общее число")];
for (int i = 0; i < flights.Length; i++)
{
Console.WriteLine();
Console.WriteLine($"Информация о №{i + 1}:");
flights[i] = new Flight(
ReadString("T. Тип"),
ReadInt("R. Номер Рейса"),
ReadInt("Z. Общая стоимость"),
ReadInt("K. Число мест"));
}
var template = "{0,4} |{1,15} |{2,15} |{3,15} |{4,15} |{5,15:F2}";
var line = new string('=', 89);
Console.WriteLine();
Console.WriteLine("Сводка:");
Console.WriteLine(template, "№", "T", "R", "Z", "K", "S");
Console.WriteLine(line);
for (int i = 0; i < flights.Length; i++)
{
var f = flights[i];
Console.WriteLine(template, i + 1, f.T, f.R, f.Z, f.K, f.S);
}
Console.WriteLine(line);
Console.WriteLine(template, "", "", "",
flights.Sum(f => f.Z),
flights.Sum(f => f.K),
flights.Average(f => f.S));
Console.ReadLine();
}
static string ReadString(string Label) => ReadValue(Label, s => s);
static int ReadInt(string Label) => ReadValue(Label, int.Parse);
static T ReadValue<T>(string Label, Func<string, T> Pipe = null)
{
while (true)
{
Console.Write($"{Label}: ");
try { return Pipe(Console.ReadLine()); }
catch
{
Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.WriteLine(new string(' ', Console.BufferWidth));
Console.SetCursorPosition(0, Console.CursorTop - 2);
}
}
}
}