Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 09.Orders
- Write a program that keeps information about products and their prices. Each product has a name, a price and a quantity. If the product doesn’t exist yet, add it with its starting quantity.
- If you receive a product, which already exists, increase its quantity by the input quantity and if its price is different, replace the price as well.
- You will receive products’ names, prices and quantities on new lines. Until you receive the command "buy", keep adding items. When you do receive the command "buy", print the items with their names and total price of all the products with that name.
- Input
- • Until you receive "buy", the products will be coming in the format: "{name} {price} {quantity}".
- • The product data is always delimited by a single space.
- Output
- • Print information about each product in the following format:
- "{productName} -> {totalPrice}"
- • Format the average grade to the 2nd digit after the decimal separator.
- Examples
- Input Output
- Beer 2.20 100 Beer -> 220.00
- IceTea 1.50 50 IceTea -> 75.00
- NukaCola 3.30 80 NukaCola -> 264.00
- Water 1.00 500 Water -> 500.00
- buy
- Beer 1.20 200 Beer -> 660.00
- IceTea 0.50 120 Water -> 250.00
- Water 1.25 200 IceTea -> 110.00
- IceTea 5.20 100
- Beer 2.40 350
- buy
- Beer 1.35 350 CesarSalad -> 255.00
- IceCream 1.50 25 SuperEnergy -> 320.00
- CesarSalad 10.20 25 Beer -> 472.50
- SuperEnergy 0.80 400 IceCream -> 37.50
- buy
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _04Orders
- {
- class Program
- {
- static void Main(string[] args)
- {
- var command = Console.ReadLine().Split();
- var price = new Dictionary<string, double>();
- var quantity = new Dictionary<string, double>();
- while (command[0] != "buy")
- {
- if (price.ContainsKey(command[0]) == false)
- {
- price.Add(command[0], double.Parse(command[1]));
- quantity.Add(command[0], double.Parse(command[2]));
- }
- else
- {
- if (price[command[0]] > double.Parse(command[1]))//Чрез "command[0]", който е всъщност "Key" се достъпва до
- { //стойността на "Value"!
- price[command[0]] = double.Parse(command[1]);//Така "Value" приема нова стойност!
- }
- quantity[command[0]] += double.Parse(command[2]);//Тук "Value" се увеличава!
- }
- command = Console.ReadLine().Split();
- }
- foreach (var item in price)
- {
- double sum = item.Value * quantity[item.Key];//Чрез "item.Key", който е общ за двата речника "price" и "quantity" се
- //достъпва стойността "Value"!
- Console.WriteLine($"{item.Key} -> {sum:F2}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment