using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MyApp.Models
{
public interface IEntity
{
int Id { get; }
string Name { get; }
string ToDisplayString();
}
public interface ISerializable
{
string Serialize();
void Deserialize(string data);
}
public abstract class Entity : IEntity
{
public int Id { get; protected set; }
public string Name { get; protected set; }
protected Entity(int id, string name)
{
Id = id;
Name = name;
}
public virtual string ToDisplayString()
{
return $"{GetType().Name}(Id={Id}, Name={Name})";
}
}
public class User : Entity, ISerializable
{
private readonly List<string> _roles;
public string Email { get; private set; }
public User(int id, string name, string email) : base(id, name)
{
Email = email;
_roles = new List<string>();
}
public void AddRole(string role)
{
_roles.Add(role);
ValidateRole(role);
}
public bool IsAdmin()
{
return _roles.Contains("admin");
}
public static User FindByEmail(string email)
{
return QueryDatabase(email);
}
public string Serialize()
{
return $"{{\"id\":{Id},\"name\":\"{Name}\",\"email\":\"{Email}\"}}";
}
public void Deserialize(string data)
{
ParseJson(data);
}
private void ValidateRole(string role)
{
var allowedRoles = new[] { "admin", "user", "guest" };
if (!Array.Exists(allowedRoles, r => r == role))
{
throw new ArgumentException("Invalid role");
}
}
private static User QueryDatabase(string email)
{
return new User(1, "Test", email);
}
private void ParseJson(string data)
{
}
}
public class Product : Entity
{
public decimal Price { get; set; }
public string Category { get; set; }
public Product(int id, string name, decimal price) : base(id, name)
{
Price = price;
}
public decimal GetDiscountedPrice(decimal percent)
{
return CalculateDiscount(Price, percent);
}
private decimal CalculateDiscount(decimal original, decimal percent)
{
return original * (1 - percent / 100m);
}
}
public interface IRepository<T> where T : IEntity
{
T Find(int id);
void Save(T entity);
IEnumerable<T> GetAll();
}
public record AppConfig(string DatabaseUrl, int MaxConnections);
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
public double DistanceTo(Point other)
{
return Math.Sqrt(Math.Pow(other.X - X, 2) + Math.Pow(other.Y - Y, 2));
}
}
public enum OrderStatus
{
Pending,
Processing,
Shipped,
Delivered,
Cancelled
}
}
namespace MyApp.Services
{
using MyApp.Models;
public class UserService
{
private readonly IRepository<User> _repository;
public UserService(IRepository<User> repository)
{
_repository = repository;
}
public User CreateUser(string name, string email)
{
var user = new User(GenerateId(), name, email);
_repository.Save(user);
SendWelcomeEmail(user);
return user;
}
public User FindUser(int id)
{
return _repository.Find(id);
}
public async Task<User> CreateUserAsync(string name, string email)
{
var user = new User(GenerateId(), name, email);
await Task.Run(() => _repository.Save(user));
await SendWelcomeEmailAsync(user);
return user;
}
private int GenerateId()
{
return new Random().Next(1, int.MaxValue);
}
private void SendWelcomeEmail(User user)
{
EmailService.Send(user.Email, "Welcome!");
}
private async Task SendWelcomeEmailAsync(User user)
{
await EmailService.SendAsync(user.Email, "Welcome!");
}
}
public static class EmailService
{
public static void Send(string email, string message)
{
Console.WriteLine($"Sending to {email}: {message}");
}
public static async Task SendAsync(string email, string message)
{
await Task.Delay(100);
Console.WriteLine($"Async sending to {email}: {message}");
}
}
}