From a537e0b8999a283e21d99b144ab9e802fe8a2b8b Mon Sep 17 00:00:00 2001
From: wienkop <uwe.wienkop@th-nuernberg.de>
Date: Tue, 4 May 2021 07:28:54 +0200
Subject: [PATCH] 2021-05-03 Interfaces

---
 05-2 IntroVererbungKfz/KfzCoolStyle.cs        |  8 ++-
 05-2 IntroVererbungKfz/Program.cs             |  1 +
 .../07-1 Intro Interfaces.csproj              |  9 +++
 07-1 Intro Interfaces/Classes.cs              | 65 +++++++++++++++++++
 07-1 Intro Interfaces/Program.cs              | 43 ++++++++++++
 07-UbgWarenwirtschaft/Classes.cs              | 32 ++++++++-
 07-UbgWarenwirtschaft/Program.cs              | 29 ++++++++-
 Prog2WienkopSS2021.sln                        |  8 ++-
 8 files changed, 189 insertions(+), 6 deletions(-)
 create mode 100644 07-1 Intro Interfaces/07-1 Intro Interfaces.csproj
 create mode 100644 07-1 Intro Interfaces/Classes.cs
 create mode 100644 07-1 Intro Interfaces/Program.cs

diff --git a/05-2 IntroVererbungKfz/KfzCoolStyle.cs b/05-2 IntroVererbungKfz/KfzCoolStyle.cs
index 5ed9cc5..2fa0ff6 100644
--- a/05-2 IntroVererbungKfz/KfzCoolStyle.cs	
+++ b/05-2 IntroVererbungKfz/KfzCoolStyle.cs	
@@ -9,7 +9,7 @@ namespace _05_2_IntroVererbungKfz
     {
         // public string kennzeichen;  -- Nicht public machen!
 
-        /* protected */ string kennzeichen;
+        /* protected */  string kennzeichen;
             // protected ~ öffentlich für abgeleitete Klassen
             //           ~ private für alle anderen
 
@@ -38,7 +38,7 @@ namespace _05_2_IntroVererbungKfz
         int hubraum=1599;
         int co2;
         public Pkw(int Hubraum, int CO2, string Kennzeichen)
-            : base(Kennzeichen.ToLower())
+            : base(Kennzeichen)
         {
             hubraum = Hubraum;
             co2 = CO2;
@@ -49,6 +49,10 @@ namespace _05_2_IntroVererbungKfz
         }
         public double CO2Wert() => co2;
 
+        public override string ToString()
+        {
+            return base.ToString();
+        }
     }
 
     class Motorrad : Kfz
diff --git a/05-2 IntroVererbungKfz/Program.cs b/05-2 IntroVererbungKfz/Program.cs
index dd46df8..d4f8ca0 100644
--- a/05-2 IntroVererbungKfz/Program.cs	
+++ b/05-2 IntroVererbungKfz/Program.cs	
@@ -58,6 +58,7 @@ namespace _05_2_IntroVererbungKfz
             kfz[1] = new Pkw(1599, 2, "ER-XY 456");
             kfz[2] = new Motorrad("FÜ-CD 789",250);
             kfz[3] = new Tieflader("AN-345", 20000, 4500, 12000);
+
             // kfz[4] = new Kfz("XX-123");
             // Keine Objekte einer abstrakten Klasse anlegbar!
 
diff --git a/07-1 Intro Interfaces/07-1 Intro Interfaces.csproj b/07-1 Intro Interfaces/07-1 Intro Interfaces.csproj
new file mode 100644
index 0000000..d3fed5a
--- /dev/null
+++ b/07-1 Intro Interfaces/07-1 Intro Interfaces.csproj	
@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <RootNamespace>_07_1_Intro_Interfaces</RootNamespace>
+  </PropertyGroup>
+
+</Project>
diff --git a/07-1 Intro Interfaces/Classes.cs b/07-1 Intro Interfaces/Classes.cs
new file mode 100644
index 0000000..faa76b7
--- /dev/null
+++ b/07-1 Intro Interfaces/Classes.cs	
@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace _07_1_Intro_Interfaces
+{
+    interface IBesteuerbar
+    {
+        /* public abstract */ double Steuern();
+    }
+    // Interface ist eine abstrakte Klasse
+    // alle Methoden sind public und abstract
+
+    interface IVerkaufbar
+    {
+        void Verkaufen();
+    }
+
+    abstract class Kfz : IBesteuerbar, IVerkaufbar
+    {
+        public abstract double Steuern();
+        public abstract void Verkaufen();
+        public abstract void Fahren();
+    }
+
+    class Pkw : Kfz
+    {
+        public override void Fahren()
+        {
+            Console.WriteLine("Pkw fährt");
+        }
+
+        public override double Steuern()
+        {
+            return 100;
+        }
+
+        public override void Verkaufen()
+        {
+            Console.WriteLine("Pkw wird verkauft");
+        }
+    }
+    //class Motorrad : Kfz
+    //{
+
+    //}
+    class Pflanzen : IVerkaufbar
+    {
+        public void Verkaufen()
+        {
+            Console.WriteLine("Pflanze wird verkauft");
+        }
+    }
+
+    class Immobilien : IBesteuerbar, IVerkaufbar
+    {
+        // Steuern() und Verkaufen() müssen direkt implementiert werden, da ansonsten
+        // nicht alle abstrakten Methoden implementiert sind
+        public  double Steuern() => 999;
+        public void Verkaufen() 
+        { 
+            Console.WriteLine("Immobilie wird verkauft"); 
+        }
+    }
+}
diff --git a/07-1 Intro Interfaces/Program.cs b/07-1 Intro Interfaces/Program.cs
new file mode 100644
index 0000000..941e7af
--- /dev/null
+++ b/07-1 Intro Interfaces/Program.cs	
@@ -0,0 +1,43 @@
+using System;
+
+namespace _07_1_Intro_Interfaces
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            //IBesteuerbar k=null;
+            //k.Steuern();
+
+            Pkw p = new Pkw();
+            // p = new Motorrad();  geht nicht, da ein Motorrad kein Pkw ist (wohl aber Kfz)
+
+            Kfz kfz = new Pkw();
+            //kfz = new Motorrad();
+            kfz.Fahren();
+
+            IVerkaufbar[] verkObj = new IVerkaufbar[3];
+            verkObj[0] = new Pkw();
+            verkObj[1] = new Pflanzen();
+            verkObj[2] = new Immobilien();
+
+            foreach (var item in verkObj)
+                item.Verkaufen();
+
+            IBesteuerbar[] bestObj = new IBesteuerbar[2];
+            int ind = 0;
+            for (int i = 0; i < verkObj.Length; i++)
+            {
+                if (verkObj[i] is IBesteuerbar item)
+                    //bestObj[ind++] = item;
+                    Console.WriteLine(item.Steuern());
+            }
+
+            foreach (var item in bestObj)
+            {
+                Console.WriteLine(  item.Steuern());
+            }
+
+        }
+    }
+}
diff --git a/07-UbgWarenwirtschaft/Classes.cs b/07-UbgWarenwirtschaft/Classes.cs
index f9563b0..441512a 100644
--- a/07-UbgWarenwirtschaft/Classes.cs
+++ b/07-UbgWarenwirtschaft/Classes.cs
@@ -19,7 +19,37 @@ namespace _07_UbgWarenwirtschaft
     // und schreiben Sie eine Methode, die z.B. nach der Pflanze über den Namen sucht und dieses Objekt zurückliefert
     // Rufen Sie dann für die Pflanze diese besondere Methode auf.
 
-    class Classes
+    abstract class Produkt
     {
+        int stueckzahl;
+        double preis;
+        // protected string name;
+        public string Name { get; protected set; }
+        // protected: Die Methoden dieser Klasse und die Methoden der abgeleiteten Klassen
+        // dürfen den set aufrufen.
+        public Produkt(int stueckzahl, double preis, string name)
+        {
+            this.stueckzahl = stueckzahl;
+            this.preis = preis;
+            this.Name = name;
+        }
+        public double Wert() => preis * stueckzahl;
+    }
+    abstract class Gartenprodukte : Produkt
+    {
+        public Gartenprodukte(int stückzahl, double preis, string name)
+            : base(stückzahl, preis, name) { }
+    }
+    class Pflanzen : Gartenprodukte
+    {
+        public enum Pflanzentyp { nutz, zier };
+        Pflanzentyp typ;
+        public Pflanzen(int stückzahl, double preis, string name, Pflanzentyp typ)
+            : base(stückzahl, preis, name)
+        {
+            this.typ = typ;
+        }
+        public void Giessen() { }
+
     }
 }
diff --git a/07-UbgWarenwirtschaft/Program.cs b/07-UbgWarenwirtschaft/Program.cs
index 657ac3c..e975705 100644
--- a/07-UbgWarenwirtschaft/Program.cs
+++ b/07-UbgWarenwirtschaft/Program.cs
@@ -6,7 +6,32 @@ namespace _07_UbgWarenwirtschaft
     {
         static void Main(string[] args)
         {
-            Console.WriteLine("Hello World!");
-        }
+            Produkt[] produkte = new Produkt[3];
+            produkte[0] = new Pflanzen(10, 0.35, "Geranien", Pflanzen.Pflanzentyp.zier);
+            produkte[1] = new Pflanzen(10, 0.55, "Salat", Pflanzen.Pflanzentyp.nutz);
+            foreach (var item in produkte)
+            {
+                Console.WriteLine(item.Wert());
+                if (item.Name == "Geranien")
+                {
+                    // item.Giessen(); geht nicht, da die Basisklasse Produkt keine Methode Gießen enthält!!!
+
+                    //if (item is Pflanzen)
+                    //{
+                    //   Pflanzen p = (Pflanzen)item;
+                    //          ( ) liefert Fehler, wenn Konvertierung nicht möglich ist
+                    //   p.Giessen();
+                    //}
+
+                    //Pflanzen p2 = item as Pflanzen;
+                    //          as liefert "null", wenn Konvertierung nicht möglich ist
+                    //p2.Giessen();
+
+                    if (item is Pflanzen p3)
+                        p3.Giessen();
+
+                    (item as Pflanzen)?.Giessen();
+                    // Konvertiere item zu Pflanze; wenn erfolgreich (d.h. != null) wird Giessen aufgerufen
+                }
     }
 }
diff --git a/Prog2WienkopSS2021.sln b/Prog2WienkopSS2021.sln
index abc873a..715a687 100644
--- a/Prog2WienkopSS2021.sln
+++ b/Prog2WienkopSS2021.sln
@@ -45,7 +45,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "06-UbgVererbung-Di", "06-Ub
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "06-Demo WPF-Anwendung", "06-Demo WPF-Anwendung\06-Demo WPF-Anwendung.csproj", "{E3C0CDD8-B068-4148-81DC-39F3C40B9805}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "07-UbgWarenwirtschaft", "07-UbgWarenwirtschaft\07-UbgWarenwirtschaft.csproj", "{46EE1C7D-DCEF-4291-8531-0000BD977FFB}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "07-UbgWarenwirtschaft", "07-UbgWarenwirtschaft\07-UbgWarenwirtschaft.csproj", "{46EE1C7D-DCEF-4291-8531-0000BD977FFB}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "07-1 Intro Interfaces", "07-1 Intro Interfaces\07-1 Intro Interfaces.csproj", "{D60AFAC5-6DB1-4F7A-AC86-B1C0D304F83F}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -141,6 +143,10 @@ Global
 		{46EE1C7D-DCEF-4291-8531-0000BD977FFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{46EE1C7D-DCEF-4291-8531-0000BD977FFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{46EE1C7D-DCEF-4291-8531-0000BD977FFB}.Release|Any CPU.Build.0 = Release|Any CPU
+		{D60AFAC5-6DB1-4F7A-AC86-B1C0D304F83F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{D60AFAC5-6DB1-4F7A-AC86-B1C0D304F83F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{D60AFAC5-6DB1-4F7A-AC86-B1C0D304F83F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{D60AFAC5-6DB1-4F7A-AC86-B1C0D304F83F}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
-- 
GitLab