Advertisement
Guest User

Untitled

a guest
May 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.36 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace Kursach
  6. {
  7. class Product
  8. {
  9. string name;
  10. double price;
  11. int year;
  12. double volume;
  13. public Product(string n, double p, int y, double v)
  14. {
  15. name = n;
  16. price = p;
  17. year = y;
  18. volume = v;
  19. }
  20. public string Name
  21. {
  22. get
  23. {
  24. return name;
  25. }
  26. set
  27. {
  28. name = value;
  29. }
  30.  
  31. }
  32. public double Price
  33. {
  34. get
  35. {
  36. return price;
  37. }
  38. set
  39. {
  40. price = value;
  41. }
  42.  
  43. }
  44. public int Year
  45. {
  46. get
  47. {
  48. return year;
  49. }
  50. set
  51. {
  52. year = value;
  53. }
  54.  
  55. }
  56. public double Volume
  57. {
  58. get
  59. {
  60. return volume;
  61. }
  62. set
  63. {
  64. volume = value;
  65. }
  66. }
  67.  
  68. public void Display()
  69. {
  70. Console.WriteLine("Название: " + name);
  71. Console.WriteLine("Цена: " + price);
  72. Console.WriteLine("Год выпуска: " + year);
  73. Console.WriteLine("Объем: " + volume);
  74.  
  75. }
  76.  
  77. }
  78. class Sklad
  79. {
  80. public List<Product> products;
  81. double EPSILON = 1e-6;
  82. public List<string> op = new List<string>();
  83. public Sklad()
  84. {
  85. products = new List<Product>();
  86. }
  87. public void AddProduct(Product p)
  88. {
  89.  
  90. for (int i = 0; i < products.Count; i++) // ищем есть ли такой продукт на складе, если да то просто объем увеличиваем
  91. {
  92. if (products[i].Year == p.Year && Math.Abs(products[i].Price - p.Price) < EPSILON && products[i].Name == p.Name)
  93. {
  94. products[i].Volume += p.Volume;
  95. return;
  96. }
  97. }
  98.  
  99. products.Add(p);
  100. }
  101. public bool DeleteProduct(string name, int year, double price)
  102. {
  103. for (int i = 0; i < products.Count; i++)
  104. {
  105. if (products[i].Year == year && Math.Abs(products[i].Price - price) < EPSILON && products[i].Name == name)
  106. {
  107. products.RemoveAt(i);
  108. return true;
  109. }
  110.  
  111. }
  112. return false;
  113. }
  114. public void SortByYear()
  115. {
  116.  
  117. products.Sort((x, y) => x.Year.CompareTo(y.Year));
  118.  
  119. }
  120. public void SortByName()
  121. {
  122. products.Sort((x, y) => x.Name.CompareTo(y.Name));
  123.  
  124. }
  125. public void SortByPrice()
  126. {
  127. products.Sort( (x, y) => x.Price.CompareTo(y.Price));
  128.  
  129. }
  130. public void SortByVolume()
  131. {
  132. products.Sort( (x, y) => x.Volume.CompareTo(y.Volume));
  133.  
  134. }
  135.  
  136. public List<Product> FindByName(string name)
  137. {
  138. List<Product> ans = new List<Product>();
  139. for (int i = 0; i < products.Count; i++)
  140. {
  141. if (products[i].Name == name)
  142. ans.Add(products[i]);
  143.  
  144. }
  145. return ans;
  146. }
  147. public List<Product> FindByYear(int year)
  148. {
  149. List<Product> ans = new List<Product>();
  150. for (int i = 0; i < products.Count; i++)
  151. {
  152. if (products[i].Year == year)
  153. ans.Add(products[i]);
  154.  
  155. }
  156. return ans;
  157. }
  158. public List<Product> FindByPrice(double price)
  159. {
  160. List<Product> ans = new List<Product>();
  161. for (int i = 0; i < products.Count; i++)
  162. {
  163. if (Math.Abs(products[i].Price - price) < EPSILON)
  164. ans.Add(products[i]);
  165.  
  166. }
  167. return ans;
  168. }
  169.  
  170. public List<Product> FindByVolume(double volume)
  171. {
  172. List<Product> ans = new List<Product>();
  173. for (int i = 0; i < products.Count; i++)
  174. {
  175. if (Math.Abs(products[i].Volume - volume) < EPSILON)
  176. ans.Add(products[i]);
  177.  
  178. }
  179. return ans;
  180. }
  181.  
  182.  
  183.  
  184. public void DisplayInStock()// товары в наличии
  185. {
  186. Console.WriteLine("Информация о продуктах в наличии");
  187. bool wasprinted = false;
  188. for (int i = 0; i < products.Count; i++)
  189. if (products[i].Volume > 0)
  190. {
  191. products[i].Display();
  192. wasprinted = true;
  193. }
  194. if (wasprinted == false)
  195. Console.WriteLine("Продуктов в наличии нет");
  196.  
  197. }
  198. public void DisplayAll() // все товары
  199. {
  200.  
  201. Console.WriteLine("Информация о всех продуктах");
  202. if (products.Count == 0)
  203. Console.WriteLine("Продуктов в наличии нет");
  204. for (int i = 0; i < products.Count; i++)
  205. products[i].Display();
  206. }
  207. public void ReadFromFile(string fname)
  208. {
  209. StreamReader sr = new StreamReader(fname);
  210. while (!sr.EndOfStream)
  211. {
  212. string[] s = sr.ReadLine().Split();
  213. string name = s[0], price = s[1], year = s[2], volume = s[3];
  214.  
  215. Product p = new Product(name, double.Parse(price), int.Parse(year), double.Parse(volume));
  216. AddProduct(p);
  217.  
  218. }
  219. sr.Close();
  220. }
  221. public void PrintInFile(string fname)
  222. {
  223. StreamWriter sw = new StreamWriter(fname);
  224. for (int i = 0; i < products.Count; i++)
  225. {
  226.  
  227. sw.WriteLine(products[i].Name + " " + products[i].Price + " " + products[i].Year + " " + products[i].Volume);
  228. }
  229.  
  230. sw.Close();
  231. }
  232. public bool Buy(string name, double price, int year, double volume)
  233. {
  234. for (int i = 0; i < products.Count; i++)
  235. {
  236. if (products[i].Name == name && Math.Abs(products[i].Price - price) < EPSILON && products[i].Year == year && products[i].Volume >= volume)
  237. {
  238. products[i].Volume -= volume;
  239. return true;
  240. }
  241. }
  242.  
  243.  
  244.  
  245. return false;
  246. }
  247.  
  248. }
  249.  
  250. class MainClass
  251. {
  252. public static int getCommand()
  253. {
  254. int x;
  255. while (true)
  256. {
  257. Console.WriteLine("Для просмотра продуктов в наличии нажмите 1");
  258. Console.WriteLine("Для просмотра всех продуктов нажмите 2");
  259. Console.WriteLine("Для добавления продукта нажмите 3");
  260. Console.WriteLine("Для удаления продукта нажмите 4");
  261. Console.WriteLine("Для поиска продукта нажмите 5");
  262. Console.WriteLine("Для сортировки продуктов нажмите 6");
  263. Console.WriteLine("Для покупки товаров нажмите 7");
  264. Console.WriteLine("Для выхода нажмите 0");
  265. try
  266. {
  267. x = int.Parse(Console.ReadLine());
  268. }
  269. catch
  270. {
  271. continue;
  272. }
  273. if (x >= 0 && x <= 7)
  274. break;
  275.  
  276. }
  277. return x;
  278. }
  279.  
  280. public static int FindParametr(string ask)
  281. {
  282. int x;
  283. while (true)
  284. {
  285. Console.WriteLine("Для {0} по названию нажмите 1", ask);
  286. Console.WriteLine("Для {0} по году выпуска нажмите 2", ask);
  287. Console.WriteLine("Для {0} по цене нажмите 3", ask);
  288. Console.WriteLine("Для {0} по объему нажмите 4", ask);
  289.  
  290. Console.WriteLine("Для выхода нажмите 0");
  291. try
  292. {
  293. x = int.Parse(Console.ReadLine());
  294. }
  295. catch
  296. {
  297. continue;
  298. }
  299. if (x >= 0 && x <= 4)
  300. break;
  301.  
  302. }
  303. return x;
  304. }
  305. public static int onemore()
  306. {
  307. int x;
  308. while (true)
  309. {
  310. Console.WriteLine("Если вы хотите еще что то купить нажмите 1");
  311.  
  312.  
  313. Console.WriteLine("Для выхода нажмите 0");
  314. try
  315. {
  316. x = int.Parse(Console.ReadLine());
  317. }
  318. catch
  319. {
  320. continue;
  321. }
  322. if (x >= 0 && x <= 1)
  323. break;
  324.  
  325. }
  326. return x;
  327. }
  328.  
  329. public static void Main(string[] args)
  330. {
  331.  
  332. string fname = "input.txt";
  333. Sklad s = new Sklad();
  334. s.ReadFromFile(fname);
  335. Console.WriteLine("Здравствуйте!");
  336. int x, y;
  337. string name;
  338. double volume, price;
  339. int year;
  340. Product p;
  341. List<Product> prods;
  342. double totalprice = 0;
  343. while (true)
  344. {
  345. x = getCommand();
  346. if (x == 0)
  347. {
  348. Console.WriteLine("Спасибо за работу с нами. До свидания!");
  349. s.PrintInFile(fname);
  350. break;
  351. }
  352. if (x == 1)
  353. {
  354.  
  355. s.DisplayInStock();
  356.  
  357.  
  358. }
  359. else if (x == 2)
  360. {
  361.  
  362. s.DisplayAll();
  363.  
  364. }
  365. else if (x == 3)
  366. {
  367. Console.WriteLine("Введите название продукта");
  368. name = Console.ReadLine();
  369. Console.WriteLine("Введите стоимость продукта");
  370. price = double.Parse(Console.ReadLine());
  371. Console.WriteLine("Введите год выпуска продукта");
  372. year = int.Parse(Console.ReadLine());
  373. Console.WriteLine("Введите объем продукта");
  374. volume = double.Parse(Console.ReadLine());
  375. p = new Product(name, price, year, volume);
  376. s.AddProduct(p);
  377. }
  378. else if (x == 4)
  379. {
  380. Console.WriteLine("Введите название продукта");
  381. name = Console.ReadLine();
  382. Console.WriteLine("Введите стоимость продукта");
  383. price = double.Parse(Console.ReadLine());
  384. Console.WriteLine("Введите год выпуска продукта");
  385. year = int.Parse(Console.ReadLine());
  386.  
  387. bool deleted = s.DeleteProduct(name, year, price);
  388. if (deleted)
  389. Console.WriteLine("Удаление прошло успешно");
  390. else
  391. Console.WriteLine("Данный продукт не найден");
  392.  
  393. }
  394. else if (x == 5)
  395. {
  396. y = FindParametr("поиска");
  397. if (y == 0)
  398. continue;
  399. if (y == 1)
  400. {
  401. Console.WriteLine("Введите название продукта");
  402. name = Console.ReadLine();
  403. prods = s.FindByName(name);
  404. }
  405. else if (y == 2)
  406. {
  407.  
  408. Console.WriteLine("Введите год выпуска продукта");
  409. year = int.Parse(Console.ReadLine());
  410. prods = s.FindByYear(year);
  411. }
  412. else if (y == 3)
  413. {
  414. Console.WriteLine("Введите стоимость продукта");
  415. price = double.Parse(Console.ReadLine());
  416.  
  417. prods = s.FindByPrice(price);
  418. }
  419. else
  420. {
  421.  
  422. Console.WriteLine("Введите объем продукта");
  423. volume = double.Parse(Console.ReadLine());
  424. prods = s.FindByVolume(volume);
  425. }
  426. if (prods.Count == 0)
  427. {
  428. Console.WriteLine("По запрашиваему параметру продуктов не найдено");
  429. }
  430. else
  431.  
  432. {
  433. Console.WriteLine("Найденные продукты");
  434. for (int i = 0; i < prods.Count; i++)
  435. prods[i].Display();
  436. }
  437. }
  438. else if (x == 6)
  439. {
  440. y = FindParametr("сортировки");
  441. if (y == 0)
  442. continue;
  443. if (y == 1)
  444. {
  445.  
  446. s.SortByName();
  447. }
  448. else if (y == 2)
  449. {
  450.  
  451. s.SortByYear();
  452. }
  453. else if (y == 3)
  454. {
  455. s.SortByPrice();
  456. }
  457. else
  458. {
  459.  
  460. s.SortByVolume();
  461. }
  462.  
  463.  
  464. Console.WriteLine("Продукты после сортировки");
  465. s.DisplayAll();
  466. }
  467. else
  468. {
  469. totalprice = 0;
  470. do
  471. {
  472. Console.WriteLine("Введите название продукта");
  473. name = Console.ReadLine();
  474. Console.WriteLine("Введите стоимость продукта");
  475. price = double.Parse(Console.ReadLine());
  476. Console.WriteLine("Введите год выпуска продукта");
  477. year = int.Parse(Console.ReadLine());
  478. Console.WriteLine("Введите объем продукта");
  479. volume = double.Parse(Console.ReadLine());
  480. bool success = s.Buy(name, price, year, volume);
  481. if (success)
  482. {
  483. Console.WriteLine("Покупка прошла успешно");
  484. totalprice += volume * price;
  485. Console.WriteLine("Общий счет за покупку: {0}", totalprice);
  486.  
  487. }
  488. else
  489. {
  490. Console.WriteLine("Данного товара в таком количестве нет на складе");
  491. }
  492. y = onemore();
  493. if (y == 0)
  494. break;
  495. }
  496. while (true);
  497. }
  498. }
  499.  
  500. }
  501. }
  502. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement