From 96c1b521105309c7505a6c4ec51d46488b7ac35e Mon Sep 17 00:00:00 2001 From: Geir Okkenhaug Jerstad Date: Mon, 2 Dec 2024 12:39:21 +0100 Subject: [PATCH] refactoring --- .../StaticogIkkestatic/Places.cs | 30 +++++++++++++++++++ .../StaticogIkkestatic/Program.cs | 23 +++++++------- 2 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 Emne 3/StaticogIkkestatic/StaticogIkkestatic/Places.cs diff --git a/Emne 3/StaticogIkkestatic/StaticogIkkestatic/Places.cs b/Emne 3/StaticogIkkestatic/StaticogIkkestatic/Places.cs new file mode 100644 index 0000000..4c91dc6 --- /dev/null +++ b/Emne 3/StaticogIkkestatic/StaticogIkkestatic/Places.cs @@ -0,0 +1,30 @@ +namespace StaticogIkkestatic; + +public class Places +{ + public string PlaceName; + public string Municipality; + public string Region; + + public void ShowPlace() + { + var labelWidth = 8; + ShowSeperatorRow(); + ShowFielAndValue("Navn", labelWidth,PlaceName); + ShowFielAndValue("Kommune", labelWidth,Municipality); + ShowFielAndValue("Fylke", labelWidth,Region); + ShowSeperatorRow(); + } + + static void ShowFielAndValue(string label, int labelWidth,string fieldValue) + { + labelWidth = label.Length; + Console.WriteLine(" " + label +":" + String.Empty.PadLeft(labelWidth, ' ') + fieldValue); + } + + static void ShowSeperatorRow(int labelWidth = 8) + { + labelWidth += 14; + Console.WriteLine(String.Empty.PadLeft(labelWidth, '*')); + } +} \ No newline at end of file diff --git a/Emne 3/StaticogIkkestatic/StaticogIkkestatic/Program.cs b/Emne 3/StaticogIkkestatic/StaticogIkkestatic/Program.cs index 0a60e1d..07978ac 100644 --- a/Emne 3/StaticogIkkestatic/StaticogIkkestatic/Program.cs +++ b/Emne 3/StaticogIkkestatic/StaticogIkkestatic/Program.cs @@ -3,19 +3,18 @@ class Program { static void Main(string[] args) - { - ShowSeperatorRow(); - var label = "Navn"; - var fieldValue = "Stavern"; - Console.WriteLine("Navn: Stavern"); - Console.WriteLine("Kommune: Larvik"); - Console.WriteLine("Fylke: Vestfold"); - ShowSeperatorRow(); - } - static void ShowSeperatorRow() { - Console.WriteLine("****************************"); + var place = new Places(); + place.PlaceName = "Stavern"; + place.Municipality = "Larvik"; + place.Region = "Vestfold"; + place.ShowPlace(); + var place2 = new Places(); + place2.PlaceName = "Rjukan"; + place2.Municipality = "Tinn"; + place2.Region = "Telemark"; + place2.ShowPlace(); } + } } -