Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ANOTHER_Kontras
- {
- class Program
- {
- static void Main(string[] args)
- {
- BookStore allBooks = InOut.InputBooks("Knygos.txt");
- List<Book> soldBooks = InOut.InputSoldBooks("Pardavimai.txt");
- File.Delete("Data.txt");
- InOut.Print(allBooks, "Data.txt", "Knygynas");
- InOut.Print(soldBooks, "Data.txt", "Parduotos knygos");
- allBooks.AddSalePrice(soldBooks);
- InOut.Print(soldBooks, "Data.txt", "Papildyta");
- BookStore allSoldBooks = new BookStore(soldBooks);
- string moneyLeft = String.Format("Truksta Manio {0}", allBooks.Sum() - allSoldBooks.Sum());
- InOut.Print(allBooks, "Data.txt", "Knygynas");
- File.AppendAllText("Data.txt", moneyLeft);
- }
- }
- class Book
- {
- public string Distributor { get; set; }
- public string Title { get; set; }
- public int Amount { get; set; }
- public decimal Price { get; set; }
- public Book(string distributor, string title, int amount, decimal price)
- {
- this.Distributor = distributor;
- this.Title = title;
- this.Amount = amount;
- this.Price = price;
- }
- public override string ToString()
- {
- return string.Format("| {0, -18} | {1, -18} | {2, 8} | {3, 8} |", this.Distributor, this.Title, this.Amount, this.Price);
- }
- public static bool operator <=(Book lhs, Book rhs)
- {
- return lhs.Price <= rhs.Price;
- }
- public static bool operator >=(Book lhs, Book rhs)
- {
- return lhs.Price >= rhs.Price;
- }
- }
- class BookStore
- {
- private List<Book> allBooks = new List<Book>();
- public BookStore(List<Book> books)
- {
- foreach (Book book in books)
- {
- allBooks.Add(book);
- }
- }
- public int GetCount()
- {
- return this.allBooks.Count;
- }
- public Book GetBook(int index)
- {
- return allBooks[index];
- }
- public void AddBook(Book book)
- {
- this.allBooks.Add(book);
- }
- public decimal Sum()
- {
- decimal max = 0;
- foreach (Book book in allBooks)
- {
- max += book.Price * book.Amount;
- }
- return max;
- }
- public int IndexMaxPrice(Book book)
- {
- int index = 0;
- decimal price = 0m;
- for (int i = 0; i < allBooks.Count; i++)
- {
- if(allBooks[i].Title == book.Title && price < allBooks[i].Price)
- {
- index = i;
- }
- }
- return index;
- }
- public void AddSalePrice(List<Book> books)
- {
- int index;
- foreach(Book book in books)
- {
- for (int i = 0; i < allBooks.Count; i++)
- {
- if (book.Title == allBooks[i].Title)
- {
- index = IndexMaxPrice(allBooks[i]);
- book.Price = allBooks[index].Price;
- }
- }
- }
- }
- }
- static class InOut
- {
- public static BookStore InputBooks(string fileName)
- {
- List<Book> allBooks = new List<Book>();
- string[] lines = File.ReadAllLines(fileName);
- foreach(string line in lines)
- {
- string[] value = line.Split(';');
- string distributor = value[0];
- string title = value[1];
- int amount = int.Parse(value[2]);
- decimal price = decimal.Parse(value[3]);
- Book book = new Book(distributor, title, amount, price);
- allBooks.Add(book);
- }
- BookStore bookStore = new BookStore(allBooks);
- return bookStore;
- }
- public static List<Book> InputSoldBooks(string fileName)
- {
- List<Book> soldBooks = new List<Book>();
- string[] lines = File.ReadAllLines(fileName);
- 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[] lines = new string[books.GetCount() + 5];
- lines[0] = header;
- lines[1] = new string('-', 64);
- lines[2] = String.Format("| {0, -18} | {1, -18} | {2, 8} | {3, 8} |", "Platintojas", "Pavadinimas", "Kiekis", "Kaina");
- lines[3] = new string('-', 64);
- for (int i = 0; i < books.GetCount(); i++)
- {
- lines[i + 4] = books.GetBook(i).ToString();
- }
- lines[lines.Length - 1] = new string('-', 64);
- File.AppendAllLines(fileName, lines, Encoding.UTF8);
- }
- public static void Print(List<Book> books, string fileName, string header)
- {
- string[] lines = new string[books.Count + 5];
- lines[0] = header;
- lines[1] = new string('-', 64);
- lines[2] = String.Format("| {0, -18} | {1, -18} | {2, 8} | {3, 8} |", "Platintojas", "Pavadinimas", "Kiekis", "Kaina");
- lines[3] = new string('-', 64);
- for (int i = 0; i < books.Count; i++)
- {
- lines[i + 4] = books[i].ToString();
- }
- lines[lines.Length - 1] = new string('-', 64);
- File.AppendAllLines(fileName, lines, Encoding.UTF8);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement