Advertisement
vencinachev

ShoppingCart

Mar 27th, 2022
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Lists
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             SortedDictionary<string, double> shopCart = new SortedDictionary<string, double>();
  12.             double total = 0;
  13.             while (true)
  14.             {
  15.                 string command = Console.ReadLine();
  16.                 if (command == "STOP SHOPPING")
  17.                 {
  18.                     break;
  19.                 }
  20.                 string[] input = command.Split('-').ToArray();
  21.                 string product = input[0];
  22.                 double price = double.Parse(input[1]);
  23.                 total += price;
  24.                 if (shopCart.ContainsKey(product))
  25.                 {
  26.                     shopCart[product] += price;
  27.                 }
  28.                 else
  29.                 {
  30.                     shopCart.Add(product, price);
  31.                 }
  32.             }
  33.  
  34.             foreach (var item in shopCart)
  35.             {
  36.                 Console.WriteLine($"{item.Key}->{item.Value}");
  37.             }
  38.             Console.WriteLine($"{total:F2}");
  39.         }
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement