Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.83 KB | None | 0 0
  1. using System;
  2.  
  3. namespace IMJunior
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int money = 1000;
  10.             string[] bag = new string[0];
  11.             string[] finalBag = new string[0];
  12.             string[][] arrayOfFinalBags = new string[0][];
  13.             int sum = 0;
  14.             bool exit = true;
  15.             Vegetables[] vegetables = { new Vegetables("Картофель", 30, 20), new Vegetables("Помидоры", 100, 20), new Vegetables("Огурцы", 70, 30)};
  16.             Salesman salesman = new Salesman();
  17.  
  18.             while (exit)
  19.             {
  20.                 MainMenu(ref money);
  21.                 Console.Write("Выбрать пункт: ");
  22.                 int chose1 = Convert.ToInt32(Console.ReadLine());
  23.  
  24.                 if (chose1 == 1)
  25.                 {
  26.                     Console.Clear();
  27.                     for (int i = 0; i < vegetables.Length; i++)
  28.                     {
  29.                         vegetables[i].ShowInfo();
  30.                     }
  31.  
  32.                     Console.Write("Выберите продукт: ");
  33.                     int chose2 = Convert.ToInt32(Console.ReadLine());
  34.  
  35.                     if (chose2 == 1)
  36.                     {
  37.                         Vegetables(vegetables, ref bag, 0, ref sum);
  38.                     }
  39.  
  40.                     else if (chose2 == 2)
  41.                     {
  42.                         Vegetables(vegetables, ref bag, 1, ref sum);
  43.                     }
  44.  
  45.                     else if (chose2 == 3)
  46.                     {
  47.                         Vegetables(vegetables, ref bag, 2, ref sum);
  48.                     }
  49.  
  50.                     else Console.WriteLine("Ошибка ввода!");
  51.                 }
  52.                 else if (chose1 == 2)
  53.                 {
  54.                     BagInfo(ref money, ref bag);
  55.                     Console.ReadLine();
  56.                     Console.Clear();
  57.                 }
  58.                 else if (chose1 == 3)
  59.                 {
  60.                     BagInfo(ref money, ref bag);
  61.                     Console.SetCursorPosition(70, 0);
  62.                     Console.Write("Какой товар убрать: ");
  63.                     int chose4 = Convert.ToInt32(Console.ReadLine());
  64.                     if (chose4 < 0 || chose4 > bag.Length)
  65.                     {
  66.                         Console.WriteLine("Неверный ввод!");
  67.                     }
  68.                     else
  69.                     {
  70.                         string[] temp = new string[bag.Length - 1];
  71.                         string[] temp2 = new string[bag.Length];
  72.                         temp2 = bag;
  73.                         string tempString = temp2[chose4 - 1];
  74.                         temp2[chose4 - 1] = temp2[temp2.Length - 1];
  75.                         temp2[temp2.Length - 1] = tempString;
  76.  
  77.                         sum -= Convert.ToInt32(temp2[temp2.Length - 1].Split(' ')[7]);
  78.  
  79.                         for(int i = 0; i < temp.Length; i++)
  80.                         {
  81.                             if(i < temp2.Length - 1)
  82.                             {
  83.                                 temp[i] = temp2[i];
  84.                             }
  85.                         }
  86.  
  87.                         bag = temp;
  88.                     }
  89.                     Console.Clear();
  90.                 }
  91.                 else if (chose1 == 4)
  92.                 {
  93.                     BagInfo(ref money, ref bag);
  94.                     Console.SetCursorPosition(70, 0);
  95.                     Console.WriteLine($"Всего получилось - {sum} рублей");
  96.                     Console.SetCursorPosition(70, 1);
  97.                     Console.Write("Желаете оплатить? Да/Нет: ");
  98.                     string answer = Console.ReadLine().ToLower();
  99.  
  100.                     if (answer == "да")
  101.                     {
  102.                         if (money >= sum)
  103.                         {
  104.                             money -= sum;
  105.                             sum = 0;                          
  106.                             salesman.BuyProducts(ref finalBag, ref bag, ref arrayOfFinalBags);
  107.                         }
  108.                         else
  109.                         {
  110.                             Console.SetCursorPosition(70, 2);
  111.                             Console.WriteLine("У вас недостаточно денег!");
  112.                         }
  113.                     }
  114.                     else if (answer == "нет")
  115.                     {
  116.                         Console.SetCursorPosition(70, 2);
  117.                         Console.WriteLine("Хорошо.");
  118.                     }
  119.                     else
  120.                     {
  121.                         Console.SetCursorPosition(70, 2);
  122.                         Console.WriteLine("Неверный ввод!");
  123.                     }
  124.  
  125.                     Console.ReadLine();
  126.                     Console.Clear();
  127.                 }
  128.                 else if (chose1 == 5)
  129.                 {
  130.                     Console.Clear();
  131.                     Console.WriteLine("Купленные продукты: ");
  132.                     for (int i = 0; i < arrayOfFinalBags.Length; i++)
  133.                     {
  134.                         for (int j = 0; j < arrayOfFinalBags[i].Length; j++)
  135.                         {
  136.                             Console.WriteLine(i + ") " + arrayOfFinalBags[i][j]);
  137.                         }
  138.                         Console.WriteLine();
  139.                     }
  140.                     Console.ReadLine();
  141.                     Console.Clear();
  142.                 }
  143.                 else if (chose1 == 6)
  144.                 {
  145.                     Console.Clear();
  146.                     Console.WriteLine("Всего доброго!");
  147.                     exit = false;
  148.                 }
  149.                 else Console.WriteLine("Неверный ввод!");
  150.             }
  151.         }
  152.  
  153.         static void MainMenu(ref int money)
  154.         {
  155.             Console.WriteLine("1) Овощной отдел");
  156.             Console.WriteLine("2) Корзина");
  157.             Console.WriteLine("3) Убрать из корзины");
  158.             Console.WriteLine("4) Продавец");
  159.             Console.WriteLine("5) Купленные товары");
  160.             Console.WriteLine("6) Уйти из магазина");
  161.             Console.WriteLine($"Ваши деньги: {money} рублей");
  162.         }
  163.  
  164.         static void Vegetables(Vegetables[] vegetables, ref string[] bag, int j, ref int sum)
  165.         {
  166.             Console.Write("Сколько кг: ");
  167.             int chose3 = Convert.ToInt32(Console.ReadLine());
  168.  
  169.             if (chose3 < 0 || chose3 > vegetables[j].AmountInKg)
  170.             {
  171.                 Console.WriteLine("Ошибка ввода!");
  172.                 Console.ReadLine();
  173.                 Console.Clear();
  174.             }
  175.             else
  176.             {
  177.                 vegetables[j].AmountInKg -= chose3;
  178.                 int localPrice = chose3 * vegetables[j].Price;
  179.                 string message = $"{vegetables[j].Name} в количестве {chose3} кг, стоимость - {localPrice}";
  180.                 string[] tempBag = new string[bag.Length + 1];
  181.                 sum += localPrice;
  182.  
  183.                 for (int i = 0; i < bag.Length; i++)
  184.                 {
  185.                     tempBag[i] = bag[i];
  186.                 }
  187.  
  188.                 tempBag[tempBag.Length - 1] = message;
  189.                 bag = tempBag;
  190.             }
  191.             Console.Clear();
  192.         }
  193.  
  194.         static void BagInfo(ref int money, ref string[] bag)
  195.         {
  196.             Console.Clear();
  197.             Console.WriteLine($"У вас денег: {money}. Ваша корзина: ");
  198.             for (int i = 0; i < bag.Length; i++)
  199.             {
  200.                 Console.WriteLine(bag[i]);
  201.             }
  202.         }
  203.     }
  204.  
  205.     class Shop
  206.     {
  207.         private int _price;
  208.  
  209.         public Shop(int price)
  210.         {
  211.             _price = price;
  212.         }
  213.     }
  214.  
  215.     class Salesman
  216.     {
  217.         public void BuyProducts(ref string[] finalBag, ref string[] curBag, ref string[][] arrayOfFinalBags)
  218.         {
  219.             string[] newBag = new string[0];
  220.             string[] tempBag = new string[curBag.Length];
  221.             string[][] tempArrayOfFinalBags = new string[arrayOfFinalBags.Length + 1][];
  222.  
  223.             for (int i = 0; i < curBag.Length; i++)
  224.             {
  225.                 tempBag[i] = curBag[i];
  226.             }
  227.  
  228.             finalBag = tempBag;
  229.  
  230.             for (int i = 0; i < arrayOfFinalBags.Length; i++)
  231.             {
  232.                 tempArrayOfFinalBags[i] = arrayOfFinalBags[i];
  233.                 for(int j = 0; j < arrayOfFinalBags[i].Length; j++)
  234.                 {
  235.                     tempArrayOfFinalBags[i][j] = arrayOfFinalBags[i][j];
  236.                 }
  237.             }
  238.  
  239.             tempArrayOfFinalBags[tempArrayOfFinalBags.Length - 1] = finalBag;
  240.             arrayOfFinalBags = tempArrayOfFinalBags;
  241.             curBag = newBag;
  242.         }
  243.     }
  244.  
  245.     class Vegetables : Shop
  246.     {
  247.         private int _price;
  248.         public int AmountInKg;
  249.         private string _name;
  250.  
  251.         public string Name
  252.         {
  253.             get
  254.             {
  255.                 return _name;
  256.             }
  257.             set
  258.             {
  259.                 _name = value;
  260.             }
  261.         }
  262.         public int Price
  263.         {
  264.             get
  265.             {
  266.                 return _price;
  267.             }
  268.             set
  269.             {
  270.                 _price = value;
  271.             }
  272.         }
  273.  
  274.         public Vegetables(string name, int price, int amountInKg) : base(price)
  275.         {
  276.             _name = name;
  277.             _price = price;
  278.             AmountInKg = amountInKg;
  279.         }
  280.  
  281.         public void ShowInfo()
  282.         {
  283.             Console.WriteLine($"{_name}: всего: {AmountInKg} кг, цена за 1 килограм: {_price}");
  284.         }
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement