Created
September 9, 2023 08:39
-
-
Save mvodep/5ff9f5eab2940cc738fd886b5d5d8735 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
record ProductId | |
{ | |
public string URN { get; } | |
private static readonly string URNPattern = @"^urn:ISBN:\d{1,5}-\d{1,7}-\d{1,7}-\d{1}$"; | |
public ProductId(string urn) | |
{ | |
if (!IsValidURN(urn)) | |
{ | |
throw new ArgumentException("Invalid URN: ISBN format is not valid.", nameof(urn)); | |
} | |
URN = urn; | |
} | |
private static bool IsValidURN(string urn) | |
{ | |
return Regex.IsMatch(urn, URNPattern); | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
try | |
{ | |
ProductId book = new ProductId("urn:ISBN:12345-6789101-2345678-9"); | |
Console.WriteLine($"URN: {book.URN}"); | |
} | |
catch (ArgumentException ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment