From 890a3c5ce0f26671d1f962a5353632bbaf6918bd Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Wed, 9 Nov 2022 23:11:23 +0800 Subject: [PATCH 01/17] chore: setting up the development environment --- .gitignore | 454 ++++++++++++++++++ .vscode/launch.json | 26 + .vscode/tasks.json | 41 ++ homework-2-test-first-development.sln | 28 ++ .../UnitTest1.cs | 10 + .../Usings.cs | 1 + ...ework-2-test-first-development.test.csproj | 27 ++ homework-2-test-first-development/Program.cs | 2 + .../homework-2-test-first-development.csproj | 11 + 9 files changed, 600 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 homework-2-test-first-development.sln create mode 100644 homework-2-test-first-development.test/UnitTest1.cs create mode 100644 homework-2-test-first-development.test/Usings.cs create mode 100644 homework-2-test-first-development.test/homework-2-test-first-development.test.csproj create mode 100644 homework-2-test-first-development/Program.cs create mode 100644 homework-2-test-first-development/homework-2-test-first-development.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a72f3dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,454 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET +project.lock.json +project.fragment.lock.json +artifacts/ + +# Tye +.tye/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +## +## Visual studio for Mac +## + + +# globs +Makefile.in +*.userprefs +*.usertasks +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.tar.gz +tarballs/ +test-results/ + +# Mac bundle stuff +*.dmg +*.app + +# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# JetBrains Rider +.idea/ +*.sln.iml + +## +## Visual Studio Code +## +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..29a14c1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/homework-2-test-first-development/bin/Debug/net6.0/homework-2-test-first-development.dll", + "args": [], + "cwd": "${workspaceFolder}/homework-2-test-first-development", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..6082ae1 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/homework-2-test-first-development/homework-2-test-first-development.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/homework-2-test-first-development/homework-2-test-first-development.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/homework-2-test-first-development/homework-2-test-first-development.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/homework-2-test-first-development.sln b/homework-2-test-first-development.sln new file mode 100644 index 0000000..6369438 --- /dev/null +++ b/homework-2-test-first-development.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "homework-2-test-first-development", "homework-2-test-first-development\homework-2-test-first-development.csproj", "{F9AEC99A-E67B-44EB-9CD9-92DC0DD47AB0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "homework-2-test-first-development.test", "homework-2-test-first-development.test\homework-2-test-first-development.test.csproj", "{05E1FD50-7888-4A6B-B3C6-5160937F846F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F9AEC99A-E67B-44EB-9CD9-92DC0DD47AB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F9AEC99A-E67B-44EB-9CD9-92DC0DD47AB0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F9AEC99A-E67B-44EB-9CD9-92DC0DD47AB0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F9AEC99A-E67B-44EB-9CD9-92DC0DD47AB0}.Release|Any CPU.Build.0 = Release|Any CPU + {05E1FD50-7888-4A6B-B3C6-5160937F846F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {05E1FD50-7888-4A6B-B3C6-5160937F846F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {05E1FD50-7888-4A6B-B3C6-5160937F846F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {05E1FD50-7888-4A6B-B3C6-5160937F846F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/homework-2-test-first-development.test/UnitTest1.cs b/homework-2-test-first-development.test/UnitTest1.cs new file mode 100644 index 0000000..d486a8f --- /dev/null +++ b/homework-2-test-first-development.test/UnitTest1.cs @@ -0,0 +1,10 @@ +namespace homework_2_test_first_development.test; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + + } +} \ No newline at end of file diff --git a/homework-2-test-first-development.test/Usings.cs b/homework-2-test-first-development.test/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/homework-2-test-first-development.test/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/homework-2-test-first-development.test/homework-2-test-first-development.test.csproj b/homework-2-test-first-development.test/homework-2-test-first-development.test.csproj new file mode 100644 index 0000000..338afd6 --- /dev/null +++ b/homework-2-test-first-development.test/homework-2-test-first-development.test.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + homework_2_test_first_development.test + enable + enable + + false + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/homework-2-test-first-development/Program.cs b/homework-2-test-first-development/Program.cs new file mode 100644 index 0000000..83fa4f4 --- /dev/null +++ b/homework-2-test-first-development/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/homework-2-test-first-development/homework-2-test-first-development.csproj b/homework-2-test-first-development/homework-2-test-first-development.csproj new file mode 100644 index 0000000..1dded12 --- /dev/null +++ b/homework-2-test-first-development/homework-2-test-first-development.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + homework_2_test_first_development + enable + enable + + + -- Gitee From 1801d6356ddf1a03f7f5b5d6509d6f82f41ac23b Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 01:12:07 +0800 Subject: [PATCH 02/17] feat: validation will fail when barcode contains special characters --- .../BarcodeValidatorTest.cs | 39 +++++++++++++++++++ .../UnitTest1.cs | 10 ----- ...ework-2-test-first-development.test.csproj | 4 ++ .../Enums/ErrorType.cs | 8 ++++ .../Models/Product.cs | 10 +++++ .../Validators/BarcodeValidator.cs | 22 +++++++++++ .../Validators/ValidationResult.cs | 10 +++++ 7 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 homework-2-test-first-development.test/BarcodeValidatorTest.cs delete mode 100644 homework-2-test-first-development.test/UnitTest1.cs create mode 100644 homework-2-test-first-development/Enums/ErrorType.cs create mode 100644 homework-2-test-first-development/Models/Product.cs create mode 100644 homework-2-test-first-development/Validators/BarcodeValidator.cs create mode 100644 homework-2-test-first-development/Validators/ValidationResult.cs diff --git a/homework-2-test-first-development.test/BarcodeValidatorTest.cs b/homework-2-test-first-development.test/BarcodeValidatorTest.cs new file mode 100644 index 0000000..e85ead1 --- /dev/null +++ b/homework-2-test-first-development.test/BarcodeValidatorTest.cs @@ -0,0 +1,39 @@ +using AutoFixture; +using FluentAssertions; +using homework_2_test_first_development.Enums; +using homework_2_test_first_development.Models; +using homework_2_test_first_development.Validators; + +namespace homework_2_test_first_development.test; + +public class BarcodeValidatorTest +{ + private readonly BarcodeValidator _barcodeValidator = new(); + private readonly Fixture _fixture = new(); + + public BarcodeValidatorTest() + { + } + + [Fact] + public void Should_Error_Return_Invalid_Barcode_When_Barcode_Contains_Special_Characters() + { + // Given + List selectedBarcodes = new List { + "ITEM000001", + "ITEM000001", + "ITEM000001", + "ITEM000003-2", + "(ITEM000001)", + }; + + var products = _fixture.CreateMany(); + + // When + var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + + // Then + validationResult.Should().BeOfType().Which + .Error.Should().Be(ErrorType.InvalidBarcode); + } +} \ No newline at end of file diff --git a/homework-2-test-first-development.test/UnitTest1.cs b/homework-2-test-first-development.test/UnitTest1.cs deleted file mode 100644 index d486a8f..0000000 --- a/homework-2-test-first-development.test/UnitTest1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace homework_2_test_first_development.test; - -public class UnitTest1 -{ - [Fact] - public void Test1() - { - - } -} \ No newline at end of file diff --git a/homework-2-test-first-development.test/homework-2-test-first-development.test.csproj b/homework-2-test-first-development.test/homework-2-test-first-development.test.csproj index 338afd6..9b6ab6f 100644 --- a/homework-2-test-first-development.test/homework-2-test-first-development.test.csproj +++ b/homework-2-test-first-development.test/homework-2-test-first-development.test.csproj @@ -24,4 +24,8 @@ + + + + diff --git a/homework-2-test-first-development/Enums/ErrorType.cs b/homework-2-test-first-development/Enums/ErrorType.cs new file mode 100644 index 0000000..9645383 --- /dev/null +++ b/homework-2-test-first-development/Enums/ErrorType.cs @@ -0,0 +1,8 @@ +namespace homework_2_test_first_development.Enums; + +public enum ErrorType +{ + Valid, + InvalidBarcode, + ProductNotFound +} diff --git a/homework-2-test-first-development/Models/Product.cs b/homework-2-test-first-development/Models/Product.cs new file mode 100644 index 0000000..4f6e653 --- /dev/null +++ b/homework-2-test-first-development/Models/Product.cs @@ -0,0 +1,10 @@ +namespace homework_2_test_first_development.Models; + +public class Product +{ + public string Name { get; set; } = string.Empty; + + public decimal Price { get; set; } + + public string Barcode { get; set; } = string.Empty; +} diff --git a/homework-2-test-first-development/Validators/BarcodeValidator.cs b/homework-2-test-first-development/Validators/BarcodeValidator.cs new file mode 100644 index 0000000..74ab857 --- /dev/null +++ b/homework-2-test-first-development/Validators/BarcodeValidator.cs @@ -0,0 +1,22 @@ +using homework_2_test_first_development.Enums; +using homework_2_test_first_development.Models; + +namespace homework_2_test_first_development.Validators; + +public class BarcodeValidator +{ + public ValidationResult ValidateBarcodes(IEnumerable selectedBarcodes, IEnumerable products) + { + foreach (var item in selectedBarcodes) + { + var barcodeParts = item.Split('-'); + + var barcode = barcodeParts.ElementAtOrDefault(0); + + if (!string.IsNullOrEmpty(barcode) && barcode.Any(c => !(Char.IsDigit(c) || Char.IsUpper(c)))) + return new ValidationResult { Error = ErrorType.InvalidBarcode }; + } + + return new ValidationResult() { Error = ErrorType.Valid }; + } +} diff --git a/homework-2-test-first-development/Validators/ValidationResult.cs b/homework-2-test-first-development/Validators/ValidationResult.cs new file mode 100644 index 0000000..f980c4f --- /dev/null +++ b/homework-2-test-first-development/Validators/ValidationResult.cs @@ -0,0 +1,10 @@ +using homework_2_test_first_development.Enums; + +namespace homework_2_test_first_development.Validators; + +public record struct ValidationResult +{ + public string? Barcode { get; init; } + + public ErrorType Error { get; init; } +} -- Gitee From 0b7ffc2ea9fb822fa98a0b24bf41aae784ec00e5 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 01:18:50 +0800 Subject: [PATCH 03/17] feat: validation will fail when amount contains special characters --- .../BarcodeValidatorTest.cs | 24 ++++++++++++++++++- .../Validators/BarcodeValidator.cs | 4 ++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/homework-2-test-first-development.test/BarcodeValidatorTest.cs b/homework-2-test-first-development.test/BarcodeValidatorTest.cs index e85ead1..c6dd5d9 100644 --- a/homework-2-test-first-development.test/BarcodeValidatorTest.cs +++ b/homework-2-test-first-development.test/BarcodeValidatorTest.cs @@ -16,7 +16,7 @@ public class BarcodeValidatorTest } [Fact] - public void Should_Error_Return_Invalid_Barcode_When_Barcode_Contains_Special_Characters() + public void Should_Return_Error_Invalid_Barcode_When_Barcode_Contains_Special_Characters() { // Given List selectedBarcodes = new List { @@ -36,4 +36,26 @@ public class BarcodeValidatorTest validationResult.Should().BeOfType().Which .Error.Should().Be(ErrorType.InvalidBarcode); } + + [Fact] + public void Should_Return_Error_Invalid_Barcode_When_Amount_Contains_Special_Characters() + { + // Given + List selectedBarcodes = new List { + "ITEM000001", + "ITEM000001", + "ITEM000001", + "ITEM000003-2", + "ITEM000003-2A", + }; + + var products = _fixture.CreateMany(); + + // When + var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + + // Then + validationResult.Should().BeOfType().Which + .Error.Should().Be(ErrorType.InvalidBarcode); + } } \ No newline at end of file diff --git a/homework-2-test-first-development/Validators/BarcodeValidator.cs b/homework-2-test-first-development/Validators/BarcodeValidator.cs index 74ab857..3d45561 100644 --- a/homework-2-test-first-development/Validators/BarcodeValidator.cs +++ b/homework-2-test-first-development/Validators/BarcodeValidator.cs @@ -12,9 +12,13 @@ public class BarcodeValidator var barcodeParts = item.Split('-'); var barcode = barcodeParts.ElementAtOrDefault(0); + var amount = barcodeParts.ElementAtOrDefault(1); if (!string.IsNullOrEmpty(barcode) && barcode.Any(c => !(Char.IsDigit(c) || Char.IsUpper(c)))) return new ValidationResult { Error = ErrorType.InvalidBarcode }; + + if (!string.IsNullOrEmpty(amount) && amount.Any(c => !Char.IsDigit(c))) + return new ValidationResult { Error = ErrorType.InvalidBarcode }; } return new ValidationResult() { Error = ErrorType.Valid }; -- Gitee From 772e83d2809a85efd7f7d85bb480ddb4be5dc473 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 17:53:36 +0800 Subject: [PATCH 04/17] feat: validation will fail when one of the barcodes is empty --- .../BarcodeValidatorTest.cs | 22 +++++++++++++++++++ .../Validators/BarcodeValidator.cs | 3 +++ 2 files changed, 25 insertions(+) diff --git a/homework-2-test-first-development.test/BarcodeValidatorTest.cs b/homework-2-test-first-development.test/BarcodeValidatorTest.cs index c6dd5d9..3fa3c39 100644 --- a/homework-2-test-first-development.test/BarcodeValidatorTest.cs +++ b/homework-2-test-first-development.test/BarcodeValidatorTest.cs @@ -58,4 +58,26 @@ public class BarcodeValidatorTest validationResult.Should().BeOfType().Which .Error.Should().Be(ErrorType.InvalidBarcode); } + + [Fact] + public void Should_Return_Error_Invalid_Barcode_When_Barcode_Is_Empty() + { + // Given + List selectedBarcodes = new List { + "ITEM000001", + "ITEM000001", + "ITEM000001", + "ITEM000003-2", + "", + }; + + var products = _fixture.CreateMany(); + + // When + var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + + // Then + validationResult.Should().BeOfType().Which + .Error.Should().Be(ErrorType.InvalidBarcode); + } } \ No newline at end of file diff --git a/homework-2-test-first-development/Validators/BarcodeValidator.cs b/homework-2-test-first-development/Validators/BarcodeValidator.cs index 3d45561..73ec150 100644 --- a/homework-2-test-first-development/Validators/BarcodeValidator.cs +++ b/homework-2-test-first-development/Validators/BarcodeValidator.cs @@ -9,6 +9,9 @@ public class BarcodeValidator { foreach (var item in selectedBarcodes) { + if (string.IsNullOrEmpty(item)) + return new ValidationResult { Error = ErrorType.InvalidBarcode }; + var barcodeParts = item.Split('-'); var barcode = barcodeParts.ElementAtOrDefault(0); -- Gitee From 7c1ec8eb7bb9f5f043c42549f35955620296e038 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 18:13:32 +0800 Subject: [PATCH 05/17] feat: validation will fail when barcode is empty but amount not empty --- .../BarcodeValidatorTest.cs | 22 +++++++++++++++++ .../Validators/BarcodeValidator.cs | 24 ++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/homework-2-test-first-development.test/BarcodeValidatorTest.cs b/homework-2-test-first-development.test/BarcodeValidatorTest.cs index 3fa3c39..7865fd4 100644 --- a/homework-2-test-first-development.test/BarcodeValidatorTest.cs +++ b/homework-2-test-first-development.test/BarcodeValidatorTest.cs @@ -80,4 +80,26 @@ public class BarcodeValidatorTest validationResult.Should().BeOfType().Which .Error.Should().Be(ErrorType.InvalidBarcode); } + + [Fact] + public void Should_Return_Error_Invalid_Barcode_When_Barcode_Is_Empty_But_Amount_Not_Empty() + { + // Given + List selectedBarcodes = new List { + "ITEM000001", + "ITEM000001", + "ITEM000001", + "ITEM000003-2", + "-3", + }; + + var products = _fixture.CreateMany(); + + // When + var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + + // Then + validationResult.Should().BeOfType().Which + .Error.Should().Be(ErrorType.InvalidBarcode); + } } \ No newline at end of file diff --git a/homework-2-test-first-development/Validators/BarcodeValidator.cs b/homework-2-test-first-development/Validators/BarcodeValidator.cs index 73ec150..7c80ad7 100644 --- a/homework-2-test-first-development/Validators/BarcodeValidator.cs +++ b/homework-2-test-first-development/Validators/BarcodeValidator.cs @@ -7,20 +7,32 @@ public class BarcodeValidator { public ValidationResult ValidateBarcodes(IEnumerable selectedBarcodes, IEnumerable products) { + const char seperator = '-'; + foreach (var item in selectedBarcodes) { if (string.IsNullOrEmpty(item)) return new ValidationResult { Error = ErrorType.InvalidBarcode }; - var barcodeParts = item.Split('-'); + string barcode = string.Empty; + string amount = string.Empty; - var barcode = barcodeParts.ElementAtOrDefault(0); - var amount = barcodeParts.ElementAtOrDefault(1); + if (item.Contains(seperator)) + { + var barcodeParts = item.Split(seperator); - if (!string.IsNullOrEmpty(barcode) && barcode.Any(c => !(Char.IsDigit(c) || Char.IsUpper(c)))) - return new ValidationResult { Error = ErrorType.InvalidBarcode }; + barcode = barcodeParts[0]; + amount = barcodeParts[1]; + + if (string.IsNullOrEmpty(amount) || amount.Any(c => !Char.IsDigit(c))) + return new ValidationResult { Error = ErrorType.InvalidBarcode }; + } + else + { + barcode = item; + } - if (!string.IsNullOrEmpty(amount) && amount.Any(c => !Char.IsDigit(c))) + if (string.IsNullOrEmpty(barcode) || barcode.Any(c => !(Char.IsDigit(c) || Char.IsUpper(c)))) return new ValidationResult { Error = ErrorType.InvalidBarcode }; } -- Gitee From dedcafc59737773b57c2186a8305c6e042589532 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 18:21:17 +0800 Subject: [PATCH 06/17] feat: validation will fail when amount larger than 99 --- .../BarcodeValidatorTest.cs | 22 +++++++++++++++++++ .../Validators/BarcodeValidator.cs | 14 ++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/homework-2-test-first-development.test/BarcodeValidatorTest.cs b/homework-2-test-first-development.test/BarcodeValidatorTest.cs index 7865fd4..509cd87 100644 --- a/homework-2-test-first-development.test/BarcodeValidatorTest.cs +++ b/homework-2-test-first-development.test/BarcodeValidatorTest.cs @@ -102,4 +102,26 @@ public class BarcodeValidatorTest validationResult.Should().BeOfType().Which .Error.Should().Be(ErrorType.InvalidBarcode); } + + [Fact] + public void Should_Return_Error_Invalid_Barcode_When_Amount_Larger_Than_Maximum() + { + // Given + List selectedBarcodes = new List { + "ITEM000001", + "ITEM000001", + "ITEM000001", + "ITEM000003-2", + "ITEM000004-100", + }; + + var products = _fixture.CreateMany(); + + // When + var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + + // Then + validationResult.Should().BeOfType().Which + .Error.Should().Be(ErrorType.InvalidBarcode); + } } \ No newline at end of file diff --git a/homework-2-test-first-development/Validators/BarcodeValidator.cs b/homework-2-test-first-development/Validators/BarcodeValidator.cs index 7c80ad7..1ce7da6 100644 --- a/homework-2-test-first-development/Validators/BarcodeValidator.cs +++ b/homework-2-test-first-development/Validators/BarcodeValidator.cs @@ -5,10 +5,11 @@ namespace homework_2_test_first_development.Validators; public class BarcodeValidator { + public static char Seperator => '-'; + public static int Maximum => 99; + public ValidationResult ValidateBarcodes(IEnumerable selectedBarcodes, IEnumerable products) { - const char seperator = '-'; - foreach (var item in selectedBarcodes) { if (string.IsNullOrEmpty(item)) @@ -17,15 +18,20 @@ public class BarcodeValidator string barcode = string.Empty; string amount = string.Empty; - if (item.Contains(seperator)) + if (item.Contains(Seperator)) { - var barcodeParts = item.Split(seperator); + var barcodeParts = item.Split(Seperator); barcode = barcodeParts[0]; amount = barcodeParts[1]; if (string.IsNullOrEmpty(amount) || amount.Any(c => !Char.IsDigit(c))) return new ValidationResult { Error = ErrorType.InvalidBarcode }; + + int number = int.Parse(amount); + + if (number > Maximum) + return new ValidationResult { Error = ErrorType.InvalidBarcode }; } else { -- Gitee From e6e82c9b050015097bc9d23c90c3dd6ab92af6be Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 19:23:43 +0800 Subject: [PATCH 07/17] refactor: contruct test data automatically other than manually --- .../BarcodeValidatorTest.cs | 59 ++++++------------- 1 file changed, 19 insertions(+), 40 deletions(-) diff --git a/homework-2-test-first-development.test/BarcodeValidatorTest.cs b/homework-2-test-first-development.test/BarcodeValidatorTest.cs index 509cd87..528bfb4 100644 --- a/homework-2-test-first-development.test/BarcodeValidatorTest.cs +++ b/homework-2-test-first-development.test/BarcodeValidatorTest.cs @@ -10,24 +10,23 @@ public class BarcodeValidatorTest { private readonly BarcodeValidator _barcodeValidator = new(); private readonly Fixture _fixture = new(); + private static string BarcodePrefix = "ITEM"; public BarcodeValidatorTest() { + _fixture.Customize(c => c.FromFactory( + (string name, decimal price, int id) => new Product { Name = name, Price = price, Barcode = $"{BarcodePrefix}{id:D6}"}) + .OmitAutoProperties()); } [Fact] public void Should_Return_Error_Invalid_Barcode_When_Barcode_Contains_Special_Characters() { // Given - List selectedBarcodes = new List { - "ITEM000001", - "ITEM000001", - "ITEM000001", - "ITEM000003-2", - "(ITEM000001)", - }; - var products = _fixture.CreateMany(); + var invalidBarcode = "(ITEM000001)"; + + var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); @@ -41,15 +40,10 @@ public class BarcodeValidatorTest public void Should_Return_Error_Invalid_Barcode_When_Amount_Contains_Special_Characters() { // Given - List selectedBarcodes = new List { - "ITEM000001", - "ITEM000001", - "ITEM000001", - "ITEM000003-2", - "ITEM000003-2A", - }; - var products = _fixture.CreateMany(); + var invalidBarcode = "ITEM000003-2A"; + + var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); @@ -63,15 +57,10 @@ public class BarcodeValidatorTest public void Should_Return_Error_Invalid_Barcode_When_Barcode_Is_Empty() { // Given - List selectedBarcodes = new List { - "ITEM000001", - "ITEM000001", - "ITEM000001", - "ITEM000003-2", - "", - }; - var products = _fixture.CreateMany(); + var invalidBarcode = string.Empty; + + var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); @@ -85,15 +74,10 @@ public class BarcodeValidatorTest public void Should_Return_Error_Invalid_Barcode_When_Barcode_Is_Empty_But_Amount_Not_Empty() { // Given - List selectedBarcodes = new List { - "ITEM000001", - "ITEM000001", - "ITEM000001", - "ITEM000003-2", - "-3", - }; - var products = _fixture.CreateMany(); + var invalidBarcode = "-3"; + + var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); @@ -107,15 +91,10 @@ public class BarcodeValidatorTest public void Should_Return_Error_Invalid_Barcode_When_Amount_Larger_Than_Maximum() { // Given - List selectedBarcodes = new List { - "ITEM000001", - "ITEM000001", - "ITEM000001", - "ITEM000003-2", - "ITEM000004-100", - }; - var products = _fixture.CreateMany(); + var invalidBarcode = "ITEM000004-100"; + + var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); -- Gitee From 35c594cd6360ff22aa96e9ccab57f37c8e088933 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 19:30:22 +0800 Subject: [PATCH 08/17] feat: validation will fail when barcode not found --- .../BarcodeValidatorTest.cs | 17 +++++++++++++++++ .../Validators/BarcodeValidator.cs | 3 +++ 2 files changed, 20 insertions(+) diff --git a/homework-2-test-first-development.test/BarcodeValidatorTest.cs b/homework-2-test-first-development.test/BarcodeValidatorTest.cs index 528bfb4..66b7950 100644 --- a/homework-2-test-first-development.test/BarcodeValidatorTest.cs +++ b/homework-2-test-first-development.test/BarcodeValidatorTest.cs @@ -103,4 +103,21 @@ public class BarcodeValidatorTest validationResult.Should().BeOfType().Which .Error.Should().Be(ErrorType.InvalidBarcode); } + + [Fact] + public void Should_Return_Error_Product_Not_Found_When_Product_Not_Found() + { + // Given + var products = _fixture.CreateMany(); + var invalidBarcode = "ITEM0000001-2"; + + var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); + + // When + var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + + // Then + validationResult.Should().BeOfType().Which + .Error.Should().Be(ErrorType.ProductNotFound); + } } \ No newline at end of file diff --git a/homework-2-test-first-development/Validators/BarcodeValidator.cs b/homework-2-test-first-development/Validators/BarcodeValidator.cs index 1ce7da6..cb1a0f0 100644 --- a/homework-2-test-first-development/Validators/BarcodeValidator.cs +++ b/homework-2-test-first-development/Validators/BarcodeValidator.cs @@ -40,6 +40,9 @@ public class BarcodeValidator if (string.IsNullOrEmpty(barcode) || barcode.Any(c => !(Char.IsDigit(c) || Char.IsUpper(c)))) return new ValidationResult { Error = ErrorType.InvalidBarcode }; + + if (!products.Where(p => p.Barcode == barcode).Any()) + return new ValidationResult { Error = ErrorType.ProductNotFound }; } return new ValidationResult() { Error = ErrorType.Valid }; -- Gitee From a70600803f4af24ca089f00892b62ad4cce992a8 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 19:33:13 +0800 Subject: [PATCH 09/17] test: add happy path --- .../BarcodeValidatorTest.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/homework-2-test-first-development.test/BarcodeValidatorTest.cs b/homework-2-test-first-development.test/BarcodeValidatorTest.cs index 66b7950..f00e53b 100644 --- a/homework-2-test-first-development.test/BarcodeValidatorTest.cs +++ b/homework-2-test-first-development.test/BarcodeValidatorTest.cs @@ -120,4 +120,19 @@ public class BarcodeValidatorTest validationResult.Should().BeOfType().Which .Error.Should().Be(ErrorType.ProductNotFound); } + + [Fact] + public void Should_Return_Valid_When_Everything_Is_OK() + { + // Given + var products = _fixture.CreateMany(); + var selectedBarcodes = products.Select(p => p.Barcode); + + // When + var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + + // Then + validationResult.Should().BeOfType().Which + .Error.Should().Be(ErrorType.Valid); + } } \ No newline at end of file -- Gitee From f0d5202fdceefe3f231fa4b1092f6aab86482b5f Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 23:09:32 +0800 Subject: [PATCH 10/17] feat: should print not recognized when barcodes format error --- .../ReceiptPrinterTest.cs | 40 ++++++++++++++++++ .../Services/ReceiptPrinter.cs | 41 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 homework-2-test-first-development.test/ReceiptPrinterTest.cs create mode 100644 homework-2-test-first-development/Services/ReceiptPrinter.cs diff --git a/homework-2-test-first-development.test/ReceiptPrinterTest.cs b/homework-2-test-first-development.test/ReceiptPrinterTest.cs new file mode 100644 index 0000000..f7888d0 --- /dev/null +++ b/homework-2-test-first-development.test/ReceiptPrinterTest.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AutoFixture; +using FluentAssertions; +using homework_2_test_first_development.Models; +using homework_2_test_first_development.Services; +using Xunit; + +namespace homework_2_test_first_development.test; + +public class ReceiptPrinterTest +{ + private readonly Fixture _fixture = new(); + private static string BarcodePrefix = "ITEM"; + + public ReceiptPrinterTest() + { + _fixture.Customize(c => c.FromFactory( + (string name, decimal price, int id) => new Product { Name = name, Price = price, Barcode = $"{BarcodePrefix}{id:D6}"}) + .OmitAutoProperties()); + } + + [Fact] + public void Should_Print_Not_Recognized_When_Barcodes_Format_Error() + { + // Given + var products = _fixture.CreateMany(); + var invalidBarcode = "(ITEM000001)"; + + var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); + + // When + var receiptContent = ReceiptPrinter.PrintReceipt(selectedBarcodes, products); + + // Then + receiptContent.Should().Contain("ERROR").And.Contain("recognized"); + } +} diff --git a/homework-2-test-first-development/Services/ReceiptPrinter.cs b/homework-2-test-first-development/Services/ReceiptPrinter.cs new file mode 100644 index 0000000..a89c478 --- /dev/null +++ b/homework-2-test-first-development/Services/ReceiptPrinter.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using homework_2_test_first_development.Enums; +using homework_2_test_first_development.Models; +using homework_2_test_first_development.Validators; + +namespace homework_2_test_first_development.Services; + +public class ReceiptPrinter +{ + public static string PrintReceipt(IEnumerable selectedBarcodes, IEnumerable products) + { + string receiptContent = string.Empty; + + BarcodeValidator validator = new BarcodeValidator(); + var validationResult = validator.ValidateBarcodes(selectedBarcodes, products); + + if (validationResult.Error != ErrorType.Valid) + { + receiptContent = CreateErrorReceipt(validationResult); + } + + return receiptContent; + } + + private static string CreateErrorReceipt(ValidationResult validationResult) + { + string[] header = { "ERROR", "-------" }; + + string body = validationResult.Error switch + { + ErrorType.InvalidBarcode => "The barcode cannot be recognized", + _ => throw new ArgumentOutOfRangeException(nameof(validationResult.Error), + $"Not expected Error type: {validationResult.Error}"), + }; + + return string.Join(Environment.NewLine, header.Append(body)); + } +} -- Gitee From ec4d0d4c6f8a055659b226023f255f664ff833a9 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 23:15:53 +0800 Subject: [PATCH 11/17] feat: should print product not found when barcode not found --- .../ReceiptPrinterTest.cs | 16 ++++++++++++++++ .../Services/ReceiptPrinter.cs | 1 + .../Validators/BarcodeValidator.cs | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/homework-2-test-first-development.test/ReceiptPrinterTest.cs b/homework-2-test-first-development.test/ReceiptPrinterTest.cs index f7888d0..ec59a47 100644 --- a/homework-2-test-first-development.test/ReceiptPrinterTest.cs +++ b/homework-2-test-first-development.test/ReceiptPrinterTest.cs @@ -37,4 +37,20 @@ public class ReceiptPrinterTest // Then receiptContent.Should().Contain("ERROR").And.Contain("recognized"); } + + [Fact] + public void Should_Print_Product_Not_Found_When_Barcode_Not_Found() + { + // Given + var products = _fixture.CreateMany(); + var invalidBarcode = "ITEM0000001"; + + var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); + + // When + var receiptContent = ReceiptPrinter.PrintReceipt(selectedBarcodes, products); + + // Then + receiptContent.Should().Contain("ERROR").And.Contain(invalidBarcode); + } } diff --git a/homework-2-test-first-development/Services/ReceiptPrinter.cs b/homework-2-test-first-development/Services/ReceiptPrinter.cs index a89c478..d8f3736 100644 --- a/homework-2-test-first-development/Services/ReceiptPrinter.cs +++ b/homework-2-test-first-development/Services/ReceiptPrinter.cs @@ -32,6 +32,7 @@ public class ReceiptPrinter string body = validationResult.Error switch { ErrorType.InvalidBarcode => "The barcode cannot be recognized", + ErrorType.ProductNotFound => $"The product cannot be found: {validationResult.Barcode}", _ => throw new ArgumentOutOfRangeException(nameof(validationResult.Error), $"Not expected Error type: {validationResult.Error}"), }; diff --git a/homework-2-test-first-development/Validators/BarcodeValidator.cs b/homework-2-test-first-development/Validators/BarcodeValidator.cs index cb1a0f0..77ea2ad 100644 --- a/homework-2-test-first-development/Validators/BarcodeValidator.cs +++ b/homework-2-test-first-development/Validators/BarcodeValidator.cs @@ -42,7 +42,7 @@ public class BarcodeValidator return new ValidationResult { Error = ErrorType.InvalidBarcode }; if (!products.Where(p => p.Barcode == barcode).Any()) - return new ValidationResult { Error = ErrorType.ProductNotFound }; + return new ValidationResult { Error = ErrorType.ProductNotFound, Barcode = barcode }; } return new ValidationResult() { Error = ErrorType.Valid }; -- Gitee From a73bcffc020f87017def07c9e28451a99cecfb59 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Thu, 10 Nov 2022 23:46:43 +0800 Subject: [PATCH 12/17] feat: should parse barcodes successfully --- .../BarcodeProcessorTest.cs | 28 +++++++++++++++ .../Models/OrderItem.cs | 8 +++++ .../Services/BarcodeProcessor.cs | 35 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 homework-2-test-first-development.test/BarcodeProcessorTest.cs create mode 100644 homework-2-test-first-development/Models/OrderItem.cs create mode 100644 homework-2-test-first-development/Services/BarcodeProcessor.cs diff --git a/homework-2-test-first-development.test/BarcodeProcessorTest.cs b/homework-2-test-first-development.test/BarcodeProcessorTest.cs new file mode 100644 index 0000000..9979a94 --- /dev/null +++ b/homework-2-test-first-development.test/BarcodeProcessorTest.cs @@ -0,0 +1,28 @@ +using FluentAssertions; +using homework_2_test_first_development.Services; + +namespace homework_2_test_first_development.test; + +public class BarcodeProcessorTest +{ + [Fact] + public void Should_Parse_Successfully_When_Barcodes_Are_Regular() + { + // Given + IEnumerable selectedBarcodes = new string[] + { + "ITEM000001", + "ITEM000001", + "ITEM000001", + "ITEM000003-2" + }; + + // When + var orderItems = BarcodeProcessor.ParseBarcodes(selectedBarcodes); + + // Then + orderItems.Should().HaveCount(4) + .And.Contain(o => o.Barcode == "ITEM000001") + .And.Contain(o => o.Barcode == "ITEM000003"); + } +} diff --git a/homework-2-test-first-development/Models/OrderItem.cs b/homework-2-test-first-development/Models/OrderItem.cs new file mode 100644 index 0000000..d6b1827 --- /dev/null +++ b/homework-2-test-first-development/Models/OrderItem.cs @@ -0,0 +1,8 @@ +namespace homework_2_test_first_development.Models; + +public class OrderItem +{ + public string Barcode { get; set; } = string.Empty; + + public int Amount { get; set; } +} diff --git a/homework-2-test-first-development/Services/BarcodeProcessor.cs b/homework-2-test-first-development/Services/BarcodeProcessor.cs new file mode 100644 index 0000000..705b81c --- /dev/null +++ b/homework-2-test-first-development/Services/BarcodeProcessor.cs @@ -0,0 +1,35 @@ +using homework_2_test_first_development.Models; + +namespace homework_2_test_first_development.Services; + +public class BarcodeProcessor +{ + public static char Seperator => '-'; + + public static IEnumerable ParseBarcodes(IEnumerable selectedBarcodes) + { + List orderItems = new List(); + + foreach (var item in selectedBarcodes) + { + string barcode = string.Empty; + int amount = 1; + + if (item.Contains(Seperator)) + { + var barcodeParts = item.Split(Seperator); + + barcode = barcodeParts[0]; + amount = int.Parse(barcodeParts[1]); + } + else + { + barcode = item; + } + + orderItems.Add(new OrderItem { Barcode = barcode, Amount = amount }); + } + + return orderItems; + } +} -- Gitee From 436fcf47b09158196bba12e10db2527493977a68 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Fri, 11 Nov 2022 00:13:09 +0800 Subject: [PATCH 13/17] feat: add group barcodes --- .../BarcodeProcessorTest.cs | 23 +++++++++++++++++++ .../Services/BarcodeProcessor.cs | 6 +++++ 2 files changed, 29 insertions(+) diff --git a/homework-2-test-first-development.test/BarcodeProcessorTest.cs b/homework-2-test-first-development.test/BarcodeProcessorTest.cs index 9979a94..61aaa91 100644 --- a/homework-2-test-first-development.test/BarcodeProcessorTest.cs +++ b/homework-2-test-first-development.test/BarcodeProcessorTest.cs @@ -1,4 +1,5 @@ using FluentAssertions; +using homework_2_test_first_development.Models; using homework_2_test_first_development.Services; namespace homework_2_test_first_development.test; @@ -25,4 +26,26 @@ public class BarcodeProcessorTest .And.Contain(o => o.Barcode == "ITEM000001") .And.Contain(o => o.Barcode == "ITEM000003"); } + + [Fact] + public void Should_Group_Successfully_When_Barcodes_Are_Regular() + { + // Given + IEnumerable barcodesAndAmount = new OrderItem[] + { + new OrderItem {Barcode = "ITEM000001", Amount = 1}, + new OrderItem {Barcode = "ITEM000001", Amount = 1}, + new OrderItem {Barcode = "ITEM000001", Amount = 1}, + new OrderItem {Barcode = "ITEM000003", Amount = 2}, + }; + + // When + var groupedBarcodes = BarcodeProcessor.GroupBarcodes(barcodesAndAmount); + + // Then + groupedBarcodes.Should().HaveCount(2).And.BeEquivalentTo(new OrderItem[] { + new OrderItem { Barcode = "ITEM000001", Amount = 3}, + new OrderItem { Barcode = "ITEM000003", Amount = 2}, + }); + } } diff --git a/homework-2-test-first-development/Services/BarcodeProcessor.cs b/homework-2-test-first-development/Services/BarcodeProcessor.cs index 705b81c..2d1b2b7 100644 --- a/homework-2-test-first-development/Services/BarcodeProcessor.cs +++ b/homework-2-test-first-development/Services/BarcodeProcessor.cs @@ -32,4 +32,10 @@ public class BarcodeProcessor return orderItems; } + + public static IEnumerable GroupBarcodes(IEnumerable barcodesAndAmount) + { + return barcodesAndAmount.GroupBy(o => o.Barcode) + .Select(g => new OrderItem { Barcode = g.Key, Amount = g.Sum(o => o.Amount)}); + } } -- Gitee From faa386d4ba03719b0bf4b029547c675eb149af5a Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Fri, 11 Nov 2022 17:31:31 +0800 Subject: [PATCH 14/17] feat: add create receipt logic --- .../ReceiptCreatorTest.cs | 38 +++++++++++++++++++ .../Exceptions/ProductNotFoundException.cs | 5 +++ .../Models/OrderItem.cs | 4 ++ .../Models/Receipt.cs | 8 ++++ .../Services/ReceiptCreator.cs | 28 ++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 homework-2-test-first-development.test/ReceiptCreatorTest.cs create mode 100644 homework-2-test-first-development/Exceptions/ProductNotFoundException.cs create mode 100644 homework-2-test-first-development/Models/Receipt.cs create mode 100644 homework-2-test-first-development/Services/ReceiptCreator.cs diff --git a/homework-2-test-first-development.test/ReceiptCreatorTest.cs b/homework-2-test-first-development.test/ReceiptCreatorTest.cs new file mode 100644 index 0000000..35b2ca5 --- /dev/null +++ b/homework-2-test-first-development.test/ReceiptCreatorTest.cs @@ -0,0 +1,38 @@ +using FluentAssertions; +using homework_2_test_first_development.Models; +using homework_2_test_first_development.Services; + +namespace homework_2_test_first_development.test; + +public class ReceiptCreatorTest +{ + public ReceiptCreatorTest() + { + } + + [Fact] + public void Should_Create_Receipt_Successfully() + { + // Given + var products = new Product[] + { + new Product { Barcode = "ITEM000001", Name = "Apple", Price = 1.0m}, + new Product { Barcode = "ITEM000002", Name = "Orange", Price = 2.0m}, + new Product { Barcode = "ITEM000003", Name = "Pear", Price = 3.0m}, + }; + + var groupedBarcodes = new OrderItem[] + { + new OrderItem { Barcode = "ITEM000001", Amount = 1}, + new OrderItem { Barcode = "ITEM000002", Amount = 2}, + new OrderItem { Barcode = "ITEM000003", Amount = 3}, + }; + + // When + var receipt = ReceiptCreator.CreateReceipt(groupedBarcodes, products); + + // Then + receipt.TotalPrice.Should().Be(1 * 1 + 2 * 2 + 3 * 3); + receipt.Products.Should().HaveCount(3); + } +} diff --git a/homework-2-test-first-development/Exceptions/ProductNotFoundException.cs b/homework-2-test-first-development/Exceptions/ProductNotFoundException.cs new file mode 100644 index 0000000..3ded54c --- /dev/null +++ b/homework-2-test-first-development/Exceptions/ProductNotFoundException.cs @@ -0,0 +1,5 @@ +namespace homework_2_test_first_development.Exceptions; + +public class ProductNotFoundException : Exception +{ +} diff --git a/homework-2-test-first-development/Models/OrderItem.cs b/homework-2-test-first-development/Models/OrderItem.cs index d6b1827..311fe87 100644 --- a/homework-2-test-first-development/Models/OrderItem.cs +++ b/homework-2-test-first-development/Models/OrderItem.cs @@ -5,4 +5,8 @@ public class OrderItem public string Barcode { get; set; } = string.Empty; public int Amount { get; set; } + + public string Name { get; set; } = string.Empty; + + public decimal TotalPrice { get; set; } } diff --git a/homework-2-test-first-development/Models/Receipt.cs b/homework-2-test-first-development/Models/Receipt.cs new file mode 100644 index 0000000..e6ab510 --- /dev/null +++ b/homework-2-test-first-development/Models/Receipt.cs @@ -0,0 +1,8 @@ +namespace homework_2_test_first_development.Models; + +public class Receipt +{ + public IEnumerable Products { get; set; } = new List(); + + public decimal TotalPrice { get; set; } +} diff --git a/homework-2-test-first-development/Services/ReceiptCreator.cs b/homework-2-test-first-development/Services/ReceiptCreator.cs new file mode 100644 index 0000000..3d2566f --- /dev/null +++ b/homework-2-test-first-development/Services/ReceiptCreator.cs @@ -0,0 +1,28 @@ +using homework_2_test_first_development.Exceptions; +using homework_2_test_first_development.Models; + +namespace homework_2_test_first_development.Services; + +public class ReceiptCreator +{ + public static Receipt CreateReceipt(IEnumerable groupedBarcodes, IEnumerable products) + { + var receipt = new Receipt { TotalPrice = 0m }; + + foreach (var item in groupedBarcodes) + { + var product = products.FirstOrDefault(p => p.Barcode == item.Barcode); + + if (product is null) + throw new ProductNotFoundException(); + + item.Name = product.Name; + item.TotalPrice = item.Amount * product.Price; + + receipt.Products = receipt.Products.Append(item); + receipt.TotalPrice += item.TotalPrice; + } + + return receipt; + } +} -- Gitee From 162546b35cdea5919818e489206ceb65a06b7d3b Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Fri, 11 Nov 2022 18:03:15 +0800 Subject: [PATCH 15/17] feat: add format receipt logic --- .../ReceiptFormatterTest.cs | 36 +++++++++++++++++++ .../Models/OrderItem.cs | 2 ++ .../Services/ReceiptFormatter.cs | 18 ++++++++++ 3 files changed, 56 insertions(+) create mode 100644 homework-2-test-first-development.test/ReceiptFormatterTest.cs create mode 100644 homework-2-test-first-development/Services/ReceiptFormatter.cs diff --git a/homework-2-test-first-development.test/ReceiptFormatterTest.cs b/homework-2-test-first-development.test/ReceiptFormatterTest.cs new file mode 100644 index 0000000..4694294 --- /dev/null +++ b/homework-2-test-first-development.test/ReceiptFormatterTest.cs @@ -0,0 +1,36 @@ +using FluentAssertions; +using homework_2_test_first_development.Models; +using homework_2_test_first_development.Services; + +namespace homework_2_test_first_development.test; + +public class ReceiptFormatterTest +{ + [Fact] + public void Should_Return_Formatted_Receipt_Content_Successfully() + { + // Given + Receipt receipt = new Receipt() + { + Products = new List + { + new OrderItem { Name = "Cola", Amount = 3, Price = 3, TotalPrice = 9}, + new OrderItem { Name = "Sprit", Amount = 2, Price = 3, TotalPrice = 6}, + }, + TotalPrice = 15 + }; + + // When + var receiptContent = ReceiptFormatter.FormatReceipt(receipt); + + // Then + + receiptContent.Should().Be(@"Receipt +------- +Name: Cola, Amount: 3, Price: 3.00, Total: 9.00 +Name: Sprit, Amount: 2, Price: 3.00, Total: 6.00 +------- +Total: 15.00"); + + } +} diff --git a/homework-2-test-first-development/Models/OrderItem.cs b/homework-2-test-first-development/Models/OrderItem.cs index 311fe87..474074d 100644 --- a/homework-2-test-first-development/Models/OrderItem.cs +++ b/homework-2-test-first-development/Models/OrderItem.cs @@ -9,4 +9,6 @@ public class OrderItem public string Name { get; set; } = string.Empty; public decimal TotalPrice { get; set; } + + public decimal Price { get; set; } } diff --git a/homework-2-test-first-development/Services/ReceiptFormatter.cs b/homework-2-test-first-development/Services/ReceiptFormatter.cs new file mode 100644 index 0000000..5ce7013 --- /dev/null +++ b/homework-2-test-first-development/Services/ReceiptFormatter.cs @@ -0,0 +1,18 @@ +using homework_2_test_first_development.Models; + +namespace homework_2_test_first_development.Services; + +public class ReceiptFormatter +{ + public static string FormatReceipt(Receipt receipt) + { + string[] header = { "Receipt", "-------" }; + + var body = receipt.Products.Select( + p => $"Name: {p.Name}, Amount: {p.Amount}, Price: {p.Price:F2}, Total: {p.TotalPrice:F2}"); + + string[] summary = { "-------", $"Total: {receipt.Products.Sum(p => p.TotalPrice):F2}" }; + + return string.Join(Environment.NewLine, header.Concat(body).Concat(summary)); + } +} -- Gitee From 1c598683db017f426242f0e5120bb13e3c3eb5eb Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Fri, 11 Nov 2022 18:18:58 +0800 Subject: [PATCH 16/17] feat: add create valid receipt logic --- .../ReceiptPrinterTest.cs | 32 ++++++++++++++++++- .../Services/ReceiptCreator.cs | 1 + .../Services/ReceiptPrinter.cs | 13 ++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/homework-2-test-first-development.test/ReceiptPrinterTest.cs b/homework-2-test-first-development.test/ReceiptPrinterTest.cs index ec59a47..a092df4 100644 --- a/homework-2-test-first-development.test/ReceiptPrinterTest.cs +++ b/homework-2-test-first-development.test/ReceiptPrinterTest.cs @@ -18,7 +18,7 @@ public class ReceiptPrinterTest public ReceiptPrinterTest() { _fixture.Customize(c => c.FromFactory( - (string name, decimal price, int id) => new Product { Name = name, Price = price, Barcode = $"{BarcodePrefix}{id:D6}"}) + (string name, decimal price, int id) => new Product { Name = name, Price = price, Barcode = $"{BarcodePrefix}{id:D6}" }) .OmitAutoProperties()); } @@ -53,4 +53,34 @@ public class ReceiptPrinterTest // Then receiptContent.Should().Contain("ERROR").And.Contain(invalidBarcode); } + + [Fact] + public void Should_Print_Receipt_Content_When_Everything_Is_OK() + { + // Given + var products = new Product[] + { + new Product { Barcode = "ITEM000001", Name = "Cola", Price = 3.0m}, + new Product { Barcode = "ITEM000003", Name = "Sprit", Price = 3.0m}, + }; + + string[] selectedBarcodes = + { + "ITEM000001", + "ITEM000001", + "ITEM000001", + "ITEM000003-2" + }; + + // When + var receiptContent = ReceiptPrinter.PrintReceipt(selectedBarcodes, products); + + // Then + receiptContent.Should().Be(@"Receipt +------- +Name: Cola, Amount: 3, Price: 3.00, Total: 9.00 +Name: Sprit, Amount: 2, Price: 3.00, Total: 6.00 +------- +Total: 15.00"); + } } diff --git a/homework-2-test-first-development/Services/ReceiptCreator.cs b/homework-2-test-first-development/Services/ReceiptCreator.cs index 3d2566f..a6c5ad9 100644 --- a/homework-2-test-first-development/Services/ReceiptCreator.cs +++ b/homework-2-test-first-development/Services/ReceiptCreator.cs @@ -18,6 +18,7 @@ public class ReceiptCreator item.Name = product.Name; item.TotalPrice = item.Amount * product.Price; + item.Price = product.Price; receipt.Products = receipt.Products.Append(item); receipt.TotalPrice += item.TotalPrice; diff --git a/homework-2-test-first-development/Services/ReceiptPrinter.cs b/homework-2-test-first-development/Services/ReceiptPrinter.cs index d8f3736..6cead2e 100644 --- a/homework-2-test-first-development/Services/ReceiptPrinter.cs +++ b/homework-2-test-first-development/Services/ReceiptPrinter.cs @@ -21,6 +21,10 @@ public class ReceiptPrinter { receiptContent = CreateErrorReceipt(validationResult); } + else + { + receiptContent = CreateValidReceipt(selectedBarcodes, products); + } return receiptContent; } @@ -39,4 +43,13 @@ public class ReceiptPrinter return string.Join(Environment.NewLine, header.Append(body)); } + + private static string CreateValidReceipt(IEnumerable selectedBarcodes, IEnumerable products) + { + var barcodesAndAmount = BarcodeProcessor.ParseBarcodes(selectedBarcodes); + var groupedBarcodes = BarcodeProcessor.GroupBarcodes(barcodesAndAmount); + var receipt = ReceiptCreator.CreateReceipt(groupedBarcodes, products); + + return ReceiptFormatter.FormatReceipt(receipt); + } } -- Gitee From 16977da8f2fcfa9637787950d505f5b812824da6 Mon Sep 17 00:00:00 2001 From: "pei.gao" Date: Fri, 18 Nov 2022 22:27:48 +0800 Subject: [PATCH 17/17] feat: restrict the access level --- .../BarcodeValidatorTest.cs | 15 +++++++-------- homework-2-test-first-development/AssemblyInfo.cs | 1 + .../Enums/ErrorType.cs | 2 +- .../Models/OrderItem.cs | 2 +- .../Models/Receipt.cs | 2 +- .../Services/BarcodeProcessor.cs | 2 +- .../Services/ReceiptCreator.cs | 2 +- .../Services/ReceiptFormatter.cs | 2 +- .../Services/ReceiptPrinter.cs | 9 ++------- .../Validators/BarcodeValidator.cs | 4 ++-- .../Validators/ValidationResult.cs | 2 +- 11 files changed, 19 insertions(+), 24 deletions(-) create mode 100644 homework-2-test-first-development/AssemblyInfo.cs diff --git a/homework-2-test-first-development.test/BarcodeValidatorTest.cs b/homework-2-test-first-development.test/BarcodeValidatorTest.cs index f00e53b..a900e19 100644 --- a/homework-2-test-first-development.test/BarcodeValidatorTest.cs +++ b/homework-2-test-first-development.test/BarcodeValidatorTest.cs @@ -8,7 +8,6 @@ namespace homework_2_test_first_development.test; public class BarcodeValidatorTest { - private readonly BarcodeValidator _barcodeValidator = new(); private readonly Fixture _fixture = new(); private static string BarcodePrefix = "ITEM"; @@ -29,7 +28,7 @@ public class BarcodeValidatorTest var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When - var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + var validationResult = BarcodeValidator.ValidateBarcodes(selectedBarcodes, products); // Then validationResult.Should().BeOfType().Which @@ -46,7 +45,7 @@ public class BarcodeValidatorTest var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When - var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + var validationResult = BarcodeValidator.ValidateBarcodes(selectedBarcodes, products); // Then validationResult.Should().BeOfType().Which @@ -63,7 +62,7 @@ public class BarcodeValidatorTest var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When - var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + var validationResult = BarcodeValidator.ValidateBarcodes(selectedBarcodes, products); // Then validationResult.Should().BeOfType().Which @@ -80,7 +79,7 @@ public class BarcodeValidatorTest var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When - var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + var validationResult = BarcodeValidator.ValidateBarcodes(selectedBarcodes, products); // Then validationResult.Should().BeOfType().Which @@ -97,7 +96,7 @@ public class BarcodeValidatorTest var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When - var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + var validationResult = BarcodeValidator.ValidateBarcodes(selectedBarcodes, products); // Then validationResult.Should().BeOfType().Which @@ -114,7 +113,7 @@ public class BarcodeValidatorTest var selectedBarcodes = products.Select(p => p.Barcode).Append(invalidBarcode); // When - var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + var validationResult = BarcodeValidator.ValidateBarcodes(selectedBarcodes, products); // Then validationResult.Should().BeOfType().Which @@ -129,7 +128,7 @@ public class BarcodeValidatorTest var selectedBarcodes = products.Select(p => p.Barcode); // When - var validationResult = _barcodeValidator.ValidateBarcodes(selectedBarcodes, products); + var validationResult = BarcodeValidator.ValidateBarcodes(selectedBarcodes, products); // Then validationResult.Should().BeOfType().Which diff --git a/homework-2-test-first-development/AssemblyInfo.cs b/homework-2-test-first-development/AssemblyInfo.cs new file mode 100644 index 0000000..98067a4 --- /dev/null +++ b/homework-2-test-first-development/AssemblyInfo.cs @@ -0,0 +1 @@ +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("homework-2-test-first-development.test")] \ No newline at end of file diff --git a/homework-2-test-first-development/Enums/ErrorType.cs b/homework-2-test-first-development/Enums/ErrorType.cs index 9645383..a50ff5a 100644 --- a/homework-2-test-first-development/Enums/ErrorType.cs +++ b/homework-2-test-first-development/Enums/ErrorType.cs @@ -1,6 +1,6 @@ namespace homework_2_test_first_development.Enums; -public enum ErrorType +internal enum ErrorType { Valid, InvalidBarcode, diff --git a/homework-2-test-first-development/Models/OrderItem.cs b/homework-2-test-first-development/Models/OrderItem.cs index 474074d..46e86c1 100644 --- a/homework-2-test-first-development/Models/OrderItem.cs +++ b/homework-2-test-first-development/Models/OrderItem.cs @@ -1,6 +1,6 @@ namespace homework_2_test_first_development.Models; -public class OrderItem +internal class OrderItem { public string Barcode { get; set; } = string.Empty; diff --git a/homework-2-test-first-development/Models/Receipt.cs b/homework-2-test-first-development/Models/Receipt.cs index e6ab510..6bc0252 100644 --- a/homework-2-test-first-development/Models/Receipt.cs +++ b/homework-2-test-first-development/Models/Receipt.cs @@ -1,6 +1,6 @@ namespace homework_2_test_first_development.Models; -public class Receipt +internal class Receipt { public IEnumerable Products { get; set; } = new List(); diff --git a/homework-2-test-first-development/Services/BarcodeProcessor.cs b/homework-2-test-first-development/Services/BarcodeProcessor.cs index 2d1b2b7..29b0f9c 100644 --- a/homework-2-test-first-development/Services/BarcodeProcessor.cs +++ b/homework-2-test-first-development/Services/BarcodeProcessor.cs @@ -2,7 +2,7 @@ using homework_2_test_first_development.Models; namespace homework_2_test_first_development.Services; -public class BarcodeProcessor +internal static class BarcodeProcessor { public static char Seperator => '-'; diff --git a/homework-2-test-first-development/Services/ReceiptCreator.cs b/homework-2-test-first-development/Services/ReceiptCreator.cs index a6c5ad9..39cea8b 100644 --- a/homework-2-test-first-development/Services/ReceiptCreator.cs +++ b/homework-2-test-first-development/Services/ReceiptCreator.cs @@ -3,7 +3,7 @@ using homework_2_test_first_development.Models; namespace homework_2_test_first_development.Services; -public class ReceiptCreator +internal static class ReceiptCreator { public static Receipt CreateReceipt(IEnumerable groupedBarcodes, IEnumerable products) { diff --git a/homework-2-test-first-development/Services/ReceiptFormatter.cs b/homework-2-test-first-development/Services/ReceiptFormatter.cs index 5ce7013..12a929b 100644 --- a/homework-2-test-first-development/Services/ReceiptFormatter.cs +++ b/homework-2-test-first-development/Services/ReceiptFormatter.cs @@ -2,7 +2,7 @@ using homework_2_test_first_development.Models; namespace homework_2_test_first_development.Services; -public class ReceiptFormatter +internal static class ReceiptFormatter { public static string FormatReceipt(Receipt receipt) { diff --git a/homework-2-test-first-development/Services/ReceiptPrinter.cs b/homework-2-test-first-development/Services/ReceiptPrinter.cs index 6cead2e..8804ca9 100644 --- a/homework-2-test-first-development/Services/ReceiptPrinter.cs +++ b/homework-2-test-first-development/Services/ReceiptPrinter.cs @@ -1,21 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using homework_2_test_first_development.Enums; using homework_2_test_first_development.Models; using homework_2_test_first_development.Validators; namespace homework_2_test_first_development.Services; -public class ReceiptPrinter +public static class ReceiptPrinter { public static string PrintReceipt(IEnumerable selectedBarcodes, IEnumerable products) { string receiptContent = string.Empty; - BarcodeValidator validator = new BarcodeValidator(); - var validationResult = validator.ValidateBarcodes(selectedBarcodes, products); + var validationResult = BarcodeValidator.ValidateBarcodes(selectedBarcodes, products); if (validationResult.Error != ErrorType.Valid) { diff --git a/homework-2-test-first-development/Validators/BarcodeValidator.cs b/homework-2-test-first-development/Validators/BarcodeValidator.cs index 77ea2ad..f166e5b 100644 --- a/homework-2-test-first-development/Validators/BarcodeValidator.cs +++ b/homework-2-test-first-development/Validators/BarcodeValidator.cs @@ -3,12 +3,12 @@ using homework_2_test_first_development.Models; namespace homework_2_test_first_development.Validators; -public class BarcodeValidator +internal static class BarcodeValidator { public static char Seperator => '-'; public static int Maximum => 99; - public ValidationResult ValidateBarcodes(IEnumerable selectedBarcodes, IEnumerable products) + public static ValidationResult ValidateBarcodes(IEnumerable selectedBarcodes, IEnumerable products) { foreach (var item in selectedBarcodes) { diff --git a/homework-2-test-first-development/Validators/ValidationResult.cs b/homework-2-test-first-development/Validators/ValidationResult.cs index f980c4f..91666bf 100644 --- a/homework-2-test-first-development/Validators/ValidationResult.cs +++ b/homework-2-test-first-development/Validators/ValidationResult.cs @@ -2,7 +2,7 @@ using homework_2_test_first_development.Enums; namespace homework_2_test_first_development.Validators; -public record struct ValidationResult +internal record struct ValidationResult { public string? Barcode { get; init; } -- Gitee