Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace K1
- {
- class Book
- {
- /// <summary>
- ///
- /// </summary>
- 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<Book> Books;
- public BookStore(List<Book> books)
- {
- this.Books = books;
- }
- 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++)
- {
- Book bookTemp = book;
- if (Books[i] >= bookTemp)
- {
- bookTemp = Books[i];
- index = i;
- }
- }
- return index;
- }
- public void AddSalePrice(List<Book> books)
- {
- for (int i = 0; i < books.Count; i++)
- {
- if(IndexMaxPrice(books[i]) != -1)
- {
- int index = IndexMaxPrice(books[i]);
- decimal prc = Books[index].SinglePrice;
- Console.WriteLine(prc);
- books[i].SinglePrice = Books[index].SinglePrice;
- books[i].Count -= 1;
- }
- }
- }
- }
- /// <summary>
- /// Class contaiting methods for results input/output
- /// </summary>
- class InOut
- {
- public static BookStore InputBooks(string fileName)
- {
- List<Book> books = new List<Book>();
- 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<Book> InputSoldBooks(string fileName)
- {
- string[] lines = File.ReadAllLines(fileName, Encoding.UTF8);
- List<Book> soldBooks = new List<Book>();
- 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.WriteAllText(fileName, line, Encoding.UTF8);
- }
- public static void Print(List<Book> 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.WriteAllText(fileName, line, Encoding.UTF8);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- BookStore bookStore = InOut.InputBooks("Knyga.txt");
- List<Book> soldBooks = InOut.InputSoldBooks("Parduota.txt");
- BookStore tempSoldBookRegister = new BookStore(soldBooks);
- InOut.Print(bookStore, "Pradinis.txt", "Pradinis knyogs");
- InOut.Print(tempSoldBookRegister, "PradinisPardavimas.txt", "Pradinis Pardavimas");
- bookStore.AddSalePrice(soldBooks);
- BookStore soldBookRegister = new BookStore(soldBooks);
- InOut.Print(soldBookRegister, "Papildyta.txt", " Papildyta Parduota");
- string moneyLeft = string.Format("Turi dar surinkti {0}, pinigu", bookStore.Sum() - soldBookRegister.Sum());
- File.AppendAllText("Parduota.txt", moneyLeft);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement