From 89d987f6c6923b458217017300789ea956114972 Mon Sep 17 00:00:00 2001 From: AJ Date: Fri, 9 May 2025 07:47:29 -0400 Subject: [PATCH] Refactor Main method to separate responsibilities and improve readability --- EtchBendLines/AppConfig.cs | 16 ++++++ EtchBendLines/EtchBendLines.csproj | 1 + EtchBendLines/Program.cs | 80 +++++++++++++----------------- 3 files changed, 52 insertions(+), 45 deletions(-) create mode 100644 EtchBendLines/AppConfig.cs diff --git a/EtchBendLines/AppConfig.cs b/EtchBendLines/AppConfig.cs new file mode 100644 index 0000000..15be292 --- /dev/null +++ b/EtchBendLines/AppConfig.cs @@ -0,0 +1,16 @@ +using System; +using System.Configuration; + +namespace EtchBendLines +{ + public static class AppConfig + { + public static double GetDouble(string key) + { + if (!double.TryParse(ConfigurationManager.AppSettings[key], out var value)) + throw new Exception($"Failed to convert AppSetting[\"{key}\"] to double."); + + return value; + } + } +} diff --git a/EtchBendLines/EtchBendLines.csproj b/EtchBendLines/EtchBendLines.csproj index e5291a3..d85932b 100644 --- a/EtchBendLines/EtchBendLines.csproj +++ b/EtchBendLines/EtchBendLines.csproj @@ -47,6 +47,7 @@ + diff --git a/EtchBendLines/Program.cs b/EtchBendLines/Program.cs index 221263e..a009cb6 100644 --- a/EtchBendLines/Program.cs +++ b/EtchBendLines/Program.cs @@ -12,66 +12,56 @@ namespace EtchBendLines { try { - var etcher = new Etcher(); - etcher.EtchLength = GetAppSettingAsDouble("EtchLength"); - etcher.MaxBendRadius = GetAppSettingAsDouble("MaxBendRadius"); - - var paths = new List(args); - - if (paths.Count == 0) - { - paths.Add(AppDomain.CurrentDomain.BaseDirectory); - } - - var files = new List(); - - foreach (var path in paths) - { - if (File.Exists(path)) - { - files.Add(path); - } - else if (Directory.Exists(path)) - { - files.AddRange(Directory.GetFiles(path, "*.dxf", SearchOption.AllDirectories)); - } - } - - if (files.Count == 0) - { - Console.WriteLine($"No DXF files founds. Place DXF files in \"{AppDomain.CurrentDomain.BaseDirectory}\" and run this program again."); - } - else - { - foreach (var file in files) - { - etcher.AddEtchLines(file); - Console.WriteLine(); - } - } + Run(args); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine($"An error occured: {ex.Message}"); + Console.WriteLine($"An error occurred: {ex.Message}"); Console.ResetColor(); } PressAnyKeyToExit(); } - static double GetAppSettingAsDouble(string name) + static void Run(string[] args) { - double v; + var etchLength = AppConfig.GetDouble("EtchLength"); + var maxRadius = AppConfig.GetDouble("MaxBendRadius"); - try + var etcher = new Etcher { - return double.Parse(ConfigurationManager.AppSettings[name]); - } - catch + MaxBendRadius = maxRadius + }; + + var files = GetDxfFiles(args); + if (files.Count == 0) { - throw new Exception($"Failed to convert the value of AppSetting[\"{name}\"] to double"); + Console.WriteLine($"No DXF files found. Place DXF files in \"{AppDomain.CurrentDomain.BaseDirectory}\" and run this program again."); + return; } + + foreach (var file in files) + { + etcher.AddEtchLines(file, etchLength); + Console.WriteLine(); + } + } + + static List GetDxfFiles(string[] args) + { + var paths = args.Length > 0 ? args.ToList() : new List { AppDomain.CurrentDomain.BaseDirectory }; + var files = new List(); + + foreach (var path in paths) + { + if (File.Exists(path)) + files.Add(path); + else if (Directory.Exists(path)) + files.AddRange(Directory.GetFiles(path, "*.dxf", SearchOption.AllDirectories)); + } + + return files; } static void PressAnyKeyToExit()