Garloon

5.4

Sep 10th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _3._3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Product[] sellerProducts = { new Product("Яблоко", 3, 15), new Product("Молоко", 6, 15), new Product("Хлеб", 5, 15), new Product("Пиво", 15, 15), new Product("Колбаса", 20, 15) };
  14.             Product[] customerProducts = { new Product("Сигареты", 5)};
  15.  
  16.             Seller seller = new Seller(sellerProducts);
  17.             Customer customer = new Customer(1000, customerProducts);
  18.            
  19.             bool mainMenu = true, sellerMenu, customerMenu;
  20.             while (mainMenu == true)
  21.             {
  22.                 Console.Clear();
  23.                 Console.WriteLine("Главное меню");
  24.                 Console.WriteLine("\n1. Продавец\n2. Покупатель\n3. Выход");
  25.                 Console.Write("\nВаш выбор: ");
  26.                 switch (Convert.ToInt32(Console.ReadLine()))
  27.                 {
  28.                     case 1:
  29.                         sellerMenu = true;
  30.                         while (sellerMenu == true)
  31.                         {
  32.                             Console.Clear();
  33.                             Console.WriteLine("Меню продавца");
  34.                             Console.WriteLine("\n1. Показать товар\n2. Заполнить склад\n3. Назад");
  35.                             Console.Write("\nВаш выбор: ");
  36.                             switch (Convert.ToInt32(Console.ReadLine()))
  37.                             {
  38.                                 case 1:
  39.                                     Console.Clear();
  40.                                     Console.WriteLine("У вас на витрине: ");
  41.                                     seller.ShowAvailability();
  42.                                     Console.ReadKey();
  43.                                     break;
  44.                                 case 2:
  45.                                     seller.FillShelf();
  46.                                     ConsoleMessage("Витрины заполнены");
  47.                                     Console.ReadKey();
  48.                                     break;
  49.                                 case 3:
  50.                                     sellerMenu = false;
  51.                                     break;
  52.                                 default:
  53.                                     ConsoleMessage("Такой пункт меню отсутствует");
  54.                                     break;
  55.                             }
  56.                         }
  57.                         break;
  58.                     case 2:
  59.                         customerMenu = true;
  60.                         while (customerMenu == true)
  61.                         {
  62.                             Console.Clear();
  63.                             Console.WriteLine("Меню покупателя");
  64.                             Console.WriteLine("\nУ вас в кошельке: " + customer.Money + " монет");
  65.                             Console.WriteLine("\n1. Купить товар\n2. Показать содержимое пакета\n3. Назад");
  66.                             Console.Write("\nВаш выбор: ");
  67.                             switch (Convert.ToInt32(Console.ReadLine()))
  68.                             {
  69.                                 case 1:
  70.                                     Console.Clear();
  71.                                     Console.WriteLine("На витрине: ");
  72.                                     seller.ShowAvailability();
  73.                                     Console.Write("Название покупаемого продукта: ");
  74.                                     string uiName = Console.ReadLine();
  75.                                     Console.Write("Количество: ");
  76.                                     int uiCount = Convert.ToInt32(Console.ReadLine());
  77.                                     customer.BuyProduct(sellerProducts, uiName, uiCount);
  78.                                     Console.ReadKey();
  79.                                     break;
  80.                                 case 2:
  81.                                     Console.Clear();
  82.                                     Console.WriteLine("У вас в пакете: ");
  83.                                     customer.ShowAvailability();
  84.                                     Console.ReadKey();
  85.                                     break;
  86.                                 case 3:
  87.                                     customerMenu = false;
  88.                                     break;
  89.                                 default:
  90.                                     ConsoleMessage("Такой пункт меню отсутствует");
  91.                                     break;
  92.                             }
  93.                         }
  94.                         break;
  95.                     case 3:
  96.                         mainMenu = false;
  97.                         break;
  98.                     default:
  99.                         ConsoleMessage("Такой пункт меню отсутствует");
  100.                         break;
  101.                 }
  102.             }
  103.         }
  104.         static void ConsoleMessage(string message)
  105.         {
  106.             Console.ForegroundColor = ConsoleColor.Red;
  107.             Console.WriteLine(message);
  108.             Console.ResetColor();
  109.             Console.ReadKey();
  110.         }
  111.     }
  112.     class Product
  113.     {
  114.         public string Name;
  115.         public int Price;
  116.         public int Count;
  117.         public int CountOnPackage;
  118.         public Product(string name, int price, int count)
  119.         {
  120.             Name = name;
  121.             Price = price;
  122.             Count = count;
  123.         }
  124.         public Product(string name, int countOnPackage)
  125.         {
  126.             Name = name;
  127.             CountOnPackage = countOnPackage;
  128.         }
  129.     }
  130.     class Seller
  131.     {
  132.         public Product[] ProductsOnShelf;
  133.         public Seller(Product[] productsOnShelf)
  134.         {
  135.             ProductsOnShelf = productsOnShelf;
  136.         }
  137.         public void FillShelf()
  138.         {
  139.             foreach(Product product in ProductsOnShelf)
  140.             {
  141.                 product.Count = 15;
  142.             }
  143.         }
  144.         public void ShowAvailability()
  145.         {
  146.             foreach(Product product in ProductsOnShelf)
  147.             {
  148.                 Console.WriteLine("\n" + product.Name + " - " + product.Price + " монет" + "\nКоличество: " + product.Count + " штук");
  149.                 Console.WriteLine("=============================");
  150.             }
  151.         }
  152.     }
  153.     class Customer
  154.     {
  155.         public int Money;
  156.  
  157.         public Product[] ProductsOnPackage;
  158.         public Customer(int money, Product[] productsOnPackage)
  159.         {
  160.             Money = money;
  161.             ProductsOnPackage = productsOnPackage;
  162.         }
  163.         public void BuyProduct(Product[] sellerProducts, string product, int countProd)
  164.         {
  165.             for(int i = 0; i < sellerProducts.Length; i++)
  166.             {
  167.                 if(product == sellerProducts[i].Name && countProd <= sellerProducts[i].Count && Money >= (sellerProducts[i].Price * countProd))
  168.                 {
  169.                     for (int j = 0; j < sellerProducts.Length; j++)
  170.                     {
  171.                         if (product == sellerProducts[j].Name)
  172.                         {
  173.                             Money -= (sellerProducts[j].Price * countProd);
  174.                             sellerProducts[j].Count -= countProd;
  175.                         }
  176.                     }
  177.                     Product newProduct = new Product(product, countProd);
  178.                     Product[] tempBuyProducts = new Product[ProductsOnPackage.Length + 1];
  179.                     for (int j = 0; j < ProductsOnPackage.Length; j++)
  180.                     {
  181.                         tempBuyProducts[j] = ProductsOnPackage[j];
  182.                     }
  183.                     tempBuyProducts[tempBuyProducts.Length - 1] = newProduct;
  184.                     ProductsOnPackage = tempBuyProducts;
  185.                     Console.ForegroundColor = ConsoleColor.Green;
  186.                     Console.WriteLine("\nПокупка прошла успешно");
  187.                     Console.ResetColor();
  188.                 }
  189.                 else if(product == sellerProducts[i].Name && countProd > sellerProducts[i].Count)
  190.                 {
  191.                     Console.ForegroundColor = ConsoleColor.Red;
  192.                     Console.WriteLine("\nНедостаточно продуктов");
  193.                     Console.ResetColor();
  194.                 }
  195.                 else if (product == sellerProducts[i].Name && Money < (sellerProducts[i].Price * countProd))
  196.                 {
  197.                     Console.ForegroundColor = ConsoleColor.Red;
  198.                     Console.WriteLine("\nНедостаточно монет");
  199.                     Console.ResetColor();
  200.                 }
  201.             }
  202.         }
  203.         public void ShowAvailability()
  204.         {
  205.             foreach (Product product in ProductsOnPackage)
  206.             {
  207.                 Console.WriteLine("\n" + product.Name + " - " + product.CountOnPackage + " штук");
  208.                 Console.WriteLine("=============================");
  209.             }
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment