Advertisement
Nikolay_Kashev

03. Orders - Dictionaries, Lambda and LINQ - Exercise

Mar 14th, 2024 (edited)
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program {
  5.     public static void Main(string[] args) {
  6.         Dictionary<string, (double price, int quantity)> products = new Dictionary<string, (double price, int quantity)>();
  7.  
  8.         string input = Console.ReadLine();
  9.         while (input != "buy") {
  10.             string[] tokens = input.Split(' ');
  11.             string name = tokens[0];
  12.             double price = double.Parse(tokens[1]);
  13.             int quantity = int.Parse(tokens[2]);
  14.  
  15.             if (products.ContainsKey(name)) {
  16.                 products[name] = (price, products[name].quantity + quantity);
  17.             }
  18.             else {
  19.                 products[name] = (price, quantity);
  20.             }
  21.         }
  22.  
  23.         foreach (var product in products) {
  24.             string name = product.Key;
  25.             double totalPrice = product.Value.price * product.Value.quantity;
  26.             //Console.WriteLine($"{name} -> {totalPrice:F2}");
  27.             Console.WriteLine("{0} -> {1:F2}", name, totalPrice);
  28.         }      
  29.         // Console.WriteLine();    
  30.         // foreach (var product in products) {
  31.             // string name = product.Key;
  32.             // double price = product.Value.price;
  33.             // int quantity = product.Value.quantity;
  34.             // // Console.WriteLine($"{name} {price:F2} {quantity}");
  35.             // Console.WriteLine("{0} {1:F2} {2}", name, price, quantity);
  36.         }
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement