Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 07.ObjectAndClasses-Store Boxes
- Define a class Item, which contains these properties: Name and Price.
- Define a class Box, which contains these properties: Serial Number, Item, Item Quantity and Price for a Box.
- Until you receive "end", you will be receiving data in the following format: {Serial Number} {Item Name} {Item Quantity} {itemPrice}
- The Price of one box has to be calculated: itemQuantity * itemPrice.
- Print all the boxes, ordered descending by price for a box, in the following format:
- {boxSerialNumber}
- -- {boxItemName} – ${boxItemPrice}: {boxItemQuantity}
- -- ${boxPrice}
- The price should be formatted to the 2nd digit after the decimal separator.
- Examples
- Input Output
- 19861519 Dove 15 2.50 37741865
- 86757035 Butter 7 3.20 -- Samsung - $1000.00: 10
- 39393891 Orbit 16 1.60 -- $10000.00
- 37741865 Samsung 10 1000 19861519
- end -- Dove - $2.50: 15
- -- $37.50
- 39393891
- -- Orbit - $1.60: 16
- -- $25.60
- 86757035
- -- Butter - $3.20: 7
- -- $22.40
- 48760766 Alcatel 8 100 97617240
- 97617240 Intel 2 500 -- Intel - $500.00: 2
- 83840873 Milka 20 2.75 -- $1000.00
- 35056501 SneakersXL 15 1.50 48760766
- end -- Alcatel - $100.00: 8
- -- $800.00
- 83840873
- -- Milka - $2.75: 20
- -- $55.00
- 35056501
- -- SneakersXL - $1.50: 15
- -- $22.50
- Hints
- This is how your class Box should look like:
- 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.
- There are two ways to do that:
- First you can create a new instance of Item in the Box constructor.
- using System;
- using System.Linq;
- using System.Collections.Generic;
- //Class
- namespace StoreBoxes
- {
- class Box //Първо си създаваме class-а със зададените по условие елементи!
- {
- public string SerialNumber { get; set; }
- public string ItemName { get; set; }
- public int ItemQuantity { get; set; }
- public double PriceForBox { get; set; }
- public double TotalPrice { get; set; }
- }
- }
- //Main
- namespace StoreBoxes
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Box> itemBoxes = new List<Box>(); //Второ си създаваме празен списък, който да напълним
- //след подадените данни от конзолния прозорец!
- while (true)
- {
- string command = Console.ReadLine();//Данните от конзолата.
- string[] parts = command.Split(); //Превръщаме ги в масив!
- if (command == "end")
- {
- break;
- }
- string serialNumber = parts[0]; //Запазваме в променливи всеки елемент от масива.
- string itemName = parts[1];
- int itemQuantity = int.Parse(parts[2]);
- double itemPrice = double.Parse(parts[3]);
- double totalPrice = itemPrice * itemQuantity;
- var box = new Box(); //Създаваме обекта Box: копие-препратка на данните към class-а Box
- //за да се обединят тези данни в един елемент от списък.
- box.SerialNumber = serialNumber;
- box.ItemName = itemName;
- box.ItemQuantity = itemQuantity;
- box.PriceForBox = itemPrice;
- box.TotalPrice = itemPrice * itemQuantity;
- itemBoxes.Add(box); //Тук се съхраняват данните от обекта като отделни елементи,
- //които съдържат характеристик(в случая: сериен номер, име, цена и т.н.)
- }
- List<Box> sortedBox = itemBoxes.OrderBy(boxes => boxes.TotalPrice).ToList();//Сортираме ги от най-малката цена до най-голямата.
- sortedBox.Reverse(); //Обръщаме реда им.
- foreach (Box box in sortedBox)
- {
- Console.WriteLine($"{box.SerialNumber}");
- Console.WriteLine($"-- {box.ItemName} - ${box.PriceForBox:F2}: {box.ItemQuantity}");
- Console.WriteLine($"-- ${box.TotalPrice:F2}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment