parent
f1faf023cf
commit
9b71822864
|
@ -0,0 +1,24 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo;
|
||||
|
||||
import DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain.Animal;
|
||||
import DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain.Cat;
|
||||
import DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain.Dog;
|
||||
|
||||
public class FaktoryMethode {
|
||||
|
||||
public static Animal erzeugeTiere(String species,String name, int alter) {
|
||||
switch (species) {
|
||||
|
||||
case "Dog":
|
||||
return new Dog(name,alter);
|
||||
|
||||
case "Cat":
|
||||
return new Cat(name,alter);
|
||||
|
||||
default:
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain.*;
|
||||
|
||||
public class ZooMain {
|
||||
|
||||
public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
|
||||
// Animal tiere = Animal.erzeugeTier("Britisch","DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain.Cat", 12);
|
||||
//
|
||||
// System.out.println(tiere.speak());
|
||||
|
||||
Animal tiere = FaktoryMethode.erzeugeTiere("Cat", "bob", 12);
|
||||
|
||||
tiere.speak();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ZooUI {
|
||||
|
||||
FaktoryMethode system;
|
||||
|
||||
public ZooUI() {
|
||||
startProgramm();
|
||||
|
||||
}
|
||||
|
||||
private void startProgramm() {
|
||||
Scanner eingabe = new Scanner(System.in);
|
||||
System.out.println("Willkommen in meinem ZOO");
|
||||
while (true) {
|
||||
System.out.println("Welches Tier möchten Sie haben?");
|
||||
System.out.println("1. Katze");
|
||||
System.out.println("2. Hund");
|
||||
System.out.println("3. Mouse");
|
||||
String auswahl = eingabe.nextLine();
|
||||
String name = eingabe.nextLine();
|
||||
int alter = eingabe.nextInt();
|
||||
switch (auswahl) {
|
||||
case "Katze":
|
||||
System.out.println("Name: ");
|
||||
system.erzeugeTiere(auswahl, auswahl, 0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public abstract class Animal {
|
||||
private String name;
|
||||
private int alter;
|
||||
private String species;
|
||||
|
||||
public Animal(String name, int alter) {
|
||||
this.name = name;
|
||||
this.alter = alter;
|
||||
}
|
||||
|
||||
public abstract String speak();
|
||||
|
||||
public String getSpecies() {
|
||||
return species;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getBirthyear() {
|
||||
return alter;
|
||||
}
|
||||
|
||||
public void setBirthyear(int birthyear) {
|
||||
this.alter = birthyear;
|
||||
}
|
||||
|
||||
public void setSpecies(String species) {
|
||||
this.species = species;
|
||||
}
|
||||
|
||||
|
||||
public static Animal erzeugeTier(String species ,String name, int alter) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
|
||||
return (Animal) Class.forName(species)
|
||||
.getDeclaredConstructor(String.class, Integer.TYPE)
|
||||
.newInstance(name,alter);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain;
|
||||
|
||||
public class Cat extends Animal {
|
||||
|
||||
public Cat( String name, int alter) {
|
||||
super(name, alter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String speak() {
|
||||
return super.getSpecies()+ " macht: Meow";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain;
|
||||
|
||||
public class Dog extends Animal {
|
||||
|
||||
|
||||
public Dog( String name, int alter) {
|
||||
super(name, alter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String speak() {
|
||||
return super.getName() + " macht: Woof";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain;
|
||||
|
||||
public class Mouse extends Animal {
|
||||
|
||||
public Mouse(String name, int alter) {
|
||||
super(name, alter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String speak() {
|
||||
return super.getSpecies() + " macht etwas etwas";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory.Zoo.domain;
|
||||
|
||||
public class Zoo {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package GUIAnwendungen.Beispiele;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class GuiDemo implements ActionListener {
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame jf = new JFrame("Toller Titel");
|
||||
|
||||
jf.add(new JLabel("Hallo Welt!"), BorderLayout.NORTH);
|
||||
jf.add(new JTextField(10), BorderLayout.CENTER);
|
||||
|
||||
JButton jb1 = new JButton("Cancel");
|
||||
JButton jb2 = new JButton("OK");
|
||||
|
||||
JPanel jp = new JPanel();
|
||||
jp.setLayout(new GridLayout());
|
||||
jp.add(jb1);
|
||||
jp.add(jb2);
|
||||
|
||||
jf.add(jp, BorderLayout.SOUTH);
|
||||
|
||||
jf.pack();
|
||||
jf.setVisible(true);
|
||||
|
||||
GuiDemo obj = new GuiDemo();
|
||||
jb1.addActionListener(obj);
|
||||
jb2.addActionListener(obj);
|
||||
|
||||
System.out.println("Ende Gelände...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JButton src = (JButton) e.getSource();
|
||||
System.out.println("Button geklickt: " + src.getText());
|
||||
}
|
||||
|
||||
}
|
|
@ -32,7 +32,6 @@ public class jbutton implements ActionListener {
|
|||
// x y width height
|
||||
button.setBounds(100,100,200, 50);
|
||||
button.setText("Click me");
|
||||
button.addActionListener(null);
|
||||
|
||||
// 2. Hinzufügen des ActionListeners zum Button
|
||||
JLabel label = new JLabel();
|
||||
|
@ -46,6 +45,8 @@ public class jbutton implements ActionListener {
|
|||
* new ActionListener() ist nicht das Erzeugen einer Instanz des Interfaces selbst,
|
||||
* sondern das Erstellen einer Instanz einer anonymen Klasse, die das ActionListener-Interface implementiert.
|
||||
*/
|
||||
|
||||
// Variante 1
|
||||
button.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
@ -54,7 +55,10 @@ public class jbutton implements ActionListener {
|
|||
}
|
||||
});
|
||||
|
||||
//Variante 2:
|
||||
|
||||
jbutton obj = new jbutton();
|
||||
button.addActionListener(obj);
|
||||
|
||||
// Erstelle ein Fenster
|
||||
JFrame frame = new JFrame();
|
||||
|
@ -86,8 +90,7 @@ public class jbutton implements ActionListener {
|
|||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
System.out.println("Variante 2");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package Hashmap.Autovermietung;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Autovermietung {
|
||||
// private ArrayList<Pkw> autos = new ArrayList<>();
|
||||
HashMap<String, Pkw> autos = new HashMap<>();
|
||||
|
||||
public int getAnzahlPkws() {
|
||||
return autos.size();
|
||||
}
|
||||
|
||||
public void pkwAnlegen(String kennzeichen, String typ, int kmStand) {
|
||||
Pkw p = new Pkw(kennzeichen, typ, kmStand);
|
||||
autos.put(kennzeichen, p);
|
||||
}
|
||||
|
||||
public String[] getPkwListe() {
|
||||
String[] liste = new String[autos.size()];
|
||||
|
||||
for (int i = 0; i < autos.size(); i++)
|
||||
liste[i] = autos.get(i).toString();
|
||||
|
||||
return liste;
|
||||
}
|
||||
|
||||
public void verleiheFahrzeug(String kennzeichen) {
|
||||
Pkw zuVerleihen = suchePkwNachKennzeichen(kennzeichen);
|
||||
zuVerleihen.verleihen(new Kunde("Müller"));
|
||||
|
||||
// Hier könnten noch weitere Dinge erledigt werden, bspw. einen Counter für Vermietungen hochzählen o.ä.
|
||||
}
|
||||
|
||||
// Hier wird zu Demonstrationszwecken das Geheimnisprinzip verletzt
|
||||
public ArrayList<Pkw> suchePkwsNachTyp(String typ) {
|
||||
ArrayList<Pkw> treffer = new ArrayList<>();
|
||||
|
||||
for (Pkw p : autos.values())
|
||||
if (typ.equals(p.getType()))
|
||||
treffer.add(p);
|
||||
|
||||
return treffer;
|
||||
}
|
||||
|
||||
private Pkw suchePkwNachKennzeichen(String kennzeichen) {
|
||||
return autos.get(kennzeichen);
|
||||
|
||||
// for (Pkw p : autos.values())
|
||||
// if (kennzeichen.equals(p.getKennzeichen()))
|
||||
// return p;
|
||||
//
|
||||
// return null;
|
||||
}
|
||||
|
||||
public boolean pkwZurücknehmen(String kennzeichen, int gefahreneKm) {
|
||||
Pkw zurück = suchePkwNachKennzeichen(kennzeichen);
|
||||
|
||||
if (zurück == null)
|
||||
return false;
|
||||
|
||||
zurück.rückgabe(gefahreneKm);
|
||||
// IRL würde man vermutlich den Km-Stand abfragen, aber so haben wir eine Kleinigkeit zu rechnen.
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package Hashmap;
|
||||
package Hashmap.Autovermietung;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
|
@ -0,0 +1,21 @@
|
|||
package Hashmap.Autovermietung;
|
||||
|
||||
public class Kunde {
|
||||
private String name;
|
||||
|
||||
public Kunde(String name) {
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package Hashmap.Autovermietung;
|
||||
|
||||
public class Pkw {
|
||||
private String kennzeichen;
|
||||
private String type;
|
||||
private int kmStand;
|
||||
|
||||
public Pkw(String kennzeichen, String typ, int kmStand) {
|
||||
}
|
||||
|
||||
public String getKennzeichen() {
|
||||
return kennzeichen;
|
||||
}
|
||||
|
||||
public void setKennzeichen(String kennzeichen) {
|
||||
this.kennzeichen = kennzeichen;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getKmStand() {
|
||||
return kmStand;
|
||||
}
|
||||
|
||||
public void setKmStand(int kmStand) {
|
||||
this.kmStand = kmStand;
|
||||
}
|
||||
|
||||
public void verleihen(Kunde kunde) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void rückgabe(int gefahreneKm) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package oop.ExceptionHandling;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CCMExceptionProvoker {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ArrayList<String> al = new ArrayList<>();
|
||||
|
||||
al.add("Peggy");
|
||||
al.add("Bud");
|
||||
al.add("Kelly");
|
||||
al.add("Al");
|
||||
|
||||
String otbr = null;
|
||||
|
||||
for (String s : al) {
|
||||
System.out.println(s);
|
||||
|
||||
if (s.equals("Al")) {
|
||||
System.out.println("I move out of the House!");
|
||||
al.remove(s); // Führt zur Exception, weil dem Iterator die genutze Liste "zerschossen" wird.
|
||||
// otbr = s;
|
||||
break; // Aber wenn wir break machen, dann geht das
|
||||
}
|
||||
}
|
||||
|
||||
if (otbr != null)
|
||||
al.remove(otbr);
|
||||
|
||||
System.out.println(al.size());
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -48,7 +48,7 @@ public final class Auto implements Movable,SelfDrivable {
|
|||
|
||||
public void newMethod() {
|
||||
|
||||
System.out.print("SelfDrivable");
|
||||
System.out.print("SelfDrivable Auto");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
package oop.Interface;
|
||||
|
||||
public class Interface {
|
||||
|
||||
/*Was ist das?
|
||||
* Eigentlich ist das fast wie die Abstrakte klassen außer:
|
||||
*
|
||||
* Implementierung Mehrfachvererbung
|
||||
* abstrakte Klasse
|
||||
*
|
||||
* public interface klasseName
|
||||
* alle Attribute werden automatisch (public static final)
|
||||
* fast alle methoden werden automatisch (public abstract)
|
||||
* ,außer die default methoden, die sollen aber in unterkalsse mit dem Wort public (soll geschrieben werden) implementiert werden und die können überschrieben werden
|
||||
* Interface kann auch statiche methoden haben, die implementiert werden sollen
|
||||
* default methoden sind nur in Interface erlaubt!
|
||||
* final methoden sind nicht erlaubt, da sie abstrake sind und sollen überschreiben werden!
|
||||
* private methoden sind erlaubt aber nur in der selben Klasse!
|
||||
* Interface kann keinen Konstruktor haben!
|
||||
* die untereklasse soll alle methode der Interface klasse implementieren
|
||||
*
|
||||
*
|
||||
* NestedInterface:
|
||||
* public interface NestedInterface {
|
||||
void test();
|
||||
|
||||
interface InnerInterface {
|
||||
default void test2() {
|
||||
System.out.print("Innerinterface");
|
||||
}
|
||||
}
|
||||
|
||||
implements wid durch NestedInterface.InnerInterface
|
||||
|
||||
*Functional Interface:
|
||||
* @FunctionalInterface
|
||||
public interface Test2{
|
||||
int berechne(int a, int b);
|
||||
|
||||
}
|
||||
* @FunctionalInterface dieses Interfaceklasse kann nur
|
||||
* (eine abstratke) methode haben aber unendlich viele
|
||||
* default,static und private methoden haben
|
||||
*
|
||||
* Marker or Tagging Interface:
|
||||
* ist ein Interface in Java, das keine Methoden oder Konstanten deklarier
|
||||
*
|
||||
*
|
||||
*
|
||||
* wichtig:
|
||||
* . eine unterekalsse extends eine Obereklasse
|
||||
* . eine normale oder final Klasse kann implements eine interface klasse
|
||||
* . eine interface klasse extends eine interface klasse
|
||||
*
|
||||
* welche java types kann interface implementieren?
|
||||
* 1. klassen
|
||||
* 2. abstrakte klassen
|
||||
* 3. nested Klassen (Verschateten klassen)
|
||||
* 4. Enum
|
||||
* 5. dynamic proxy
|
||||
*/
|
||||
|
||||
}
|
Binary file not shown.
|
@ -1,6 +1,7 @@
|
|||
package oop.Interface;
|
||||
|
||||
public interface Movable {
|
||||
public static final int x = 0;
|
||||
|
||||
void moveUp();
|
||||
void moveDown();
|
||||
|
@ -12,4 +13,9 @@ public interface Movable {
|
|||
System.out.print("SelfDrivable");
|
||||
}
|
||||
|
||||
|
||||
public static void print() {
|
||||
System.out.println("Statische Methode");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 33 KiB |
|
@ -5,6 +5,7 @@ public class Test {
|
|||
|
||||
public static void main(String[] args) {
|
||||
Movable a1 = new Auto();
|
||||
a1.newMethod();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
package oop.inctanseof_Beispiel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//Interface
|
||||
public interface Dtails {
|
||||
|
||||
void getDetails();
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Dtails> datenListe = new ArrayList<>();
|
||||
|
||||
|
||||
|
||||
// Objekte hinzufügen
|
||||
datenListe.add(new Manager("Herr Müller"));
|
||||
datenListe.add(new Katze("Whiskers"));
|
||||
datenListe.add(new Hund("Bello"));
|
||||
|
||||
// Überprüfen, ob es ein Mitarbeiter, Katze oder Hund ist
|
||||
for (Dtails daten : datenListe) {
|
||||
if (daten instanceof Mitarbeiter) {
|
||||
System.out.println("Das ist ein Mitarbeiter.");
|
||||
daten.getDetails(); // Ruft die Methode von Manager auf
|
||||
} else if (daten instanceof Katze) {
|
||||
System.out.println("Das ist eine Katze.");
|
||||
daten.getDetails(); // Ruft die Methode von Katze auf
|
||||
} else if (daten instanceof Hund) {
|
||||
System.out.println("Das ist ein Hund.");
|
||||
daten.getDetails(); // Ruft die Methode von Hund auf
|
||||
} else if (daten instanceof Animal) {
|
||||
System.out.println("Das ist ein anderes Tier.");
|
||||
daten.getDetails(); // Ruft die Methode von Animal auf
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Abstrakte Klasse Mitarbeiter
|
||||
abstract class Mitarbeiter implements Dtails {
|
||||
private String name;
|
||||
|
||||
public Mitarbeiter(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
// Subklasse Manager
|
||||
class Manager extends Mitarbeiter {
|
||||
public Manager(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getDetails() {
|
||||
System.out.println("Manager Name: " + getName());
|
||||
}
|
||||
}
|
||||
|
||||
// Abstrakte Klasse Animal
|
||||
abstract class Animal implements Dtails {
|
||||
private String name;
|
||||
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
// Subklasse Katze
|
||||
class Katze extends Animal {
|
||||
public Katze(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getDetails() {
|
||||
System.out.println("Katze Name: " + getName());
|
||||
}
|
||||
}
|
||||
|
||||
// Subklasse Hund
|
||||
class Hund extends Animal {
|
||||
public Hund(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getDetails() {
|
||||
System.out.println("Hund Name: " + getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -30,18 +30,11 @@ public class Controller implements ActionListener {
|
|||
public void actionPerformed(ActionEvent e) {
|
||||
letzterText = v1.eingabe.getText();
|
||||
String speicher = "";
|
||||
JButton clickedButton;
|
||||
// Gibt mir, welche Button gedrückt ist
|
||||
JButton src = (JButton) e.getSource();
|
||||
|
||||
if (e.getSource() == v1.button1 || e.getSource() == v1.button2 || e.getSource() == v1.button3
|
||||
|| e.getSource() == v1.button4 || e.getSource() == v1.button5 || e.getSource() == v1.button6
|
||||
|| e.getSource() == v1.button7 || e.getSource() == v1.button8 || e.getSource() == v1.button9
|
||||
|| e.getSource() == v1.button0 || e.getSource() == v1.plus || e.getSource() == v1.minus
|
||||
|| e.getSource() == v1.multi || e.getSource() == v1.divid || e.getSource() == v1.punkt) {
|
||||
|
||||
clickedButton = (JButton) e.getSource();
|
||||
speicher = clickedButton.getText();
|
||||
speicher = src.getText();
|
||||
v1.setEingabe(letzterText + speicher);
|
||||
}
|
||||
letzterText = v1.eingabe.getText();
|
||||
|
||||
if (e.getSource() == v1.berechnen) {
|
||||
|
|
Loading…
Reference in New Issue