How to sum up values working with c# and Newtonsoft JSON?

Viewed 267

I am making a shopping cart. Adding meals to the cart is done and I also can show the products with the price in cart. The only thing what I haven't been able to do is to add all the prices up and write that value. The shoppingcart can have random number of products.

This is the code I use for the shopping cart:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace menu
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var myJsonString = new JsonShop("ShoppingCart.json");
            var myJsonObject = myJsonString.Load22();
            Console.Clear();
            Console.WriteLine("========================");
            Console.WriteLine("      Shopping cart");
            Console.WriteLine("========================\n");
            foreach (Shopping shop in myJsonObject.shoppinglist)
            {
                Console.WriteLine($"Meal: {shop.meal} Price: {shop.price}");
            }
            Console.WriteLine($"Total cost = ");
        }
        
        public class Shopping
        {
            public string meal;
            public double price;
                
            public Shopping(string Meal, double Price)
            {
                this.meal = Meal;
                this.price = Price;  
            }
        }

        public class Shoppinglist
        {
            public List<Shopping> shoppinglist = new List<Shopping>();
        }
    }
    
    public class JsonShop
    {
        public string Location;
        public JsonShop(string location)
        {
            this.Location = location;
        }

        public void Save22(Program.Shoppinglist ShoppingData)
        {
            string file = Newtonsoft.Json.JsonConvert.SerializeObject(ShoppingData, Newtonsoft.Json.Formatting.Indented);
            File.WriteAllText(Location, file);
        }

        public Program.Shoppinglist Load22()
        {
            Program.Shoppinglist ShoppingData = Newtonsoft.Json.JsonConvert.DeserializeObject<Program.Shoppinglist>(File.ReadAllText(Location));
            return ShoppingData;
        }
    }
}

This is the JSON code I use:

{
    "shoppinglist": [
    {
        "meal": "Tiramisu",
        "price": 6.5
    },
    {
        "meal": "Pasta carbonara",
        "price": 12.99
    }]
}

Can somebody help me with adding up all the prices? I think the next step is something with a for loop?

1 Answers

You can do this it 2 ways on your code:

  1. you can sum it on the current loop like this:

    public static void Main(string[] args)
    {
            var myJsonString = new JsonShop("ShoppingCart.json");
            var myJsonObject = myJsonString.Load22();
            Console.Clear();
            Console.WriteLine("========================");
            Console.WriteLine("      Shopping cart");
            Console.WriteLine("========================\n");
            double totalCost = 0;
            foreach (Shopping shop in myJsonObject.shoppinglist)
            {
                Console.WriteLine($"Meal: {shop.meal} Price: {shop.price}");
                totalCost += shop.price;
            }
            Console.WriteLine($"Total cost = ");
    }
    
    

2.you can sum it with linq:

public static void Main(string[] args)
{
        var myJsonString = new JsonShop("ShoppingCart.json");
        var myJsonObject = myJsonString.Load22();
        Console.Clear();
        Console.WriteLine("========================");
        Console.WriteLine("      Shopping cart");
        Console.WriteLine("========================\n");
        foreach (Shopping shop in myJsonObject.shoppinglist)
        {
            Console.WriteLine($"Meal: {shop.meal} Price: {shop.price}");
        }
        Console.WriteLine($"Total cost = {myJsonObject.shoppinglist.Sum(x => x.price)}");
}
Related