Advertisement
nikolayneykov

Untitled

Mar 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         List<Box> boxes = new List<Box>();
  10.         string input = string.Empty;
  11.  
  12.         while ((input=Console.ReadLine())!="end")
  13.         {
  14.             string[] tokens = input.Split(' ');
  15.             string serialNumber = tokens[0];
  16.             string itemName = tokens[1];
  17.             int itemQuantity = int.Parse(tokens[2]);
  18.             decimal itemPrice = decimal.Parse(tokens[3]);
  19.             Item item = new Item(itemName,itemQuantity,itemPrice);
  20.  
  21.             Box currentBox = boxes.Find(x => x.SerialNumber == serialNumber);
  22.  
  23.             if (currentBox== null)
  24.             {
  25.                 currentBox = new Box(serialNumber);
  26.                 currentBox.AddItem(item);
  27.             }
  28.  
  29.             boxes.Add(currentBox);
  30.         }
  31.  
  32.         boxes.OrderByDescending(b=>b.GetBoxPrice()).ToList().ForEach(b => Console.WriteLine(b));
  33.     }
  34. }
  35.  
  36. class Box
  37. {
  38.     public Box(string serialNumber)
  39.     {
  40.         this.SerialNumber = serialNumber;
  41.         this.Items = new List<Item>();
  42.     }
  43.  
  44.     public string SerialNumber { get; set; }
  45.     public List<Item> Items { get; set; }
  46.  
  47.     public void AddItem(Item item)
  48.     {
  49.         this.Items.Add(item);
  50.     }
  51.  
  52.     public decimal GetBoxPrice()
  53.     {
  54.         return this.Items.Sum(x => x.Price * x.Quantity);
  55.     }
  56.  
  57.     public override string ToString()
  58.     {
  59.         return this.SerialNumber +
  60.             Environment.NewLine +
  61.             string.Join(Environment.NewLine, this.Items) +
  62.             Environment.NewLine +
  63.             $"-- ${this.GetBoxPrice():F2}";
  64.     }
  65. }
  66.  
  67. class Item
  68. {
  69.     public Item(string name,int quantity,decimal price)
  70.     {
  71.         this.Name = name;
  72.         this.Quantity = quantity;
  73.         this.Price = price;
  74.     }
  75.  
  76.     public string Name { get; set; }
  77.     public int Quantity { get; set; }
  78.     public decimal Price { get; set; }
  79.  
  80.     public override string ToString()
  81.     {
  82.         return $"-- {this.Name} - ${this.Price:F2}: {this.Quantity}";
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement