Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Program
- {
- static void Main(string[] args)
- {
- List<Box> boxes = new List<Box>();
- string input = string.Empty;
- while ((input=Console.ReadLine())!="end")
- {
- string[] tokens = input.Split(' ');
- string serialNumber = tokens[0];
- string itemName = tokens[1];
- int itemQuantity = int.Parse(tokens[2]);
- decimal itemPrice = decimal.Parse(tokens[3]);
- Item item = new Item(itemName,itemQuantity,itemPrice);
- Box currentBox = boxes.Find(x => x.SerialNumber == serialNumber);
- if (currentBox== null)
- {
- currentBox = new Box(serialNumber);
- currentBox.AddItem(item);
- }
- boxes.Add(currentBox);
- }
- boxes.OrderByDescending(b=>b.GetBoxPrice()).ToList().ForEach(b => Console.WriteLine(b));
- }
- }
- class Box
- {
- public Box(string serialNumber)
- {
- this.SerialNumber = serialNumber;
- this.Items = new List<Item>();
- }
- public string SerialNumber { get; set; }
- public List<Item> Items { get; set; }
- public void AddItem(Item item)
- {
- this.Items.Add(item);
- }
- public decimal GetBoxPrice()
- {
- return this.Items.Sum(x => x.Price * x.Quantity);
- }
- public override string ToString()
- {
- return this.SerialNumber +
- Environment.NewLine +
- string.Join(Environment.NewLine, this.Items) +
- Environment.NewLine +
- $"-- ${this.GetBoxPrice():F2}";
- }
- }
- class Item
- {
- public Item(string name,int quantity,decimal price)
- {
- this.Name = name;
- this.Quantity = quantity;
- this.Price = price;
- }
- public string Name { get; set; }
- public int Quantity { get; set; }
- public decimal Price { get; set; }
- public override string ToString()
- {
- return $"-- {this.Name} - ${this.Price:F2}: {this.Quantity}";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement