JulianJulianov

07.ObjectAndClasses-Store Boxes

Mar 5th, 2020
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 KB | None | 0 0
  1. 07.ObjectAndClasses-Store Boxes
  2. Define a class Item, which contains these properties: Name and Price.
  3. Define a class Box, which contains these properties: Serial Number, Item, Item Quantity and Price for a Box.
  4. Until you receive "end", you will be receiving data in the following format: {Serial Number} {Item Name} {Item Quantity} {itemPrice}
  5. The Price of one box has to be calculated: itemQuantity * itemPrice.
  6. Print all the boxes, ordered descending by price for a box, in the following format:
  7. {boxSerialNumber}
  8. -- {boxItemName} – ${boxItemPrice}: {boxItemQuantity}
  9. -- ${boxPrice}
  10. The price should be formatted to the 2nd digit after the decimal separator.
  11. Examples
  12. Input                              Output
  13. 19861519 Dove 15 2.50              37741865
  14. 86757035 Butter 7 3.20             -- Samsung - $1000.00: 10
  15. 39393891 Orbit 16 1.60             -- $10000.00
  16. 37741865 Samsung 10 1000           19861519
  17. end                                -- Dove - $2.50: 15
  18.                                    -- $37.50
  19.                                    39393891
  20.                                    -- Orbit - $1.60: 16
  21.                                    -- $25.60
  22.                                    86757035
  23.                                    -- Butter - $3.20: 7
  24.                                    -- $22.40
  25.  
  26. 48760766 Alcatel 8 100             97617240
  27. 97617240 Intel 2 500               -- Intel - $500.00: 2
  28. 83840873 Milka 20 2.75             -- $1000.00
  29. 35056501 SneakersXL 15 1.50        48760766
  30. end                                -- Alcatel - $100.00: 8
  31.                                    -- $800.00
  32.                                    83840873
  33.                                    -- Milka - $2.75: 20
  34.                                    -- $55.00
  35.                                    35056501
  36.                                    -- SneakersXL - $1.50: 15
  37.                                    -- $22.50
  38. Hints
  39. This is how your class Box should look like:
  40.  
  41. Create an instance of Item in such a way, that when you try to set a value to some of the properties, it will not throw you an exception.
  42. There are two ways to do that:
  43. First you can create a new instance of Item in the Box constructor.
  44.  
  45. using System;
  46. using System.Linq;
  47. using System.Collections.Generic;
  48.  
  49. //Class
  50. namespace StoreBoxes
  51. {
  52.     class Box    //Първо си създаваме class-а със зададените по условие елементи!
  53.     {
  54.         public string SerialNumber { get; set; }
  55.         public string ItemName { get; set; }
  56.         public int ItemQuantity { get; set; }
  57.         public double PriceForBox { get; set; }
  58.         public double TotalPrice { get; set; }
  59.     }
  60. }
  61.  
  62. //Main
  63. namespace StoreBoxes
  64. {
  65.     class Program
  66.     {
  67.         static void Main(string[] args)
  68.         {
  69.             List<Box> itemBoxes = new List<Box>(); //Второ си създаваме празен списък,  който да напълним
  70.                                                    //след подадените данни от конзолния прозорец!
  71.             while (true)
  72.             {
  73.                 string command = Console.ReadLine();//Данните от конзолата.
  74.                 string[] parts = command.Split();   //Превръщаме ги в масив!
  75.  
  76.                 if (command == "end")
  77.                 {
  78.                     break;
  79.                 }
  80.  
  81.                 string serialNumber = parts[0];     //Запазваме в променливи всеки елемент от масива.
  82.                 string itemName = parts[1];
  83.                 int itemQuantity = int.Parse(parts[2]);
  84.                 double itemPrice = double.Parse(parts[3]);
  85.                 double totalPrice = itemPrice * itemQuantity;
  86.  
  87.                 var box = new Box();                //Създаваме обекта Box: копие-препратка на данните към class-а Box
  88.                                                     //за да се обединят тези данни в един елемент от списък.
  89.                 box.SerialNumber = serialNumber;
  90.                 box.ItemName = itemName;
  91.                 box.ItemQuantity = itemQuantity;
  92.                 box.PriceForBox = itemPrice;
  93.                 box.TotalPrice = itemPrice * itemQuantity;
  94.  
  95.                 itemBoxes.Add(box);                 //Тук се съхраняват данните от обекта като отделни елементи,
  96.                                                     //които съдържат характеристик(в случая: сериен номер, име, цена и т.н.)
  97.             }
  98.  
  99.             List<Box> sortedBox = itemBoxes.OrderBy(boxes => boxes.TotalPrice).ToList();//Сортираме ги от най-малката цена до най-голямата.
  100.             sortedBox.Reverse();                    //Обръщаме реда им.
  101.  
  102.             foreach (Box box in sortedBox)
  103.             {
  104.                 Console.WriteLine($"{box.SerialNumber}");
  105.                 Console.WriteLine($"-- {box.ItemName} - ${box.PriceForBox:F2}: {box.ItemQuantity}");
  106.                 Console.WriteLine($"-- ${box.TotalPrice:F2}");
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment