Factory
parent
b48452801b
commit
7df5dc20f7
|
@ -3,37 +3,42 @@ package Algorithmus;
|
|||
public class Allgemeine_Übungen {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String text = "H";
|
||||
try {
|
||||
System.out.println(codePointBefore(text,1));
|
||||
|
||||
}catch(StringIndexOutOfBoundsException e) {
|
||||
System.out.println(e.toString());
|
||||
int [] nums = {3, 4, -7, 3, 1, 3, 1, -4, -2, -2};
|
||||
subarraysWith0Sum(nums);
|
||||
}
|
||||
|
||||
public static void subarraysWith0Sum(int [] arr) {
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < arr.length;i++) {
|
||||
sum = 0;
|
||||
for (int j = i ; j < arr.length ; j++) {
|
||||
sum += arr[j];
|
||||
if ( sum == 0)
|
||||
System.out.println("subarray: " + arr[i] + " .... " + arr[j]);
|
||||
|
||||
|
||||
}
|
||||
|
||||
static int codePointBefore(String s, int index)throws StringIndexOutOfBoundsException{
|
||||
if (index > s.length() + 1 || index < 1)
|
||||
throw new StringIndexOutOfBoundsException();
|
||||
|
||||
char[] charArray = s.toCharArray();
|
||||
return charArray[index-1];
|
||||
}
|
||||
}
|
||||
|
||||
static int codePointAt(String s, int index)throws StringIndexOutOfBoundsException {
|
||||
if (index > s.length() || index < 0)
|
||||
throw new StringIndexOutOfBoundsException();
|
||||
char[] charArray = s.toCharArray();
|
||||
return charArray[index];
|
||||
|
||||
|
||||
|
||||
|
||||
public static void findApair(int[] arr, int target) {
|
||||
|
||||
for (int i = 0; i < arr.length - 1; i++)
|
||||
for (int j = i + 1; j < arr.length - 1; j++)
|
||||
if (arr[i] + arr[j] == target) {
|
||||
System.out.println("Pair found (" + arr[i] + " ," + arr[j] + ")");
|
||||
break;
|
||||
}
|
||||
|
||||
static char charAt(String s, int index) throws StringIndexOutOfBoundsException {
|
||||
if (index > s.length() || index < 0)
|
||||
throw new StringIndexOutOfBoundsException();
|
||||
char[] charArray = s.toCharArray();
|
||||
return charArray[index];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Factory;
|
||||
|
||||
public class FactoryMit_Java_Reflection {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// Erzeugung eines Hundes mit der Factory-Methode
|
||||
Tier hund = Tier.erzeugeTier("Hund", 3);
|
||||
System.out.println(hund); // Ausgabe: Hund, Alter: 3
|
||||
|
||||
// Erzeugung einer Katze mit der Factory-Methode
|
||||
Tier katze = Tier.erzeugeTier("Katze", 5);
|
||||
System.out.println(katze); // Ausgabe: Katze, Alter: 5
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
//Basisklasse Tier
|
||||
class Tier {
|
||||
private int alter;
|
||||
|
||||
public Tier(int alter) {
|
||||
this.alter = alter;
|
||||
}
|
||||
|
||||
public int getAlter() {
|
||||
return alter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Tier, Alter: " + alter;
|
||||
}
|
||||
|
||||
// Die Factory-Methode, die ein Tier-Objekt erzeugt
|
||||
public static Tier erzeugeTier(String tierart, int alter) throws Exception {
|
||||
// Sucht die Klasse basierend auf dem tierart-String und erstellt eine neue Instanz
|
||||
return (Tier) Class.forName(tierart)
|
||||
.getDeclaredConstructor(Integer.TYPE)
|
||||
.newInstance(alter);
|
||||
}
|
||||
}
|
||||
|
||||
//Klasse Hund, erbt von Tier
|
||||
class Hund extends Tier {
|
||||
public Hund(int alter) {
|
||||
super(alter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Hund, Alter: " + getAlter();
|
||||
}
|
||||
}
|
||||
|
||||
//Klasse Katze, erbt von Tier
|
||||
class Katze extends Tier {
|
||||
public Katze(int alter) {
|
||||
super(alter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Katze, Alter: " + getAlter();
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
package GUIAnwendungen.Meine_GUI_Chat;
|
||||
|
||||
public class ChatFenster {
|
||||
|
||||
private User user;
|
||||
private Chat_Fenster chat;
|
||||
|
||||
ChatFenster(User user) {
|
||||
this.user = user;
|
||||
chat = new Chat_Fenster();
|
||||
zeigeName();
|
||||
}
|
||||
|
||||
public void zeigeName() {
|
||||
chat.zeigeName(user.getName());
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public Chat_Fenster getChat() {
|
||||
return chat;
|
||||
}
|
||||
|
||||
public void empfangeNachricht(String nachricht) {
|
||||
chat.sendText(nachricht);
|
||||
}
|
||||
|
||||
public void sendeNachricht(String nachricht) {
|
||||
chat.sendText(user.getName() + ": " + nachricht);
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package GUIAnwendungen.Meine_GUI_Chat;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class ChatManager implements ActionListener {
|
||||
|
||||
private ChatFenster sender;
|
||||
private ChatFenster receiver;
|
||||
|
||||
ChatManager(ChatFenster sender, ChatFenster receiver) {
|
||||
this.sender = sender;
|
||||
this.receiver = receiver;
|
||||
sender.getChat().getSendeNachricht().addActionListener(this);
|
||||
receiver.getChat().getSendeNachricht().addActionListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String nachricht = sender.getChat().getEingabeNachricht().getText();
|
||||
if (!nachricht.isEmpty()) {
|
||||
sender.sendeNachricht(nachricht);
|
||||
receiver.empfangeNachricht(sender.getUser().getName() + ": " + nachricht);
|
||||
sender.getChat().getEingabeNachricht().setText("");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package GUIAnwendungen.Meine_GUI_Chat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ChatRoom {
|
||||
|
||||
private User user1;
|
||||
private User user2;
|
||||
|
||||
ChatRoom() {
|
||||
user1 = new User("Müller");
|
||||
user2 = new User("Schneider");
|
||||
user1.getCahtfenster().getSendeNachricht().addActionListener(e -> sendeNachrichtUser1());
|
||||
user2.getCahtfenster().getSendeNachricht().addActionListener(e -> sendeNachrichtUser2());
|
||||
}
|
||||
|
||||
public void sendeNachrichtUser1() {
|
||||
String nachricht = user1.getCahtfenster().getEingabeNachricht().getText();
|
||||
user1.getCahtfenster().sendeNachricht(user1.getName() + ": " + nachricht);
|
||||
user2.getCahtfenster().sendeNachricht(user1.getName() + ": " + nachricht);
|
||||
user1.getCahtfenster().getEingabeNachricht().setText("");
|
||||
|
||||
}
|
||||
|
||||
public void sendeNachrichtUser2() {
|
||||
String nachricht = user2.getCahtfenster().getEingabeNachricht().getText();
|
||||
user1.getCahtfenster().sendeNachricht(user2.getName() + ": " + nachricht);
|
||||
user2.getCahtfenster().sendeNachricht(user2.getName() + ": " + nachricht);
|
||||
user2.getCahtfenster().getEingabeNachricht().setText("");
|
||||
}
|
||||
|
||||
}
|
|
@ -18,14 +18,14 @@ import javax.swing.JLabel;
|
|||
public class Chat_Fenster extends JFrame {
|
||||
|
||||
JPanel contentPane;
|
||||
JTextField eingabeNachricht;
|
||||
JTextArea nachrichtAnzeigen;
|
||||
JButton sendeNachricht;
|
||||
private JTextField eingabeNachricht;
|
||||
private JTextArea nachrichtAnzeigen;
|
||||
private JButton sendeNachricht;
|
||||
private JPanel panel;
|
||||
JLabel userName;
|
||||
private JLabel user1Name;
|
||||
|
||||
public Chat_Fenster() {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
setBounds(100, 100, 460, 665);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
|
@ -34,11 +34,11 @@ public class Chat_Fenster extends JFrame {
|
|||
contentPane.setLayout(null);
|
||||
|
||||
nachrichtAnzeigen = new JTextArea();
|
||||
nachrichtAnzeigen.setEditable(false);
|
||||
nachrichtAnzeigen.setLineWrap(true);
|
||||
nachrichtAnzeigen.setFont(new Font("Tahoma", Font.BOLD, 13));
|
||||
nachrichtAnzeigen.setForeground(new Color(0, 128, 255));
|
||||
nachrichtAnzeigen.setEnabled(false);
|
||||
nachrichtAnzeigen.setBounds(10, 11, 311, 516);
|
||||
nachrichtAnzeigen.setBounds(10, 95, 350, 432);
|
||||
contentPane.add(nachrichtAnzeigen);
|
||||
|
||||
eingabeNachricht = new JTextField();
|
||||
|
@ -52,25 +52,27 @@ public class Chat_Fenster extends JFrame {
|
|||
|
||||
panel = new JPanel();
|
||||
panel.setBackground(new Color(0, 128, 255));
|
||||
panel.setBounds(331, 11, 103, 168);
|
||||
panel.setBounds(10, 28, 350, 56);
|
||||
contentPane.add(panel);
|
||||
panel.setLayout(null);
|
||||
|
||||
userName = new JLabel("New label");
|
||||
userName.setBounds(10, 11, 83, 36);
|
||||
panel.add(userName);
|
||||
user1Name = new JLabel("User1: ");
|
||||
user1Name.setForeground(new Color(255, 255, 255));
|
||||
user1Name.setFont(new Font("Tahoma", Font.BOLD, 14));
|
||||
user1Name.setBounds(10, 11, 83, 36);
|
||||
panel.add(user1Name);
|
||||
|
||||
this.setVisible(true);
|
||||
|
||||
}
|
||||
|
||||
public void sendText(String nachricht) {
|
||||
public void sendeNachricht(String nachricht) {
|
||||
nachrichtAnzeigen.append(nachricht);
|
||||
nachrichtAnzeigen.append("\n");
|
||||
}
|
||||
|
||||
public void zeigeName(String name) {
|
||||
userName.setText(name);
|
||||
user1Name.setText(name);
|
||||
}
|
||||
|
||||
public JTextField getEingabeNachricht() {
|
||||
|
@ -81,7 +83,17 @@ public class Chat_Fenster extends JFrame {
|
|||
return sendeNachricht;
|
||||
}
|
||||
|
||||
public JLabel getUser1Name() {
|
||||
return user1Name;
|
||||
}
|
||||
|
||||
public void setUser1Name(String user1Name) {
|
||||
this.user1Name.setText(user1Name);
|
||||
}
|
||||
|
||||
public void setEingabeNachricht(JTextField eingabeNachricht) {
|
||||
this.eingabeNachricht = eingabeNachricht;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,20 +1,11 @@
|
|||
package GUIAnwendungen.Meine_GUI_Chat;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
User obai = new User("obai Albek");
|
||||
ChatFenster obai_caht = new ChatFenster(obai);
|
||||
|
||||
User omar = new User("Omar Albek");
|
||||
ChatFenster omar_chat = new ChatFenster(omar);
|
||||
|
||||
ChatManager test = new ChatManager(obai_caht, omar_chat);
|
||||
|
||||
|
||||
new ChatRoom();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,39 +4,31 @@ import java.util.ArrayList;
|
|||
|
||||
public class User {
|
||||
private String name;
|
||||
|
||||
private int id;
|
||||
private String nachricht;
|
||||
private static int ID = 100;
|
||||
private ArrayList<String> nachrichten;
|
||||
private Chat_Fenster cahtfenster;
|
||||
|
||||
public User(String name) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.id = ID++;
|
||||
nachrichten = new ArrayList<>();
|
||||
this.cahtfenster = new Chat_Fenster();
|
||||
this.cahtfenster.setUser1Name(this.name);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getNachricht() {
|
||||
return nachricht;
|
||||
}
|
||||
|
||||
public void setNachricht(String nachricht) {
|
||||
public void sendeNachricht(String nachricht) {
|
||||
this.nachricht = nachricht;
|
||||
}
|
||||
|
||||
public Chat_Fenster getCahtfenster() {
|
||||
return cahtfenster;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package GUIAnwendungen.StudentManagementSystem;
|
||||
package GUIAnwendungen.StudentManagementSystem.Domain;
|
||||
|
||||
public class Student {
|
||||
|
|
@ -3,11 +3,15 @@ package GUIAnwendungen.StudentManagementSystem;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Vector;
|
||||
|
||||
import GUIAnwendungen.StudentManagementSystem.Domain.Student;
|
||||
|
||||
public class VerwalteStudent {
|
||||
|
||||
private Student student;
|
||||
private Student_login view;
|
||||
private ArrayList<Student> allStudents;
|
||||
private int index = 0;
|
||||
|
||||
public VerwalteStudent() {
|
||||
this.view = new Student_login();
|
||||
view.getInsertStudent().addActionListener(e -> insert());
|
||||
|
@ -18,18 +22,7 @@ public class VerwalteStudent {
|
|||
allStudents = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void showAll() {
|
||||
if (allStudents.size() == 0)
|
||||
view.showErrore("Keine Student sind im System!");
|
||||
|
||||
else {
|
||||
for (Student als:allStudents ) {
|
||||
String[] array = {als.getId()+"",als.getName(),als.getVorname(),als.getGeburtstag(),als.getGeschlecht(),als.getTele(),als.getEmail(),als.getAdresse()};
|
||||
view.getModel().addRow(array);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void insert() {
|
||||
String idText = view.getEingabeId().getText();
|
||||
|
@ -45,9 +38,27 @@ public class VerwalteStudent {
|
|||
student = new Student(name, vorname, geschlecht, email, teleN, Adresse, geburtstag, id);
|
||||
allStudents.add(student);
|
||||
view.showInfo(student.toString());
|
||||
|
||||
String[] array = {student.getId()+"",student.getName(),student.getVorname(),student.getGeburtstag(),student.getGeschlecht(),student.getTele(),student.getEmail(),student.getAdresse()};
|
||||
view.getModel().addRow(array);
|
||||
|
||||
|
||||
restAlleInputs();
|
||||
}
|
||||
|
||||
public void showAll() {
|
||||
if (allStudents.size() == 0)
|
||||
view.showErrore("Keine Student sind im System!");
|
||||
|
||||
else {
|
||||
for (Student als:allStudents ) {
|
||||
String[] array = {als.getId()+"",als.getName(),als.getVorname(),als.getGeburtstag(),als.getGeschlecht(),als.getTele(),als.getEmail(),als.getAdresse()};
|
||||
view.getModel().addRow(array);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void update() {
|
||||
String idText = view.getEingabeId().getText();
|
||||
int id = Integer.parseInt(idText);
|
||||
|
@ -91,6 +102,7 @@ public class VerwalteStudent {
|
|||
if (student != null) {
|
||||
allStudents.remove(student);
|
||||
view.showInfo("Student wurde erfolgreich gelöscht");
|
||||
showAll();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -118,7 +118,7 @@ public class MyGenericHashMap<K, V> {
|
|||
|
||||
//Ausgabe der Elemente
|
||||
public void printAll() {
|
||||
for (Entry<?,?> temp : table) {
|
||||
for (Entry<K,V> temp : table) {
|
||||
if (temp != null) {
|
||||
System.out.println("[" + temp.key + "] :" + temp.value + " ");
|
||||
while(temp.next != null) {
|
||||
|
@ -134,6 +134,8 @@ public class MyGenericHashMap<K, V> {
|
|||
//Überprüft, ob der angegebene Schlüssel in der HashMap vorhanden ist.
|
||||
//Gibt true zurück, wenn der Schlüssel existiert, andernfalls false.
|
||||
public boolean containsKey(K key) {
|
||||
// hashCode(): gibt den Wert als ganzzahl zurück
|
||||
//Beispiel: 'a' => 97
|
||||
int hash = key.hashCode() % SIZE;
|
||||
Entry<K,V> temp = table[hash];
|
||||
while (temp != null) {
|
||||
|
@ -182,6 +184,7 @@ public class MyGenericHashMap<K, V> {
|
|||
|
||||
t1.printAll();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package Hashmap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MyHashMap {
|
||||
|
||||
|
@ -62,5 +61,7 @@ public class MyHashMap {
|
|||
Character key = 'a';
|
||||
int hash = key.hashCode();
|
||||
System.out.println(hash);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,9 @@ package oop.Abstraction;
|
|||
|
||||
public abstract class Auto {
|
||||
|
||||
double höhe;
|
||||
double gewicht;
|
||||
int anzahlDerRäder;
|
||||
private double höhe;
|
||||
private double gewicht;
|
||||
private int anzahlDerRäder;
|
||||
|
||||
|
||||
|
||||
|
@ -20,6 +20,8 @@ public abstract class Auto {
|
|||
|
||||
abstract void parkingSensors();
|
||||
|
||||
|
||||
private void print() {
|
||||
System.out.println("Hallo Welt");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ public class Hatchback extends Auto {
|
|||
}
|
||||
|
||||
@Override
|
||||
|
||||
void autopilot() {
|
||||
System.out.println(" Hatchback : autopilot");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue