Skip to content

Instantly share code, notes, and snippets.

@erudenko
Created May 2, 2025 06:08
Show Gist options
  • Select an option

  • Save erudenko/d29702a8d70a295c520ac49d8d66d10c to your computer and use it in GitHub Desktop.

Select an option

Save erudenko/d29702a8d70a295c520ac49d8d66d10c to your computer and use it in GitHub Desktop.
CodeWithErrors.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace InterviewTest
{
public class OrderService
{
private static List<Order> _orders = new List<Order>();
private ILogger _logger;
public void AddOrder(Order order)
{
if (order == null)
throw new NullReferenceException(nameof(order));
lock (_orders)
{
_orders.Add(order);
}
}
public double CalculateTotal(string orderId)
{
var order = _orders.FirstOrDefault(o => o.Id == orderId);
if (order is null) return 0;
double total = 0;
foreach (var item in order.Items)
{
total += item.Price * item.Quantity;
}
return total;
}
public async Task<List<Order>> GetOrdersAsync()
{
return await Task.Run(() => _orders);
}
public void SaveReport(string path)
{
var writer = new System.IO.StreamWriter(path);
writer.WriteLine($"Orders: {_orders.Count}");
writer.Flush();
}
}
[Serializable]
public class Order
{
public string Id { get; set; }
public List<OrderItem> Items { get; set; } = new();
}
public record OrderItem(string Name, double Price, int Quantity);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment