Class1.cs
namespace ClassLibrary1;
public class Table
{
public int Columns;
private string[] Title;
private int[] ColMax;
private List<String> Rows = new List<string>();
private string Result;
public Table(List<String> title) : this(title.Count)
{
SetTitle(title.ToArray());
}
public Table(string[] title) : this(title.Length)
{
SetTitle(title);
}
public Table(int columns)
{
Columns = columns;
}
private void CheckTitle()
{
if (Title == null)
{
throw new Exception("Title is not set");
}
}
public void SetTitle(List<String> title)
{
SetTitle(title.ToArray());
}
public void SetTitle(string[] title)
{
if (title.Length != Columns)
{
throw new Exception("Titles not equal columns");
}
else
{
Title = title;
ColMax = new int[Title.Length];
for (int t = 0; t < Title.Length; t++)
{
ColMax[t] = Title[t].Length;
}
}
}
public void AddRow(List<String> row)
{
AddRow(row.ToArray());
}
public void AddRow(string[] row)
{
CheckTitle();
for (int r = 0; r < row.Length; r++)
{
if (row[r].Length > ColMax[r])
{
ColMax[r] = row[r].Length;
}
Rows.Add(row[r]);
}
}
public void AddRows(List<String> row, bool withTitle = false)
{
AddRows(row.ToArray(), withTitle);
}
public void AddRows(string[] row, bool withTitle = false)
{
if (withTitle)
{
SetTitle(row[..Columns]);
row = row[Columns..];
}
CheckTitle();
for (int r = 0, i = 0; r < row.Length; r++, i++)
{
if (i == ColMax.Length) i = 0;
if (row[r].Length > ColMax[i])
{
ColMax[i] = row[r].Length;
}
Rows.Add(row[r]);
}
}
public string PrintTable()
{
var RowsT = Rows;
CheckTitle();
void PTLine(char cenCorner, char hLine= '═')
{
foreach (int e in ColMax)
{
Result += new string(hLine, e) + cenCorner;
}
}
Result += "\n" + '╔';
PTLine('╤');
Result += "\b" + '╗' + "\n" + '║';
//---
for (int t = 0; t < Title.Length; t++)
{
Result += Title[t] + new string(' ', ColMax[t] - Title[t].Length) + '│';
}
Result += "\b" + '║' + "\n" + '╟';
PTLine('┼', '─');
Result += "\b" + '╢' + "\n";
//---
int drIter = RowsT.Count / Columns;
for (int dr = 0; dr < drIter; dr++)
{
Result += '║';
for (int r = 0; r < Columns; r++)
{
Result += RowsT[r] + new string(' ', ColMax[r] - RowsT[r].Length) + '│';
}
RowsT = RowsT[Columns..];
Result += "\b" + '║' + "\n";
}
Result += '╚';
PTLine('╧');
Result += "\b" + '╝' + "\n";
return Result;
}
}
public class Customer
{
public string Name { get; set; }
public int Money { get; set; }
public int Gbalance { get; set; }
public Customer(string name, int money, int gbalance)
{
Name = name;
Money = money;
Gbalance = gbalance;
}
}
public class Goods
{
public string Name { get; set; }
public int Price { get; set; }
public int Amount { get; set; }
public string Category { get; set; }
public Goods(string name, int price, int amount, string category)
{
Name = name;
Price = price;
Amount = amount;
Category = category;
}
}
public class Category
{
public string Title { get; set; }
public Category(string title)
{
Title = title;
}
}
public class Seller
{
public string Name { get; set; }
public int Reviews { get; set; }
public List<Goods> GoodsList;
public Seller(string name, int reviews, List<Goods> goodsList)
{
Name = name;
GoodsList = goodsList;
Reviews = reviews;
}
}
Program.cs
using ClassLibrary1;
class Program
{
static int Ask(int end=0, int start=0, bool range = true)
{
while (true)
{
string ch;
int choice;
try
{
ch = Console.ReadLine();
if (ch == ":b") return -1;
else choice = Convert.ToInt32(ch);
}
catch
{
Console.Write("Неверный ввод, повторите: ");
continue;
}
if (range && choice <= end && choice >= start) return choice;
else if (range) Console.Write("Неверный ввод, повторите: ");
else return choice;
}
}
static void Main(string[] args)
{
List<Category> categories = new List<Category>()
{
new Category("Brawl Stars"),
new Category("Roblox"),
new Category("Minecraft"),
new Category("Pubg")
};
List<Goods> goods = new List<Goods>()
{
new Goods("Gems", 100, 123, categories[0].Title),
new Goods("Robux", 200, 321, categories[1].Title)
};
List<Seller> sellers = new List<Seller>()
{
new Seller("TopSell", 111, new List<Goods>() { goods[1]}),
new Seller("GameDealer", 222, new List<Goods>(){goods[0]})
};
List<Customer> customers = new List<Customer>()
{
new Customer("Maxim", 1000000, 0)
};
int indxC;
void InitTable()
{
Table t = new Table(new List<string> { "ID", "Категория", "Товар", "Кол-во", "Цена", "Продавец", "Отзывы" });
int indx = 1;
foreach (Seller sellerr in sellers)
{
foreach (Goods SelGood in sellerr.GoodsList)
{
if (SelGood.Amount != 0)
{
t.AddRow(new string[] { indx.ToString(), SelGood.Category, SelGood.Name, SelGood.Amount.ToString(), SelGood.Price.ToString(), sellerr.Name, sellerr.Reviews.ToString() });
}
indx++;
}
}
indxC = indx-1;
Console.WriteLine(t.PrintTable());
}
while (true)
{
start:
Console.Write("1: Режим просмотра.\n2: Режим создания.\n3: Купить товар.\n4: Баланс.\n(:b для возврата).\nВыберите режим: ");
int ch = Ask(4, 1);
if (ch == -1) break;
switch (ch)
{
case 1:
InitTable();
break;
case 2:
Console.WriteLine("\n\tДобавление товара.");
for (int k = 0; k < categories.Count; k++)
{
Console.WriteLine($"{k + 1}: {categories[k].Title}");
}
Console.Write("Выберите категорию: ");
int ch1 = Ask(categories.Count, 1);
if (ch1 == -1) break;
Console.WriteLine($"\n\tКатегория - {categories[ch1 - 1].Title}");
Console.Write("Название: ");
string gName = Console.ReadLine();
Console.Write("Количество: ");
int gAmount = Ask(range:false);
Console.Write("Цена: ");
int gPrice = Ask(range: false);
goods.Add(new Goods(gName, gPrice, gAmount, categories[ch1 - 1].Title));
Console.WriteLine("Товар был успешно добавлен.\n------\n");
for (int p = 0; p < sellers.Count; p++)
{
Console.WriteLine($"{p + 1}: {sellers[p].Name}");
}
Console.Write("Выберите продавца, которому необходимо добавить товар: ");
int ch2 = Ask(sellers.Count, 1);
if (ch2 == -1) break;
sellers[ch2-1].GoodsList.Add(goods[^1]);
Console.WriteLine("Товар был успешно зачислен.\n------\n");
break;
case 3:
InitTable();
Console.Write("\nВведите номер товара: ");
int ch3 = Ask(indxC, 1);
if (ch3 == -1) break;
Console.Write("Введите количество: ");
int ch3_1 = Ask(range: false);
if (ch3_1 <= 0) { Console.WriteLine("Количество товара должно быть больше 0"); break; }
for (int s = 0, gcount = 1; s < sellers.Count; s++)
{
for (int j = 0; j < sellers[s].GoodsList.Count; j++, gcount++)
{
if (gcount == ch3)
{
if (ch3_1 > sellers[s].GoodsList[j].Amount)
{
Console.WriteLine("У продавца недостаточно товара");
goto start;
}
if (customers[0].Money < sellers[s].GoodsList[j].Price * ch3_1)
{
Console.WriteLine("Недостаточно средств");
goto start;
}
sellers[s].GoodsList[j].Amount = sellers[s].GoodsList[j].Amount-ch3_1;
customers[0].Money = customers[0].Money - sellers[s].GoodsList[j].Price*ch3_1;
customers[0].Gbalance = customers[0].Gbalance + ch3_1;
Console.WriteLine("Вы успешно купили товар, с вашего счета списано {0}\n", sellers[s].GoodsList[j].Price*ch3_1);
}
}
}
break;
case 4:
Console.WriteLine("\nВаш баланс: {0} руб\nВаш игровой счет: {1}\n", customers[0].Money, customers[0].Gbalance);
break;
}
}
}
}