Advertisement
MariusPure

Untitled

Oct 12th, 2020
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using System.IO;
  6. namespace K1
  7. {
  8. class Book
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public string Creator { get; set; }
  14. public string Name { get; set; }
  15. public int Count { get; set; }
  16. public decimal SinglePrice { get; set; }
  17.  
  18. public Book(string creator, string name, int count, decimal price)
  19. {
  20. this.Name = name;
  21. this.Count = count;
  22. this.SinglePrice = price;
  23. this.Creator = creator;
  24. }
  25. public Book(string name, int count, decimal price)
  26. {
  27. this.Name = name;
  28. this.Count = count;
  29. this.SinglePrice = price;
  30. }
  31.  
  32. public override string ToString()
  33. {
  34. return string.Format("{0,-18} |{1,-20}|{2,8}|{3,10}|", Creator, Name, Count, SinglePrice);
  35. }
  36.  
  37. public static bool operator <=(Book lhs, Book rhs)
  38. {
  39. return (lhs.SinglePrice <= rhs.SinglePrice) && (lhs.Name == rhs.Name) && (lhs.Count > 0 && rhs.Count > 0);
  40. }
  41.  
  42. public static bool operator >=(Book lhs, Book rhs)
  43. {
  44. return (lhs.SinglePrice >= rhs.SinglePrice) && (lhs.Name == rhs.Name) && (lhs.Count > 0 && rhs.Count > 0);
  45.  
  46. }
  47.  
  48.  
  49. }
  50.  
  51. class BookStore
  52. {
  53. private List<Book> Books = new List<Book>();
  54.  
  55. public BookStore(List<Book> books)
  56. {
  57. foreach(Book book in books)
  58. {
  59. this.Books.Add(book);
  60. }
  61. }
  62.  
  63. public BookStore()
  64. {
  65. Books = new List<Book>();
  66. }
  67.  
  68.  
  69. public int GetCount()
  70. {
  71. return this.Books.Count;
  72. }
  73.  
  74. public Book GetBook(int index)
  75. {
  76. return this.Books[index];
  77. }
  78.  
  79. public void AddBook(Book book)
  80. {
  81. this.Books.Add(book);
  82. }
  83.  
  84. public decimal Sum()
  85. {
  86. decimal tempSum = 0;
  87.  
  88. foreach (Book book in Books)
  89. {
  90. tempSum += book.SinglePrice * book.Count;
  91. }
  92.  
  93. return tempSum;
  94. }
  95.  
  96. public int IndexMaxPrice(Book book)
  97. {
  98. int index = -1;
  99. for (int i = 0; i < Books.Count; i++)
  100. {
  101. // (lhs.SinglePrice >= rhs.SinglePrice) && (lhs.Name == rhs.Name) && (lhs.Count > 0 && rhs.Count > 0);
  102. if (Books[i] >= book)
  103. {
  104. index = i;
  105. }
  106. }
  107.  
  108. return index;
  109. }
  110.  
  111. public void AddSalePrice(List<Book> books)
  112. {
  113. for(int i = 0; i < books.Count; i++)
  114. {
  115. int index = IndexMaxPrice(books[i]);
  116. if (index != -1)
  117. {
  118. books[i].SinglePrice = Books[index].SinglePrice;
  119. Books[index].Count--;
  120. }
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// Class contaiting methods for results input/output
  126. /// </summary>
  127. class InOut
  128. {
  129. public static BookStore InputBooks(string fileName)
  130. {
  131.  
  132. List<Book> books = new List<Book>();
  133.  
  134. string[] lines = File.ReadAllLines(fileName, Encoding.UTF8);
  135. string creator, name;
  136. int count;
  137. decimal price;
  138.  
  139. for (int i = 0; i < lines.Length; i++)
  140. {
  141. string[] parts = lines[i].Split(';');
  142.  
  143. creator = parts[0];
  144. name = parts[1];
  145. count = int.Parse(parts[2]);
  146. price = decimal.Parse(parts[3]);
  147.  
  148. Book book = new Book(creator, name, count, price);
  149. books.Add(book);
  150. }
  151.  
  152. BookStore store = new BookStore(books);
  153.  
  154. return store;
  155.  
  156. }
  157. public static List<Book> InputSoldBooks(string fileName)
  158. {
  159. string[] lines = File.ReadAllLines(fileName, Encoding.UTF8);
  160.  
  161. List<Book> soldBooks = new List<Book>();
  162.  
  163. for (int i = 0; i < lines.Length; i++)
  164. {
  165. Book book = new Book(lines[i],1,0);
  166. soldBooks.Add(book);
  167. }
  168.  
  169. return soldBooks;
  170. }
  171.  
  172. public static void Print(BookStore books, string fileName, string header)
  173. {
  174. string line = "";
  175.  
  176. line += header + "\n";
  177. line += new string('-', 50) + "\n";
  178. line += string.Format("{0,-18} {1,-20} {2,8} {3,10}", "Platintojas", "Pavadinimas", "Kiekis", "Kaina") + "\n";
  179.  
  180. line += new string('-', 50) + "\n";
  181.  
  182. for (int i = 0; i < books.GetCount(); i++)
  183. {
  184. line += books.GetBook(i).ToString() + "\n";
  185. }
  186. line += new string('-', 50) + "\n";
  187.  
  188. File.AppendAllText(fileName, line, Encoding.UTF8);
  189. }
  190.  
  191. public static void Print(List<Book> books, string fileName, string header)
  192. {
  193. string line = "";
  194.  
  195. line += header + "\n";
  196. line += new string('-', 50) + "\n";
  197.  
  198. line += string.Format("{0,-18} {1,-20} {2,8} {3,10}", "Platintojas", "Pavadinimas", "Kiekis", "Kaina") + "\n";
  199.  
  200. line += new string('-', 50) + "\n";
  201.  
  202. for (int i = 0; i < books.Count; i++)
  203. {
  204. line += books[i].ToString() + "\n";
  205. }
  206.  
  207. line += new string('-', 50) + "\n";
  208.  
  209. File.AppendAllText(fileName, line, Encoding.UTF8);
  210. }
  211. }
  212. class Program
  213. {
  214. static void Main(string[] args)
  215. {
  216. File.Delete("Duomenys.txt");
  217. BookStore bookStore = InOut.InputBooks("Knyga.txt");
  218. List<Book> soldBooks = InOut.InputSoldBooks("Parduota.txt");
  219.  
  220. InOut.Print(bookStore, "Duomenys.txt", "Pradinis knyogs");
  221. InOut.Print(soldBooks, "Duomenys.txt", "Pradinis Pardavimas");
  222.  
  223. BookStore soldBookRegister = new BookStore(soldBooks);
  224. bookStore.AddSalePrice(soldBooks);
  225.  
  226.  
  227. InOut.Print(soldBookRegister, "Duomenys.txt", " Papildyta Parduota");
  228. InOut.Print(bookStore, "Duomenys.txt", "Galutinis knygos");
  229.  
  230. string moneyLeft = string.Format("Turi dar surinkti {0} eur.", bookStore.Sum());
  231. File.AppendAllText("Duomenys.txt", moneyLeft);
  232. }
  233. }
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement