From 5fa1af2cce5e776604cc319a2bf39e02c270497f Mon Sep 17 00:00:00 2001 From: wienkop <uwe.wienkop@th-nuernberg.de> Date: Tue, 16 Mar 2021 11:37:36 +0100 Subject: [PATCH] 2021-03-16 Di --- .../01-3 Personalausweis.csproj | 9 ++++ 01-3 Personalausweis/Program.cs | 42 +++++++++++++++++++ .../01-3 TestPersonalausweis.csproj | 21 ++++++++++ 01-3 TestPersonalausweis/UnitTest1.cs | 27 ++++++++++++ 01-V IntroIndexer/Personenliste.cs | 37 ++++++++++++++++ 01-V IntroIndexer/Program.cs | 18 ++++++++ Prog2WienkopSS2021.sln | 14 ++++++- 7 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 01-3 Personalausweis/01-3 Personalausweis.csproj create mode 100644 01-3 Personalausweis/Program.cs create mode 100644 01-3 TestPersonalausweis/01-3 TestPersonalausweis.csproj create mode 100644 01-3 TestPersonalausweis/UnitTest1.cs diff --git a/01-3 Personalausweis/01-3 Personalausweis.csproj b/01-3 Personalausweis/01-3 Personalausweis.csproj new file mode 100644 index 0000000..b79dd76 --- /dev/null +++ b/01-3 Personalausweis/01-3 Personalausweis.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_01_3_Personalausweis</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/01-3 Personalausweis/Program.cs b/01-3 Personalausweis/Program.cs new file mode 100644 index 0000000..a80eae1 --- /dev/null +++ b/01-3 Personalausweis/Program.cs @@ -0,0 +1,42 @@ +using System; + +namespace _01_3_Personalausweis +{ + public class Personalausweis + { + public string Name { get; private set; } + int id=1000; + + static int naechste_ID = 1000; + + public Personalausweis(string Name) + { + this.Name = Name; + id = NaechsteID++; + } + + public static int NaechsteID + { + get => naechste_ID; + set + { + if (value < naechste_ID) + throw new ArgumentOutOfRangeException("Neue ID darf nicht kleiner dem aktuellen Stand sein"); + naechste_ID = value; + } + } + public override string ToString() => $"Name: {Name}, ID: {id}"; + } + class Program + { + static void Main(string[] args) + { + Personalausweis.NaechsteID = 2000; + + Personalausweis anton = new Personalausweis("Anton"); + Console.WriteLine(anton.Name); + Console.WriteLine(anton); + Console.WriteLine(Personalausweis.NaechsteID); + } + } +} diff --git a/01-3 TestPersonalausweis/01-3 TestPersonalausweis.csproj b/01-3 TestPersonalausweis/01-3 TestPersonalausweis.csproj new file mode 100644 index 0000000..c40ff27 --- /dev/null +++ b/01-3 TestPersonalausweis/01-3 TestPersonalausweis.csproj @@ -0,0 +1,21 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_01_3_TestPersonalausweis</RootNamespace> + + <IsPackable>false</IsPackable> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> + <PackageReference Include="xunit" Version="2.4.0" /> + <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> + <PackageReference Include="coverlet.collector" Version="1.2.0" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\01-3 Personalausweis\01-3 Personalausweis.csproj" /> + </ItemGroup> + +</Project> diff --git a/01-3 TestPersonalausweis/UnitTest1.cs b/01-3 TestPersonalausweis/UnitTest1.cs new file mode 100644 index 0000000..7e554ce --- /dev/null +++ b/01-3 TestPersonalausweis/UnitTest1.cs @@ -0,0 +1,27 @@ +using System; +using Xunit; +using _01_3_Personalausweis; + +namespace _01_3_TestPersonalausweis +{ + public class UnitTest1 + { + [Fact] + public void TestPerso() + { + Personalausweis.NaechsteID = 2000; + Personalausweis perso = new Personalausweis("Anton"); + + string result = "Name: Anton, ID: 2000"; + Assert.Equal(result, perso.ToString()); + } + + [Fact] + public void TestID() + { + Personalausweis perso = new Personalausweis("Anton"); + Assert.Equal(1001, Personalausweis.NaechsteID); + + } + } +} diff --git a/01-V IntroIndexer/Personenliste.cs b/01-V IntroIndexer/Personenliste.cs index ac7fcd7..4ae2063 100644 --- a/01-V IntroIndexer/Personenliste.cs +++ b/01-V IntroIndexer/Personenliste.cs @@ -6,6 +6,43 @@ namespace _01_V_IntroIndexer { class Personenliste { + Person[] personen; + int capacity; + int count; + public Personenliste(int Capacity) + { + capacity = Capacity; + count = 0; + personen = new Person[capacity]; + } + public void Add(Person p) + { + if (count < capacity) + personen[count++] = p; + else + throw new IndexOutOfRangeException("Feld ist zu klein"); + } + public Person this[int index] + // this = Name für einen Indexer + // index-Parameter in eckigen Klammern + { + get { return personen[index]; } + set + { + if (index < 0 || index >= capacity) + throw new IndexOutOfRangeException("Der Index lag außerhalb des Personenfeldes"); + personen[index] = value; + } + } + + public IEnumerator<Person> GetEnumerator() + { + for (int i = 0; i < capacity; i++) + { + if (personen[i] != null) + yield return personen[i]; + } + } } } diff --git a/01-V IntroIndexer/Program.cs b/01-V IntroIndexer/Program.cs index 455a67f..e2cc8e6 100644 --- a/01-V IntroIndexer/Program.cs +++ b/01-V IntroIndexer/Program.cs @@ -15,6 +15,24 @@ namespace _01_V_IntroIndexer Person.Ausgabe(anton); Console.WriteLine(anton); Console.WriteLine(anton.ToString()); + + //int[] f = new int[30]; + //f[1] = 10; + //int x = f[2]; + + Personenliste freunde = new Personenliste(10); + freunde.Add(anton); + freunde.Add(new Person("Schneider", "Dieter")); + Console.WriteLine("-----------------"); + Console.WriteLine(freunde[0]); + Console.WriteLine(freunde[1]); + + freunde[5] = berta; + Console.WriteLine("-----------------"); + foreach (Person p in freunde) + { + Console.WriteLine(p); + } } } } diff --git a/Prog2WienkopSS2021.sln b/Prog2WienkopSS2021.sln index 598df9f..23162f5 100644 --- a/Prog2WienkopSS2021.sln +++ b/Prog2WienkopSS2021.sln @@ -9,7 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "01-1T WdhlgKlassen", "01-T EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "01-2 IntroIndexer", "01-V IntroIndexer\01-2 IntroIndexer.csproj", "{6781DE0D-C861-40AC-9C33-5EA5D917425D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject1", "TestProject1\TestProject1.csproj", "{24B954CA-BD5E-42BF-9396-E973BCC8A8B5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestProject1", "TestProject1\TestProject1.csproj", "{24B954CA-BD5E-42BF-9396-E973BCC8A8B5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "01-3 Personalausweis", "01-3 Personalausweis\01-3 Personalausweis.csproj", "{99D1542B-82B4-4848-967A-C5B7BB1ECD29}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "01-3 TestPersonalausweis", "01-3 TestPersonalausweis\01-3 TestPersonalausweis.csproj", "{03421B8B-5B64-4997-9BA7-F558D50799EB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +37,14 @@ Global {24B954CA-BD5E-42BF-9396-E973BCC8A8B5}.Debug|Any CPU.Build.0 = Debug|Any CPU {24B954CA-BD5E-42BF-9396-E973BCC8A8B5}.Release|Any CPU.ActiveCfg = Release|Any CPU {24B954CA-BD5E-42BF-9396-E973BCC8A8B5}.Release|Any CPU.Build.0 = Release|Any CPU + {99D1542B-82B4-4848-967A-C5B7BB1ECD29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {99D1542B-82B4-4848-967A-C5B7BB1ECD29}.Debug|Any CPU.Build.0 = Debug|Any CPU + {99D1542B-82B4-4848-967A-C5B7BB1ECD29}.Release|Any CPU.ActiveCfg = Release|Any CPU + {99D1542B-82B4-4848-967A-C5B7BB1ECD29}.Release|Any CPU.Build.0 = Release|Any CPU + {03421B8B-5B64-4997-9BA7-F558D50799EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03421B8B-5B64-4997-9BA7-F558D50799EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03421B8B-5B64-4997-9BA7-F558D50799EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03421B8B-5B64-4997-9BA7-F558D50799EB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- GitLab