using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.IO; namespace K1 { class Book { /// /// /// public string Creator { get; set; } public string Name { get; set; } public int Count { get; set; } public decimal SinglePrice { get; set; } public Book(string creator, string name, int count, decimal price) { this.Name = name; this.Count = count; this.SinglePrice = price; this.Creator = creator; } public Book(string name, int count, decimal price) { this.Name = name; this.Count = count; this.SinglePrice = price; } public override string ToString() { return string.Format("{0,-18} |{1,-20}|{2,8}|{3,10}|", Creator, Name, Count, SinglePrice); } public static bool operator <=(Book lhs, Book rhs) { return (lhs.SinglePrice <= rhs.SinglePrice) && (lhs.Name == rhs.Name) && (lhs.Count > 0 && rhs.Count > 0); } public static bool operator >=(Book lhs, Book rhs) { return (lhs.SinglePrice >= rhs.SinglePrice) && (lhs.Name == rhs.Name) && (lhs.Count > 0 && rhs.Count > 0); } } class BookStore { private List Books = new List(); public BookStore(List books) { foreach(Book book in books) { this.Books.Add(book); } } public BookStore() { Books = new List(); } public int GetCount() { return this.Books.Count; } public Book GetBook(int index) { return this.Books[index]; } public void AddBook(Book book) { this.Books.Add(book); } public decimal Sum() { decimal tempSum = 0; foreach (Book book in Books) { tempSum += book.SinglePrice * book.Count; } return tempSum; } public int IndexMaxPrice(Book book) { int index = -1; for (int i = 0; i < Books.Count; i++) { // (lhs.SinglePrice >= rhs.SinglePrice) && (lhs.Name == rhs.Name) && (lhs.Count > 0 && rhs.Count > 0); if (Books[i] >= book) { index = i; } } return index; } public void AddSalePrice(List books) { for(int i = 0; i < books.Count; i++) { int index = IndexMaxPrice(books[i]); if (index != -1) { books[i].SinglePrice = Books[index].SinglePrice; Books[index].Count--; } } } } /// /// Class contaiting methods for results input/output /// class InOut { public static BookStore InputBooks(string fileName) { List books = new List(); string[] lines = File.ReadAllLines(fileName, Encoding.UTF8); string creator, name; int count; decimal price; for (int i = 0; i < lines.Length; i++) { string[] parts = lines[i].Split(';'); creator = parts[0]; name = parts[1]; count = int.Parse(parts[2]); price = decimal.Parse(parts[3]); Book book = new Book(creator, name, count, price); books.Add(book); } BookStore store = new BookStore(books); return store; } public static List InputSoldBooks(string fileName) { string[] lines = File.ReadAllLines(fileName, Encoding.UTF8); List soldBooks = new List(); for (int i = 0; i < lines.Length; i++) { Book book = new Book(lines[i],1,0); soldBooks.Add(book); } return soldBooks; } public static void Print(BookStore books, string fileName, string header) { string line = ""; line += header + "\n"; line += new string('-', 50) + "\n"; line += string.Format("{0,-18} {1,-20} {2,8} {3,10}", "Platintojas", "Pavadinimas", "Kiekis", "Kaina") + "\n"; line += new string('-', 50) + "\n"; for (int i = 0; i < books.GetCount(); i++) { line += books.GetBook(i).ToString() + "\n"; } line += new string('-', 50) + "\n"; File.AppendAllText(fileName, line, Encoding.UTF8); } public static void Print(List books, string fileName, string header) { string line = ""; line += header + "\n"; line += new string('-', 50) + "\n"; line += string.Format("{0,-18} {1,-20} {2,8} {3,10}", "Platintojas", "Pavadinimas", "Kiekis", "Kaina") + "\n"; line += new string('-', 50) + "\n"; for (int i = 0; i < books.Count; i++) { line += books[i].ToString() + "\n"; } line += new string('-', 50) + "\n"; File.AppendAllText(fileName, line, Encoding.UTF8); } } class Program { static void Main(string[] args) { File.Delete("Duomenys.txt"); BookStore bookStore = InOut.InputBooks("Knyga.txt"); List soldBooks = InOut.InputSoldBooks("Parduota.txt"); InOut.Print(bookStore, "Duomenys.txt", "Pradinis knyogs"); InOut.Print(soldBooks, "Duomenys.txt", "Pradinis Pardavimas"); BookStore soldBookRegister = new BookStore(soldBooks); bookStore.AddSalePrice(soldBooks); InOut.Print(soldBookRegister, "Duomenys.txt", " Papildyta Parduota"); InOut.Print(bookStore, "Duomenys.txt", "Galutinis knygos"); string moneyLeft = string.Format("Turi dar surinkti {0} eur.", bookStore.Sum()); File.AppendAllText("Duomenys.txt", moneyLeft); } } }