Advertisement
GigaOrts

Untitled

Nov 1st, 2023
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ЦветочныйМагазин
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Session session = new Session();
  11.             session.Make();
  12.         }
  13.     }
  14.  
  15.     class Session
  16.     {
  17.         private Seller _seller;
  18.         private Player _player;
  19.  
  20.         public Session()
  21.         {
  22.             _seller = new Seller();
  23.             _player = new Player();
  24.         }
  25.  
  26.         public void Make()
  27.         {
  28.             string showWalletMenu = "1";
  29.             string shoppingMenu = "2";
  30.             string showPurchasesMenu = "3";
  31.             string finishGameMenu = "4";
  32.  
  33.             bool isOpen = true;
  34.  
  35.             string userInput;
  36.  
  37.             while (isOpen)
  38.             {
  39.                 Console.WriteLine("Игра \"Цветочный магазин\"\n\nМеню:");
  40.                 Console.WriteLine($"{showWalletMenu} - посмотреть баланс кошелька");
  41.                 Console.WriteLine($"{shoppingMenu} - пойти за покупками");
  42.                 Console.WriteLine($"{showPurchasesMenu} - посмотреть свои покупки");
  43.                 Console.WriteLine($"{finishGameMenu} - закончить игру");
  44.                 Console.Write("\nНаберите нужную цифру меню: ");
  45.                 userInput = Console.ReadLine();
  46.  
  47.                 if (userInput == showWalletMenu)
  48.                     _player.ShowBalance();
  49.                 else if (userInput == shoppingMenu)
  50.                     GoBuying();
  51.                 else if (userInput == showPurchasesMenu)
  52.                     _player.ShowFlowers();
  53.                 else if (userInput == finishGameMenu)
  54.                     isOpen = false;
  55.                 else
  56.                     ShowErrorMessage();
  57.  
  58.                 if (userInput != shoppingMenu && userInput != finishGameMenu)
  59.                     ShowOfferToContinue();
  60.  
  61.                 Console.Clear();
  62.             }
  63.         }
  64.  
  65.         private void GoBuying()
  66.         {
  67.             string showProductsMenu = "1";
  68.             string tradingMenu = "2";
  69.             string goOutMenu = "3";
  70.  
  71.             bool isBuying = true;
  72.  
  73.             string userInput;
  74.  
  75.             Console.Clear();
  76.  
  77.             while (isBuying)
  78.             {
  79.                 Console.WriteLine("Мы рады видеть вас в нашем цветочном магазине! Уверены, вы останетесь довольны!\n");
  80.                 Console.WriteLine($"{showProductsMenu} - покажите товары!");
  81.                 Console.WriteLine($"{tradingMenu} - хочу купить!");
  82.                 Console.WriteLine($"{goOutMenu} - выйти из магазина!");
  83.                 Console.Write("\nНаберите нужную цифру меню: ");
  84.                 userInput = Console.ReadLine();
  85.  
  86.                 if (userInput == showProductsMenu)
  87.                     _seller.ShowFlowers();
  88.                 else if (userInput == tradingMenu)
  89.                     Trade();
  90.                 else if (userInput == goOutMenu)
  91.                     isBuying = false;
  92.                 else
  93.                     ShowErrorMessage();
  94.  
  95.                 if (userInput != goOutMenu)
  96.                     ShowOfferToContinue();
  97.  
  98.                 Console.Clear();
  99.             }
  100.         }
  101.  
  102.         private void Trade()
  103.         {
  104.             Product chosenFlower = _seller.GetProduct();
  105.  
  106.             if (_seller.IsFlowerExist(chosenFlower))
  107.             {
  108.                 if (_player.IsEnoughMoneyToBuy(chosenFlower))
  109.                 {
  110.                     Console.WriteLine($"\nВы выбрали замечательный цветок - {chosenFlower.Name}\nС вас {chosenFlower.Price} руб.");
  111.                     _seller.SellFlower(chosenFlower);
  112.                     _player.BuyFlower(chosenFlower);
  113.                 }
  114.                 else
  115.                 {
  116.                     Console.WriteLine("\nУ вас недостаточно денег!");
  117.                 }
  118.             }
  119.             else
  120.             {
  121.                 Console.WriteLine("\nИзвините, эти цветы закончились! Приходите завтра!");
  122.             }
  123.         }
  124.  
  125.         private void ShowOfferToContinue()
  126.         {
  127.             Console.WriteLine("\nДля продолжения нажмите любую клавишу.");
  128.             Console.ReadKey();
  129.         }
  130.  
  131.         private void ShowErrorMessage()
  132.         {
  133.             Console.WriteLine("\nКажется, вы ввели что-то не то.");
  134.         }
  135.     }
  136.  
  137.     class Seller
  138.     {
  139.         private List<Product> _flowers;
  140.         private Random _random = new Random();
  141.  
  142.         public Seller()
  143.         {
  144.             _flowers = new List<Product>();
  145.             FillShop();
  146.         }
  147.  
  148.         private void FillShop()
  149.         {
  150.             _flowers.Add(new Product(FlowerType.Роза, GetRandomPrice(FlowerType.Роза)));
  151.             _flowers.Add(new Product(FlowerType.Хризантема, GetRandomPrice(FlowerType.Хризантема)));
  152.             _flowers.Add(new Product(FlowerType.Пион, GetRandomPrice(FlowerType.Пион)));
  153.             _flowers.Add(new Product(FlowerType.Лилия, GetRandomPrice(FlowerType.Лилия)));
  154.             _flowers.Add(new Product(FlowerType.Тюльпан, GetRandomPrice(FlowerType.Тюльпан)));
  155.             _flowers.Add(new Product(FlowerType.Колокольчик, GetRandomPrice(FlowerType.Колокольчик)));
  156.             _flowers.Add(new Product(FlowerType.Ромашка, GetRandomPrice(FlowerType.Ромашка)));
  157.         }
  158.  
  159.         public void ShowFlowers()
  160.         {
  161.             int orderNumber = 1;
  162.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  163.             Console.WriteLine("\nО, у нас чудные цветы! Выбирайте любые!");
  164.             Console.ResetColor();
  165.  
  166.             foreach (Product flower in _flowers)
  167.             {
  168.                 Console.WriteLine($"{orderNumber}. {flower.Name} - цена за штуку: {flower.Price} руб. Всего: {flower.Amount} шт.");
  169.                 orderNumber++;
  170.             }
  171.         }
  172.  
  173.         public void SellFlower(Product chosenFlower)
  174.         {
  175.             ChangeAmount(chosenFlower);
  176.         }
  177.  
  178.         public bool IsFlowerExist(Product flower)
  179.         {
  180.             return flower.Amount != 0;
  181.         }
  182.  
  183.         public Product GetProduct()
  184.         {
  185.             int index;
  186.             ShowFlowers();
  187.             Console.Write("\nКакой цветок хотите? Напишите цифру: ");
  188.  
  189.             while (int.TryParse(Console.ReadLine(), out index) == false || index < 1 || index > _flowers.Count)
  190.             {
  191.                 Console.WriteLine("Вы ввели что-то не то, попробуйте еще раз!");
  192.             }
  193.  
  194.             return _flowers[index - 1];
  195.         }
  196.  
  197.         private void ChangeAmount(Product flower)
  198.         {
  199.             //item.DecreaseCount();
  200.         }
  201.  
  202.         private int GetRandomAmount()
  203.         {
  204.             int minAmount = 0;
  205.             int maxAmount = 6;
  206.  
  207.             return _random.Next(minAmount, maxAmount);
  208.         }
  209.  
  210.         private int GetRandomPrice(FlowerType flowerName)
  211.         {
  212.             Dictionary<FlowerType, RandomTuple> pricesByType = new();
  213.  
  214.             pricesByType.Add(FlowerType.Роза, new RandomTuple(500, 1000));
  215.  
  216.             if (pricesByType.ContainsKey(flowerName))
  217.             {
  218.                 int minPrice = pricesByType[flowerName].Min;
  219.                 int maxPrice = pricesByType[flowerName].Max;
  220.                 return _random.Next(minPrice, maxPrice);
  221.             }
  222.  
  223.             return 0;
  224.         }
  225.     }
  226.  
  227.     class RandomTuple
  228.     {
  229.         public RandomTuple(int min, int max)
  230.         {
  231.             Min = min;
  232.             Max = max;
  233.         }
  234.  
  235.         public int Min { get; }
  236.         public int Max { get; }
  237.     }
  238.  
  239.     class Randomizer
  240.     {
  241.         public static Random rand = new Random();
  242.  
  243.         public static int GenerateInt(int min, int max)
  244.         {
  245.             return rand.Next();
  246.         }
  247.     }
  248.  
  249.  
  250.     class Player
  251.     {
  252.         private List<Product> _bag;
  253.         private Random _random = new Random();
  254.         private int _money;
  255.  
  256.         public Player()
  257.         {
  258.             _money = SetMoneyAmount();
  259.             _bag = new List<Product>();
  260.         }
  261.  
  262.         public void ShowBalance()
  263.         {
  264.             Console.WriteLine($"\nУ вас на счету: {_money} руб.");
  265.         }
  266.  
  267.         public void ShowFlowers()
  268.         {
  269.             int orderNumber = 1;
  270.  
  271.             Console.WriteLine("В сумке:");
  272.  
  273.             foreach (Product flower in _bag)
  274.             {
  275.                 Console.WriteLine($"{orderNumber}. {flower.Name}. Всего: {flower.Amount} шт.");
  276.                 orderNumber++;
  277.             }
  278.         }
  279.  
  280.         public void BuyFlower(Product chosenFlower)
  281.         {
  282.             _money -= chosenFlower.Price;
  283.  
  284.             if (_bag.Count == 0)
  285.             {
  286.                 AddNewFlower(chosenFlower);
  287.             }
  288.             else
  289.             {
  290.                 int flowersCount = _bag.Count;
  291.                 int foundFlowers = 0;
  292.  
  293.                 for (int i = 0; i < flowersCount; i++)
  294.                 {
  295.                     if (_bag[i].Name == chosenFlower.Name)
  296.                     {
  297.                         _bag[i].Amount++;
  298.                         foundFlowers = 1;
  299.                     }
  300.                 }
  301.  
  302.                 if (foundFlowers == 0)
  303.                 {
  304.                     AddNewFlower(chosenFlower);
  305.                 }
  306.             }
  307.         }
  308.  
  309.         public bool IsEnoughMoneyToBuy(Product flower)
  310.         {
  311.             return _money - flower.Price > 0;
  312.  
  313.         }
  314.  
  315.         private void AddNewFlower(Product chosenFlower)
  316.         {
  317.             _bag.Add(chosenFlower);
  318.         }
  319.  
  320.         private int SetMoneyAmount()
  321.         {
  322.             int minMoneyAmount = 1000;
  323.             int maxMoneyAmount = 7001;
  324.  
  325.             return _random.Next(minMoneyAmount, maxMoneyAmount);
  326.         }
  327.     }
  328.  
  329.     class Product
  330.     {
  331.         public Product(FlowerType name, int price)
  332.         {
  333.             Name = name;
  334.             Price = price;
  335.         }
  336.  
  337.         public Product()
  338.         {
  339.  
  340.         }
  341.  
  342.         public FlowerType Name { get; private set; }
  343.         public int Price { get; private set; }
  344.     }
  345.  
  346.     class Cell
  347.     {
  348.         public Cell(Product product, int count)
  349.         {
  350.             Product = product;
  351.             Count = count;
  352.         }
  353.  
  354.         public Product Product { get; private set; }
  355.         public int Count { get; private set; }
  356.  
  357.         public void Decrease(int amount)
  358.         {
  359.             Count -= amount;
  360.         }
  361.         //public int Count => _products.Count;
  362.  
  363.         //public void Add(int amount)
  364.         //{
  365.         //    for (int i = 0; i < amount; i++)
  366.         //    {
  367.         //        //_products.Add(new Product());
  368.         //    }
  369.         //}
  370.  
  371.         //public void Remove(int amount)
  372.         //{
  373.         //    for (int i = 0; i < amount; i++)
  374.         //    {
  375.         //        _products.RemoveAt(_products.Count - i);
  376.         //    }
  377.         //}
  378.     }
  379.  
  380.     enum FlowerType
  381.     {
  382.         Роза,
  383.         Хризантема,
  384.         Пион,
  385.         Ромашка,
  386.         Тюльпан,
  387.         Лилия,
  388.         Колокольчик
  389.     }
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement