Advertisement
Guest User

EM code

a guest
Oct 16th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.67 KB | None | 0 0
  1. // Mažeika EImantas IFF- 9/3 K1-pavyzdinis P175B118
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.IO;
  8.  
  9.  
  10. namespace BookStore
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             BookStore books = InOut.InputBooks("Knyga.txt");
  17.  
  18.             InOut.Print(books, "rezultatai.txt", "Knygynas");
  19.  
  20.  
  21.             List<Book> soldBooksList = InOut.InputSoldBooks("Parduota.txt");
  22.  
  23.             BookStore Soldbooks = new BookStore(soldBooksList);
  24.  
  25.             InOut.Print(Soldbooks, "rezultatai.txt", "Pardavimas");
  26.  
  27.             books.AddSalePrice(soldBooksList);
  28.             BookStore FilledSoldbooks = new BookStore(soldBooksList);
  29.  
  30.             InOut.Print(FilledSoldbooks, "rezultatai.txt", "Parduota");
  31.  
  32.             InOut.AppendStringToFile(String.Format("Reikia dar surinkti {0} pinigu", books.Sum() - FilledSoldbooks.Sum()), "rezultatai.txt");
  33.             //Stall the program
  34.             Console.ReadKey();
  35.         }
  36.     }
  37.  
  38.     class Book
  39.     {
  40.         public string Name { get; set; }
  41.         public string Seller { get; set; }
  42.         public int AmountAvailable { get; set; }
  43.         public decimal Price { get; set; }
  44.  
  45.         public Book(string seller, string name, int amount, decimal price)
  46.         {
  47.             this.Name = name;
  48.             this.Seller = seller;
  49.             this.AmountAvailable = amount;
  50.             this.Price = price;
  51.         }
  52.  
  53.         public Book(string Name)
  54.         {
  55.             this.Name = Name;
  56.             this.Seller = "";
  57.             this.Price = 0;
  58.             this.AmountAvailable = 1;
  59.         }
  60.  
  61.         public override string ToString()
  62.         {
  63.             string str = string.Format("{0, -20} {1, -20} {2, 10} {3, 10}", Seller, Name, AmountAvailable, Price);
  64.             return str;
  65.         }
  66.  
  67.         public static bool operator >=(Book obj1, Book obj2)
  68.         {
  69.             if (obj1.Name == obj2.Name && obj1.Price >= obj2.Price)
  70.                 return true;
  71.             else
  72.                 return false;
  73.         }
  74.  
  75.         public static bool operator <=(Book obj1, Book obj2)
  76.         {
  77.             if (obj1.Name == obj2.Name && obj1.Price <= obj2.Price)
  78.                 return true;
  79.             else
  80.                 return false;
  81.         }
  82.     }
  83.  
  84.     class BookStore
  85.     {
  86.         private List<Book> allBooks = new List<Book>();
  87.         public BookStore(List<Book> books)
  88.         {
  89.             foreach (Book book in books)
  90.             {
  91.                 allBooks.Add(book);
  92.             }
  93.         }
  94.         public void AddEntry(Book book)
  95.         {
  96.             allBooks.Add(book);
  97.         }
  98.         public Book GetEntry(int index)
  99.         {
  100.             if (index < GetCount())
  101.                 return allBooks[index];
  102.             else
  103.                 return null;
  104.         }
  105.         public int GetCount()
  106.         {
  107.             return allBooks.Count;
  108.         }
  109.         public decimal Sum()
  110.         {
  111.             decimal sum = 0;
  112.             foreach (Book book in allBooks)
  113.             {
  114.                 sum += book.AmountAvailable * book.Price;
  115.             }
  116.             return sum;
  117.         }
  118.  
  119.         public decimal MaximumPrice(Book book)
  120.         {
  121.             decimal maxPrice = -9999;
  122.             foreach (Book bookFromList in allBooks)
  123.             {
  124.                 if (bookFromList >= book)
  125.                 {
  126.                     maxPrice = bookFromList.Price;
  127.                 }
  128.             }
  129.             return maxPrice;
  130.         }
  131.        
  132.        
  133.         public void AddSalePrice(List<Book> soldBooks)
  134.         {
  135.             foreach (Book book in soldBooks)
  136.             {
  137.                 decimal price = MaximumPrice(book);
  138.                 book.Price = price;
  139.             }
  140.         }
  141.        
  142.  
  143.  
  144.  
  145.     }
  146.  
  147.     class InOut
  148.     {
  149.         public static BookStore InputBooks(string fileName)
  150.         {
  151.             string[] lines = File.ReadAllLines(fileName);
  152.             List<Book> allBooks = new List<Book>();
  153.  
  154.             foreach (string line in lines)
  155.             {
  156.                 string[] values = line.Split(';');
  157.                 string seller = values[0];
  158.                 string bookName = values[1];
  159.                 int amount = int.Parse(values[2]);
  160.                 decimal price = decimal.Parse(values[3]);
  161.  
  162.                 allBooks.Add(new Book(seller, bookName, amount, price));
  163.             }
  164.             return new BookStore(allBooks);
  165.         }
  166.  
  167.         public static List<Book> InputSoldBooks(string fileName)
  168.         {
  169.             List<Book> soldBooks = new List<Book>();
  170.  
  171.             string[] lines = File.ReadAllLines(fileName);
  172.  
  173.             foreach (string line in lines)
  174.             {
  175.                 soldBooks.Add(new Book(line));
  176.             }
  177.  
  178.             return soldBooks;
  179.         }
  180.  
  181.         public static void Print(BookStore books, string fileName, string header)
  182.         {
  183.             string[] lines = new string[books.GetCount() * 2 + 4];
  184.             lines[0] = header;
  185.             lines[1] = new string('-', 100);
  186.             lines[2] = string.Format("{0, -20} {1, -20} {2, 10} {3, 10}", "Seller", "Name", "Available", "Price");
  187.             for (int i=0;i<books.GetCount();i++)
  188.             {
  189.                 lines[i * 2 + 3] = new string('-', 100);
  190.                 lines[i * 2 + 4] = (books.GetEntry(i)).ToString();
  191.             }
  192.  
  193.             File.AppendAllLines(fileName, lines);
  194.  
  195.         }
  196.  
  197.         public static void AppendStringToFile(string s, string fileName)
  198.         {
  199.             string[] str = new string[1];
  200.             str[0] = s;
  201.             File.AppendAllLines(fileName, str);
  202.  
  203.         }
  204.  
  205.  
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement