Advertisement
MariusPure

K1_random

Oct 11th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 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. using System.IO;
  7.  
  8. namespace ANOTHER_Kontras
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. BookStore allBooks = InOut.InputBooks("Knygos.txt");
  15. List<Book> soldBooks = InOut.InputSoldBooks("Pardavimai.txt");
  16.  
  17. File.Delete("Data.txt");
  18. InOut.Print(allBooks, "Data.txt", "Knygynas");
  19. InOut.Print(soldBooks, "Data.txt", "Parduotos knygos");
  20.  
  21. allBooks.AddSalePrice(soldBooks);
  22. InOut.Print(soldBooks, "Data.txt", "Papildyta");
  23.  
  24. BookStore allSoldBooks = new BookStore(soldBooks);
  25. string moneyLeft = String.Format("Truksta Manio {0}", allBooks.Sum() - allSoldBooks.Sum());
  26. InOut.Print(allBooks, "Data.txt", "Knygynas");
  27. File.AppendAllText("Data.txt", moneyLeft);
  28.  
  29. }
  30. }
  31. class Book
  32. {
  33. public string Distributor { get; set; }
  34. public string Title { get; set; }
  35. public int Amount { get; set; }
  36. public decimal Price { get; set; }
  37.  
  38. public Book(string distributor, string title, int amount, decimal price)
  39. {
  40. this.Distributor = distributor;
  41. this.Title = title;
  42. this.Amount = amount;
  43. this.Price = price;
  44. }
  45. public override string ToString()
  46. {
  47. return string.Format("| {0, -18} | {1, -18} | {2, 8} | {3, 8} |", this.Distributor, this.Title, this.Amount, this.Price);
  48. }
  49. public static bool operator <=(Book lhs, Book rhs)
  50. {
  51. return lhs.Price <= rhs.Price;
  52. }
  53. public static bool operator >=(Book lhs, Book rhs)
  54. {
  55. return lhs.Price >= rhs.Price;
  56. }
  57. }
  58. class BookStore
  59. {
  60. private List<Book> allBooks = new List<Book>();
  61.  
  62. public BookStore(List<Book> books)
  63. {
  64. foreach (Book book in books)
  65. {
  66. allBooks.Add(book);
  67. }
  68. }
  69. public int GetCount()
  70. {
  71. return this.allBooks.Count;
  72. }
  73. public Book GetBook(int index)
  74. {
  75. return allBooks[index];
  76. }
  77. public void AddBook(Book book)
  78. {
  79. this.allBooks.Add(book);
  80. }
  81. public decimal Sum()
  82. {
  83. decimal max = 0;
  84. foreach (Book book in allBooks)
  85. {
  86. max += book.Price * book.Amount;
  87. }
  88. return max;
  89. }
  90. public int IndexMaxPrice(Book book)
  91. {
  92. int index = 0;
  93. decimal price = 0m;
  94. for (int i = 0; i < allBooks.Count; i++)
  95. {
  96. if(allBooks[i].Title == book.Title && price < allBooks[i].Price)
  97. {
  98. index = i;
  99. }
  100. }
  101. return index;
  102. }
  103. public void AddSalePrice(List<Book> books)
  104. {
  105. int index;
  106. foreach(Book book in books)
  107. {
  108. for (int i = 0; i < allBooks.Count; i++)
  109. {
  110. if (book.Title == allBooks[i].Title)
  111. {
  112. index = IndexMaxPrice(allBooks[i]);
  113. book.Price = allBooks[index].Price;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. static class InOut
  120. {
  121. public static BookStore InputBooks(string fileName)
  122. {
  123. List<Book> allBooks = new List<Book>();
  124. string[] lines = File.ReadAllLines(fileName);
  125. foreach(string line in lines)
  126. {
  127. string[] value = line.Split(';');
  128. string distributor = value[0];
  129. string title = value[1];
  130. int amount = int.Parse(value[2]);
  131. decimal price = decimal.Parse(value[3]);
  132.  
  133. Book book = new Book(distributor, title, amount, price);
  134. allBooks.Add(book);
  135. }
  136. BookStore bookStore = new BookStore(allBooks);
  137. return bookStore;
  138.  
  139. }
  140. public static List<Book> InputSoldBooks(string fileName)
  141. {
  142. List<Book> soldBooks = new List<Book>();
  143. string[] lines = File.ReadAllLines(fileName);
  144. for (int i = 0; i < lines.Length; i++)
  145. {
  146. Book book = new Book("", lines[i], 1, 0);
  147. soldBooks.Add(book);
  148. }
  149. return soldBooks;
  150. }
  151. public static void Print(BookStore books, string fileName, string header)
  152. {
  153. string[] lines = new string[books.GetCount() + 5];
  154. lines[0] = header;
  155. lines[1] = new string('-', 64);
  156. lines[2] = String.Format("| {0, -18} | {1, -18} | {2, 8} | {3, 8} |", "Platintojas", "Pavadinimas", "Kiekis", "Kaina");
  157. lines[3] = new string('-', 64);
  158. for (int i = 0; i < books.GetCount(); i++)
  159. {
  160. lines[i + 4] = books.GetBook(i).ToString();
  161. }
  162. lines[lines.Length - 1] = new string('-', 64);
  163. File.AppendAllLines(fileName, lines, Encoding.UTF8);
  164. }
  165. public static void Print(List<Book> books, string fileName, string header)
  166. {
  167. string[] lines = new string[books.Count + 5];
  168. lines[0] = header;
  169. lines[1] = new string('-', 64);
  170. lines[2] = String.Format("| {0, -18} | {1, -18} | {2, 8} | {3, 8} |", "Platintojas", "Pavadinimas", "Kiekis", "Kaina");
  171. lines[3] = new string('-', 64);
  172. for (int i = 0; i < books.Count; i++)
  173. {
  174. lines[i + 4] = books[i].ToString();
  175. }
  176. lines[lines.Length - 1] = new string('-', 64);
  177. File.AppendAllLines(fileName, lines, Encoding.UTF8);
  178. }
  179. }
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement