123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- 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;
- }
- }
- 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;
- }
- }
- 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;
- }
- }
- class Category
- {
- public string Title { get; set; }
- public Category(string title)
- {
- Title = title;
- }
- }
- 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;
- }
- }
- 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;
- }
- }
- }
- }
|