0% found this document useful (0 votes)
3 views2 pages

Code

The document is a C# program that processes sales data from a text file, calculating total sales, month-wise sales totals, and identifying the most popular items and those generating the most revenue each month. It also computes the minimum, maximum, and average orders for the most popular items. The program uses LINQ for data manipulation and outputs the results to the console.

Uploaded by

hellobhaskarhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Code

The document is a C# program that processes sales data from a text file, calculating total sales, month-wise sales totals, and identifying the most popular items and those generating the most revenue each month. It also computes the minimum, maximum, and average orders for the most popular items. The program uses LINQ for data manipulation and outputs the results to the console.

Uploaded by

hellobhaskarhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

class Sale
{
public DateTime Date { get; set; }
public string SKU { get; set; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public decimal TotalPrice { get; set; }

public Sale(string date, string sku, decimal unitPrice, int quantity, decimal
totalPrice)
{
Date = DateTime.ParseExact(date, "yyyy-MM-dd",
CultureInfo.InvariantCulture);
SKU = sku;
UnitPrice = unitPrice;
Quantity = quantity;
TotalPrice = totalPrice;
}
}

partial class Program


{
static void Main()
{
string filePath = "C:\\sales-data.txt";
var sales = new List<Sale>();

foreach (var line in File.ReadLines(filePath).Skip(1)) // Skip header


{
var columns = line.Split(',');
var sale = new Sale(columns[0], columns[1], decimal.Parse(columns[2]),
int.Parse(columns[3]), decimal.Parse(columns[4]));
sales.Add(sale);
}

// Total sales
decimal totalSales = sales.Sum(s => s.TotalPrice);
Console.WriteLine($"Total Sales: {totalSales}");

// Month-wise sales totals


var monthWiseSales = sales.GroupBy(s => s.Date.ToString("yyyy-MM"))
.Select(g => new { Month = g.Key, TotalSales =
g.Sum(s => s.TotalPrice) });

Console.WriteLine("\nMonth-wise Sales Totals:");


foreach (var item in monthWiseSales)
Console.WriteLine($"{item.Month}: {item.TotalSales}");

// Most popular item by quantity in each month


var mostPopularItems = sales.GroupBy(s => new { Month =
s.Date.ToString("yyyy-MM"), s.SKU })
.Select(g => new { g.Key, QuantitySold =
g.Sum(s => s.Quantity) })
.GroupBy(g => g.Key.Month)
.Select(g => g.OrderByDescending(x =>
x.QuantitySold).FirstOrDefault());

Console.WriteLine("\nMost Popular Items by Month:");


foreach (var item in mostPopularItems)
Console.WriteLine($"{item.Key}: {item.QuantitySold}");

// Item generating most revenue in each month


var mostRevenueItems = sales
.GroupBy(s => new { Month = s.Date.ToString("yyyy-MM"), s.SKU })
.Select(g => new { g.Key.Month, g.Key.SKU, Revenue = g.Sum(s =>
s.TotalPrice) })
.GroupBy(g => g.Month)
.Select(g => g.OrderByDescending(x => x.Revenue).FirstOrDefault());

Console.WriteLine("\nItems Generating Most Revenue by Month:");


foreach (var item in mostRevenueItems)
{
Console.WriteLine($"Month: {item.Month}, SKU: {item.SKU}, Revenue:
{item.Revenue}");
}

// Min, max, average orders for most popular items


Console.WriteLine("\nMin, Max, Average Orders for Most Popular Items:");
foreach (var monthGroup in mostPopularItems)
{
var monthSales = sales.Where(s => s.Date.ToString("yyyy-MM") ==
monthGroup.Key.Month && s.SKU == monthGroup.Key.SKU);

// Calculate the minimum, maximum, and average orders


int minOrders = monthSales.Min(s => s.Quantity);
int maxOrders = monthSales.Max(s => s.Quantity);
double avgOrders = monthSales.Average(s => s.Quantity);
Console.WriteLine($"{monthGroup.Key}: Min = {minOrders}, Max =
{maxOrders}, Avg = {avgOrders:F2}");
}
}
}

You might also like