using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
namespace Lab18.Models
{
public class GSRecords
{
///
/// Массив объектов для связи
///
GSLink[] gs_Links;
///
/// Максимальное количество регистраций
///
int max_counts;
///
/// текущее количество регистраций
///
int cur_counts;
public GSLink[] Gs_Links
{
get { return gs_Links; }
}
//констурктор
public GSRecords(int max_counts)
{
this.max_counts = max_counts;
cur_counts = 0;
//выделение памяти под массив ссылок
gs_Links = new GSLink[max_counts];
//память под объекты будет выделяться при регистрации поставщиков
}
//проверка, существует ли регистрация поставщика
public bool GS_Find(int num_sp, int num_goods)
{
GSLink temp = new GSLink(num_sp, num_goods);
int i = 0;
while ((i < cur_counts) && !temp.Equals(gs_Links[i])) i++;
return i != cur_counts;
}
///
/// Регистрация желания поставщика продавать товар
///
/// true, if record was added, false otherwise.
/// ID поставщика
/// ID товара
public bool AddRecord(int num_sp, int num_gsr)
{
if (GS_Find(num_sp, num_gsr) || (cur_counts == max_counts))
{
// Добавить поставщика нельзя - либо он уже есть, либо
// нет памяти
return false;
}
gs_Links[cur_counts] = new GSLink(num_sp, num_gsr);
cur_counts++;
return true;
}
///
/// Печать информации обо всех регистрациях поставщиков
///
public void Display()
{
Console.WriteLine("Поставщик " + "товар " + "количество ");
for (int i = 0; i < cur_counts; i++)
{
Console.WriteLine(
"{0,4:d}{1,10:d}{2,9:d}",
gs_Links[i].S_id,
gs_Links[i].G_id,
gs_Links[i].Quantity);
}
}
///
/// Добавляет количество товару поставщика
///
/// true, if supply was added, false otherwise.
/// Good identifier.
/// Count.
/// Supplier identifier.
public bool AddSupply(int goodID, int count, int supplierID)
{
GSLink compareObj = new GSLink(supplierID, goodID);
for (int i = 0; i < cur_counts; i++)
{
if (gs_Links[i].Equals(compareObj))
{
// Найден объект связи поставщика с товаром
gs_Links[i].Quantity += count;
return true;
}
}
return false;
}
///
/// Возвращает информацию о товарах и их количествах
///
/// The max data.
public void GetGoodCountsData(out int goodID, out int goodCount)
{
var data = gs_Links.Where(l => l != null).GroupBy(l => l.G_id).Select(cl => new
{
GoodID = cl.Key,
GoodCount = cl.Sum(i => i.Quantity)
}).OrderByDescending(i => i.GoodCount).First();
goodID = data.GoodID;
goodCount = data.GoodCount;
}
}
}