Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Mažeika EImantas IFF- 9/3 K1-pavyzdinis P175B118
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace BookStore
- {
- class Program
- {
- static void Main(string[] args)
- {
- BookStore books = InOut.InputBooks("Knyga.txt");
- InOut.Print(books, "rezultatai.txt", "Knygynas");
- List<Book> soldBooksList = InOut.InputSoldBooks("Parduota.txt");
- BookStore Soldbooks = new BookStore(soldBooksList);
- InOut.Print(Soldbooks, "rezultatai.txt", "Pardavimas");
- books.AddSalePrice(soldBooksList);
- BookStore FilledSoldbooks = new BookStore(soldBooksList);
- InOut.Print(FilledSoldbooks, "rezultatai.txt", "Parduota");
- InOut.AppendStringToFile(String.Format("Reikia dar surinkti {0} pinigu", books.Sum() - FilledSoldbooks.Sum()), "rezultatai.txt");
- //Stall the program
- Console.ReadKey();
- }
- }
- class Book
- {
- public string Name { get; set; }
- public string Seller { get; set; }
- public int AmountAvailable { get; set; }
- public decimal Price { get; set; }
- public Book(string seller, string name, int amount, decimal price)
- {
- this.Name = name;
- this.Seller = seller;
- this.AmountAvailable = amount;
- this.Price = price;
- }
- public Book(string Name)
- {
- this.Name = Name;
- this.Seller = "";
- this.Price = 0;
- this.AmountAvailable = 1;
- }
- public override string ToString()
- {
- string str = string.Format("{0, -20} {1, -20} {2, 10} {3, 10}", Seller, Name, AmountAvailable, Price);
- return str;
- }
- public static bool operator >=(Book obj1, Book obj2)
- {
- if (obj1.Name == obj2.Name && obj1.Price >= obj2.Price)
- return true;
- else
- return false;
- }
- public static bool operator <=(Book obj1, Book obj2)
- {
- if (obj1.Name == obj2.Name && obj1.Price <= obj2.Price)
- return true;
- else
- return false;
- }
- }
- class BookStore
- {
- private List<Book> allBooks = new List<Book>();
- public BookStore(List<Book> books)
- {
- foreach (Book book in books)
- {
- allBooks.Add(book);
- }
- }
- public void AddEntry(Book book)
- {
- allBooks.Add(book);
- }
- public Book GetEntry(int index)
- {
- if (index < GetCount())
- return allBooks[index];
- else
- return null;
- }
- public int GetCount()
- {
- return allBooks.Count;
- }
- public decimal Sum()
- {
- decimal sum = 0;
- foreach (Book book in allBooks)
- {
- sum += book.AmountAvailable * book.Price;
- }
- return sum;
- }
- public decimal MaximumPrice(Book book)
- {
- decimal maxPrice = -9999;
- foreach (Book bookFromList in allBooks)
- {
- if (bookFromList >= book)
- {
- maxPrice = bookFromList.Price;
- }
- }
- return maxPrice;
- }
- public void AddSalePrice(List<Book> soldBooks)
- {
- foreach (Book book in soldBooks)
- {
- decimal price = MaximumPrice(book);
- book.Price = price;
- }
- }
- }
- class InOut
- {
- public static BookStore InputBooks(string fileName)
- {
- string[] lines = File.ReadAllLines(fileName);
- List<Book> allBooks = new List<Book>();
- foreach (string line in lines)
- {
- string[] values = line.Split(';');
- string seller = values[0];
- string bookName = values[1];
- int amount = int.Parse(values[2]);
- decimal price = decimal.Parse(values[3]);
- allBooks.Add(new Book(seller, bookName, amount, price));
- }
- return new BookStore(allBooks);
- }
- public static List<Book> InputSoldBooks(string fileName)
- {
- List<Book> soldBooks = new List<Book>();
- string[] lines = File.ReadAllLines(fileName);
- foreach (string line in lines)
- {
- soldBooks.Add(new Book(line));
- }
- return soldBooks;
- }
- public static void Print(BookStore books, string fileName, string header)
- {
- string[] lines = new string[books.GetCount() * 2 + 4];
- lines[0] = header;
- lines[1] = new string('-', 100);
- lines[2] = string.Format("{0, -20} {1, -20} {2, 10} {3, 10}", "Seller", "Name", "Available", "Price");
- for (int i=0;i<books.GetCount();i++)
- {
- lines[i * 2 + 3] = new string('-', 100);
- lines[i * 2 + 4] = (books.GetEntry(i)).ToString();
- }
- File.AppendAllLines(fileName, lines);
- }
- public static void AppendStringToFile(string s, string fileName)
- {
- string[] str = new string[1];
- str[0] = s;
- File.AppendAllLines(fileName, str);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement