diff --git a/01-3 Personalausweis/01-3 Personalausweis.csproj b/01-3 Personalausweis/01-3 Personalausweis.csproj new file mode 100644 index 0000000000000000000000000000000000000000..b79dd76a8fc8fffa02967f8166fccd5942310e94 --- /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 0000000000000000000000000000000000000000..a80eae17be786d23c3199601c129abc0c4da1fa0 --- /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 0000000000000000000000000000000000000000..c40ff27042ae9aa906861925f39388f01d72f165 --- /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 0000000000000000000000000000000000000000..7e554cef3d34021c59ebc0f7fc500ef52879b540 --- /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 ac7fcd7147780454be0d5512246c89392dd5a2e1..4ae2063421b8c048d814bdbe07ca83f1c30d51c4 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 455a67ff2d90e8a97874969fce4eaf09f40c7350..e2cc8e630fbccbdb238db6afb9f4a6c0727c24c8 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 598df9ff6768284eec91738e9cf754b82f921ccb..23162f5f24c40590bcf3c4e184c42fa9a2dbdd1e 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