Advertisement
MariusPure

K1_as

Oct 11th, 2020
2,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.45 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.     class BookStore
  51.     {
  52.         private List<Book> Books;
  53.  
  54.         public BookStore(List<Book> books)
  55.         {
  56.             this.Books = books;
  57.         }
  58.  
  59.  
  60.         public int GetCount()
  61.         {
  62.             return this.Books.Count;
  63.         }
  64.  
  65.         public Book GetBook(int index)
  66.         {
  67.             return this.Books[index];
  68.         }
  69.  
  70.         public void AddBook(Book book)
  71.         {
  72.             this.Books.Add(book);
  73.         }
  74.  
  75.         public decimal Sum()
  76.         {
  77.             decimal tempSum = 0;
  78.  
  79.             foreach (Book book in Books)
  80.             {
  81.                 tempSum += book.SinglePrice * book.Count;
  82.             }
  83.  
  84.             return tempSum;
  85.         }
  86.  
  87.         public int IndexMaxPrice(Book book)
  88.         {
  89.             int index = -1;
  90.             for (int i = 0; i < Books.Count; i++)
  91.             {
  92.                 Book bookTemp = book;
  93.                 if (Books[i] >= bookTemp)
  94.                 {
  95.                     bookTemp = Books[i];
  96.                     index = i;
  97.                 }
  98.             }
  99.  
  100.             return index;
  101.         }
  102.  
  103.         public void AddSalePrice(List<Book> books)
  104.         {
  105.             for (int i = 0; i < books.Count; i++)
  106.             {
  107.                 if(IndexMaxPrice(books[i]) != -1)
  108.                 {
  109.                         int index = IndexMaxPrice(books[i]);
  110.                         decimal prc = Books[index].SinglePrice;
  111.                         Console.WriteLine(prc);
  112.                         books[i].SinglePrice = Books[index].SinglePrice;
  113.                         books[i].Count -= 1;
  114.                 }
  115.             }
  116.         }
  117.     }
  118.     /// <summary>
  119.     /// Class contaiting methods for results input/output
  120.     /// </summary>
  121.     class InOut
  122.     {
  123.         public static BookStore InputBooks(string fileName)
  124.         {
  125.  
  126.             List<Book> books = new List<Book>();
  127.  
  128.             string[] lines = File.ReadAllLines(fileName, Encoding.UTF8);
  129.             string creator, name;
  130.             int count;
  131.             decimal price;
  132.  
  133.             for (int i = 0; i < lines.Length; i++)
  134.             {
  135.                 string[] parts = lines[i].Split(';');
  136.  
  137.                 creator = parts[0];
  138.                 name = parts[1];
  139.                 count = int.Parse(parts[2]);
  140.                 price = decimal.Parse(parts[3]);
  141.  
  142.                 Book book = new Book(creator, name, count, price);
  143.                 books.Add(book);
  144.             }
  145.  
  146.             BookStore store = new BookStore(books);
  147.  
  148.             return store;
  149.  
  150.         }
  151.         public static List<Book> InputSoldBooks(string fileName)
  152.         {
  153.             string[] lines = File.ReadAllLines(fileName, Encoding.UTF8);
  154.  
  155.             List<Book> soldBooks = new List<Book>();
  156.  
  157.             for (int i = 0; i < lines.Length; i++)
  158.             {
  159.                 Book book = new Book(lines[i],1,0);
  160.                 soldBooks.Add(book);
  161.             }
  162.  
  163.             return soldBooks;
  164.         }
  165.  
  166.         public static void Print(BookStore books, string fileName, string header)
  167.         {
  168.             string line = "";
  169.  
  170.             line += header + "\n";
  171.             line += new string('-', 50) + "\n";
  172.             line += string.Format("{0,-18} {1,-20} {2,8} {3,10}", "Platintojas", "Pavadinimas", "Kiekis", "Kaina") + "\n";
  173.  
  174.             line += new string('-', 50) + "\n";
  175.  
  176.             for (int i = 0; i < books.GetCount(); i++)
  177.             {
  178.                 line += books.GetBook(i).ToString() + "\n";
  179.             }
  180.             line += new string('-', 50) + "\n";
  181.  
  182.             File.WriteAllText(fileName, line, Encoding.UTF8);
  183.         }
  184.  
  185.         public static void Print(List<Book> books, string fileName, string header)
  186.         {
  187.             string line = "";
  188.  
  189.             line += header + "\n";
  190.             line += new string('-', 50) + "\n";
  191.  
  192.             line += string.Format("{0,-18} {1,-20} {2,8} {3,10}", "Platintojas", "Pavadinimas", "Kiekis", "Kaina") + "\n";
  193.  
  194.             line += new string('-', 50) + "\n";
  195.  
  196.             for (int i = 0; i < books.Count; i++)
  197.             {
  198.                 line += books[i].ToString() + "\n";
  199.             }
  200.  
  201.             line += new string('-', 50) + "\n";
  202.  
  203.             File.WriteAllText(fileName, line, Encoding.UTF8);
  204.         }
  205.     }
  206.     class Program
  207.     {
  208.         static void Main(string[] args)
  209.         {
  210.             BookStore bookStore = InOut.InputBooks("Knyga.txt");
  211.             List<Book> soldBooks = InOut.InputSoldBooks("Parduota.txt");
  212.  
  213.             BookStore tempSoldBookRegister = new BookStore(soldBooks);
  214.             InOut.Print(bookStore, "Pradinis.txt", "Pradinis knyogs");
  215.             InOut.Print(tempSoldBookRegister, "PradinisPardavimas.txt", "Pradinis Pardavimas");
  216.  
  217.             bookStore.AddSalePrice(soldBooks);
  218.             BookStore soldBookRegister = new BookStore(soldBooks);
  219.             InOut.Print(soldBookRegister, "Papildyta.txt", " Papildyta Parduota");
  220.             string moneyLeft = string.Format("Turi dar surinkti {0}, pinigu", bookStore.Sum() - soldBookRegister.Sum());
  221.             File.AppendAllText("Parduota.txt", moneyLeft);
  222.         }
  223.     }
  224. }
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement