master
3009594 2024-09-10 16:40:22 +02:00
parent d4fa8a35ba
commit ff3375a644
25 changed files with 721 additions and 505 deletions

View File

@ -11,5 +11,10 @@
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/Users/obaya/Downloads/mysql-connector-j-9.0.0/mysql-connector-j-9.0.0.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -1,50 +0,0 @@
package BankSystemGUI;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class BankController {
private File file;
BankController(){
file = new File("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\BankSystemGUI\\Kunde.txt");
if (!file.exists()) {
try {
if (file.createNewFile()) // createNewFile() gibt boolean zurück,ob der File ergolgreich erstellt wurde
// oder nicht
System.out.println("Datei erstellt: " + file.getName()); // der File muss im Pfad nicht existieren,
// damit es erstellt wurde
} catch (IOException d) {
d.printStackTrace();
}
}
}
public BankSystem addKunde(String name,String vorname,String password,String email) {
BankSystem b1 = new BankSystem(name,vorname,password,email);
saveKunde(b1);
return b1;
}
public void saveKunde(BankSystem b1) {
try (PrintWriter write = new PrintWriter(new FileWriter(file))) {
write.println("ID: " + b1.getId());
write.println("Vorname: " + b1.getVorname());
write.println("Nachname: " + b1.getName());
write.println("Password: " + b1.getPassword());
write.println("Email: " + b1.getEmail());
write.println("kontoStand " + b1.getKontoStand());
write.println(); // Leere Zeile zwischen Einträgen
write.flush();
write.close();
System.out.println(b1.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -1,93 +0,0 @@
package BankSystemGUI;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
// Recourses Klasse (Model)
public class BankSystem {
private String name;
private String vorname;
private String password;
private String email;
private double kontoStand;
private int Id;
private static int IDCounter = 0;
BankSystem(){}
BankSystem(String name,String vorname,String password,String email){
this.name = name;
this.vorname = vorname;
this.password = password;
this.email = email;
this.kontoStand = 0;
this.Id = ++IDCounter;
}
public double einzahlung(double wert) {
return this.kontoStand += wert;
}
public int loadIDCounter() {
return Id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getKontoStand() {
return kontoStand;
}
public int getId() {
return Id;
}
@Override
public String toString() {
return "BankSystem [name=" + name + ", vorname=" + vorname + ", password=" + password + ", kontoStand="
+ kontoStand + ", Id=" + Id + "]";
}
}

View File

@ -1,14 +0,0 @@
package BankSystemGUI;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class BankSystemTest {
@Test
public void testEinzahlung() {
BankSystem test = new BankSystem();
double x = test.einzahlung(100);
assertEquals(100, x, 0.001); // Hier wird überprüft, ob der Kontostand 100.0 ist
}
}

View File

@ -0,0 +1,77 @@
package BankSystemGUI;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class Controller {
private Datenbanken datenbanken;
private Window view;
private Modell erstelleKonto;
public Controller() throws SQLException {
this.datenbanken = new Datenbanken();
this.view = new Window();
this.view.setAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
erstelleKonto();
} catch (SQLException | NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
}
});
}
public void erstelleKonto() throws SQLException, NoSuchAlgorithmException {
String email = view.textFieldEmail.getText();
if (checkUser(email)) {
String name = view.textFieldName.getText();
String vorname = view.textFieldVorname.getText();
String password = view.textFieldPassword.getText();
String teleN = view.textFieldTeleN.getText();
String salt = createSalt();
String hashedPassword = hashPassword(password, salt);
erstelleKonto = new Modell(name, vorname, hashedPassword, email, teleN); // Salt wird übergeben
datenbanken.fügeKontoEin(erstelleKonto);
datenbanken.sendeUserDaten(email);
view.showMessage();
view.dispose();
} else {
view.showError();
}
}
public void einzahlung(int betrag) {
if (betrag > 0)
erstelleKonto.setKontoStand(erstelleKonto.getKontoStand() + betrag);
}
private boolean checkUser(String email) throws SQLException {
return datenbanken.checkUser(email);
}
// Erzeuge ein Salt
private static String createSalt() {
SecureRandom sr = new SecureRandom();
byte[] salt = new byte[16];
sr.nextBytes(salt);
return Base64.getEncoder().encodeToString(salt);
}
// Passwort hashen
public static String hashPassword(String password, String salt) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(Base64.getDecoder().decode(salt)); // Salt wird hinzugefügt
byte[] hashedPassword = md.digest(password.getBytes());
return Base64.getEncoder().encodeToString(hashedPassword); // Hashed Password wird zurückgegeben
}
}

View File

@ -0,0 +1,30 @@
package BankSystemGUI;
import java.sql.SQLException;
import java.sql.SQLException;
public class ControllerUserWindow {
private UserWindow user;
private Datenbanken datenbanken;
private String[] speicher;
public ControllerUserWindow(String email) throws SQLException {
this.user = new UserWindow();
this.datenbanken = new Datenbanken();
sendUserDaten(email);
}
public void sendUserDaten(String email) throws SQLException {
datenbanken.sendeUserDaten(email);
speicher = datenbanken.getSpeicher();
user.showUserDaten(speicher[0] + " " + speicher[1], speicher[2]);
}
public static void main(String[] args) throws SQLException {
ControllerUserWindow c1 = new ControllerUserWindow("obay@gmail.com");
}
}

View File

@ -0,0 +1,62 @@
package BankSystemGUI;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Datenbanken {
private Connection connection;
private String[] datenSpeicher;
public Datenbanken() throws SQLException {
this.connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/banksystem", "root", "");
}
private void fügeKonto(Modell konto) throws SQLException {
String query = "INSERT INTO kunden (name, vorname, password, email, teleN, kontostand) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setString(1, konto.getName());
stmt.setString(2, konto.getVorname());
stmt.setString(3, konto.getPassword());
stmt.setString(4, konto.getEmail());
stmt.setString(5, konto.getTeleN());
stmt.setInt(6, konto.getKontoStand());
stmt.executeUpdate();
}
public void fügeKontoEin(Modell konto) throws SQLException {
fügeKonto(konto);
}
public boolean checkUser(String email) throws SQLException {
String query = "SELECT email FROM kunden WHERE email = ?";
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setString(1, email);
ResultSet rs = stmt.executeQuery();
return !rs.next(); // Wenn ein Ergebnis gefunden wurde, existiert der User
}
public void sendeUserDaten(String email) throws SQLException {
datenSpeicher = new String[3];
String sql = "SELECT name, vorname, kontostand FROM kunden WHERE email = ?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, email);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
datenSpeicher[0] = rs.getString("name");
datenSpeicher[1] = rs.getString("vorname");
datenSpeicher[2] = rs.getString("kontostand");
}
}
public String[] getSpeicher() {
return datenSpeicher;
}
}

View File

@ -1,57 +0,0 @@
package BankSystemGUI;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JLabel;
public class Hauptfenster extends BankSystem {
private Window fenster;
private Scanner scan;
Hauptfenster() throws FileNotFoundException{
File fr = new File("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\BankSystemGUI\\Kunde.txt");
scan = new Scanner(fr);
String vorname = null;
String name = null;
double kontoStand = 0.0;
while (scan.hasNextLine()) {
String line = scan.nextLine();
// Prüfen, ob die Zeile den "Vorname" enthält
if (line.startsWith("Vorname:"))
vorname = line.split(":")[1].trim(); // Den Namen extrahieren
if (line.startsWith("Nachname: "))
name = line.split(":")[1].trim();
if (line.startsWith("kontoStand ")) {
String value = line.split(" ")[1].trim();
kontoStand = Double.parseDouble(value);
break; // Wir haben den Namen gefunden, also beenden wir die Schleife
}
}
fenster = new Window();
fenster.setTitle("Haupseite");
JLabel willkommennachricht = new JLabel("Willkommen " + vorname + " " + name);
willkommennachricht.setBounds(10, 10, 200, 40);
JLabel konotstand = new JLabel("Deine Aktuelle Kontostand: " + kontoStand);
konotstand.setBounds(10,50 ,200 ,40 );
fenster.add(konotstand);
fenster.add(willkommennachricht);
}
public static void main(String[] args) throws FileNotFoundException {
Hauptfenster test = new Hauptfenster();
}
}

View File

@ -1,7 +0,0 @@
ID: 1
Vorname: obai
Nachname: albek
Password: 12345
Email: obay@gmail.com
kontoStand 0.0

View File

@ -1,125 +0,0 @@
package BankSystemGUI;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LogIn extends JFrame implements ActionListener {
private JButton submit;
private File file;
private PrintWriter write;
private JTextField eingabeVorname;
private JTextField eingabenNachname;
private JTextField eingabenPassword;
private JTextField eingabeEmail;
private BankController controller;
private Window fenster;
LogIn() {
controller = new BankController();
fenster = new Window();
fenster.setTitle("log in Seite");
JLabel überschrift = new JLabel("loggen Sie sich ein: ");
überschrift.setBounds(10, 2, 120, 40);
JLabel vorname = new JLabel("Vorname: ");
vorname.setBounds(10, 50, 60, 10);
eingabeVorname = new JTextField();
eingabeVorname.setBounds(80, 50, 100, 20);
// setBounds(int x, int y, int width, int height)
JLabel nachname = new JLabel("nachname: ");
nachname.setBounds(10, 90, 100, 20);
eingabenNachname = new JTextField();
eingabenNachname.setBounds(80, 90, 100, 20);
JLabel password = new JLabel("password: ");
password.setBounds(10, 130, 100, 20);
eingabenPassword = new JTextField();
eingabenPassword.setBounds(80, 130, 100, 20);
JLabel email = new JLabel("Email: ");
email.setBounds(10, 170, 100, 20);
eingabeEmail = new JTextField();
eingabeEmail.setBounds(80, 170, 100, 20);
submit = new JButton("Submit");
submit.setBounds(10, 210, 100, 20);
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
logUser();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
fenster.add(überschrift);
fenster.add(vorname);
fenster.add(eingabeVorname);
fenster.add(nachname);
fenster.add(eingabenNachname);
fenster.add(password);
fenster.add(eingabenPassword);
fenster.add(email);
fenster.add(eingabeEmail);
fenster.add(submit);
}
public void logUser() throws IOException {
if (eingabeVorname.getText().isEmpty() || eingabenNachname.getText().isEmpty()
|| eingabenPassword.getText().isEmpty() || eingabeEmail.getText().isEmpty())
JOptionPane.showMessageDialog(null, "Alle Felder sind erforderlich", "Fehler", JOptionPane.ERROR_MESSAGE);
else {
String vorname = eingabeVorname.getText();
String nachname = eingabenNachname.getText();
String password = eingabenPassword.getText();
String email = eingabeEmail.getText();
BankSystem kunde = controller.addKunde(nachname, vorname, password, email);
//JOptionPane.showMessageDialog(null, "Erfolgreich eingeloggt!", "Info", JOptionPane.INFORMATION_MESSAGE);
fenster.dispose();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,54 @@
package BankSystemGUI;
public class Modell {
private String name;
private String vorname;
private String password;
private String email;
private String teleN;
private int kontoStand;
private int id;
public Modell(String name, String vorname, String password, String email, String teleN) {
this.name = name;
this.vorname = vorname;
this.password = password;
this.email = email;
this.teleN = teleN;
this.kontoStand = 0;
}
public String getName() {
return name;
}
public String getVorname() {
return vorname;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
public int getId() {
return id;
}
public String getTeleN() {
return teleN;
}
public int getKontoStand() {
return kontoStand;
}
public void setKontoStand(int kontoStand) {
this.kontoStand = kontoStand;
}
}

View File

@ -1,17 +1,13 @@
package BankSystemGUI;
import java.util.ArrayList;
import java.awt.EventQueue;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) {
public static void main(String[] args) throws ClassNotFoundException, SQLException, NoSuchAlgorithmException {
Controller c1 = new Controller ();
BankSystem b1 = new BankSystem("obai","albek","12345","obay@gmail.com");
System.out.println(b1.toString());
b1.einzahlung(100);
System.out.println(b1.getKontoStand());
System.out.println(b1.toString());
//
}

View File

@ -0,0 +1,76 @@
package BankSystemGUI;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class UserWindow extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
JLabel begrüsseUser;
JLabel userKontoStand;
public UserWindow(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 757, 654);
this.contentPane = new JPanel();
this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
this.setLocationRelativeTo(this);
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(128, 128, 0), 2));
panel.setBackground(new Color(192, 192, 192));
panel.setBounds(21, 23, 405, 377);
contentPane.add(panel);
panel.setLayout(null);
begrüsseUser = new JLabel("Willkommen ");
begrüsseUser.setFont(new Font("Tahoma", Font.BOLD, 15));
begrüsseUser.setForeground(new Color(0, 128, 0));
begrüsseUser.setBounds(24, 23, 333, 28);
panel.add(begrüsseUser);
userKontoStand = new JLabel("Kontostand: ");
userKontoStand.setFont(new Font("Tahoma", Font.BOLD, 15));
userKontoStand.setBounds(24, 82, 263, 47);
panel.add(userKontoStand);
JButton btnNewButton = new JButton("Einzahlen");
btnNewButton.setBounds(10, 249, 110, 23);
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Auszahlen");
btnNewButton_1.setBounds(130, 249, 109, 23);
panel.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Überweisen");
btnNewButton_2.setBounds(249, 249, 128, 23);
panel.add(btnNewButton_2);
this.setVisible(true);
}
public void showUserDaten(String name, String kontoStand) {
begrüsseUser.setText(begrüsseUser.getText() + " " + name);
userKontoStand.setText(userKontoStand.getText() + " " + kontoStand);
}
public static void main(String[] args) {
new UserWindow();
}
}

View File

@ -1,22 +1,124 @@
package BankSystemGUI;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Window extends JFrame {
public JFrame window;
private static final long serialVersionUID = 1L;
JPanel contentPane;
JTextField textFieldName;
JTextField textFieldVorname;
JTextField textFieldPassword;
JTextField textFieldEmail;
JTextField textFieldTeleN;
JButton btnNewButton;
Window(){
public Window() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 955, 664);
this.setLocationRelativeTo(this);
setSize(700, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setVisible(true);
contentPane = new JPanel();
contentPane.setForeground(new Color(128, 128, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(255, 0, 0), 1, true));
panel.setBackground(new Color(192, 192, 192));
panel.setBounds(31, 23, 349, 343);
contentPane.add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Erstelle Konto");
lblNewLabel.setBounds(21, 11, 120, 20);
panel.add(lblNewLabel);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel.setForeground(new Color(128, 128, 255));
lblNewLabel.setBackground(new Color(0, 0, 160));
JLabel lblNewLabel_1 = new JLabel("Name: ");
lblNewLabel_1.setBounds(10, 42, 46, 14);
panel.add(lblNewLabel_1);
textFieldName = new JTextField();
textFieldName.setBounds(75, 39, 169, 20);
panel.add(textFieldName);
textFieldName.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("Vorname: ");
lblNewLabel_2.setBounds(10, 80, 60, 14);
panel.add(lblNewLabel_2);
textFieldVorname = new JTextField();
textFieldVorname.setBounds(75, 77, 169, 20);
panel.add(textFieldVorname);
textFieldVorname.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("Password: ");
lblNewLabel_3.setBounds(10, 123, 66, 14);
panel.add(lblNewLabel_3);
JLabel lblNewLabel_3_1 = new JLabel("Email: ");
lblNewLabel_3_1.setBounds(10, 170, 66, 14);
panel.add(lblNewLabel_3_1);
JLabel lblNewLabel_4 = new JLabel("TeleN: ");
lblNewLabel_4.setBounds(10, 215, 46, 14);
panel.add(lblNewLabel_4);
btnNewButton = new JButton("Konto Erstellen");
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 13));
btnNewButton.setForeground(new Color(128, 128, 255));
btnNewButton.setBounds(10, 262, 150, 23);
panel.add(btnNewButton);
textFieldPassword = new JTextField();
textFieldPassword.setBounds(75, 120, 169, 20);
panel.add(textFieldPassword);
textFieldPassword.setColumns(10);
textFieldEmail = new JTextField();
textFieldEmail.setColumns(10);
textFieldEmail.setBounds(75, 167, 169, 20);
panel.add(textFieldEmail);
textFieldTeleN = new JTextField();
textFieldTeleN.setColumns(10);
textFieldTeleN.setBounds(75, 212, 169, 20);
panel.add(textFieldTeleN);
this.setVisible(true);
}
public void showError() {
JOptionPane.showMessageDialog(this, "Dieser Konto existiert bereits! ", "Error Nachricht", JOptionPane.ERROR_MESSAGE);
}
public void showMessage() {
JOptionPane.showMessageDialog(this, "Login war erfolgreich", "Login Erfolgreich", JOptionPane.PLAIN_MESSAGE);
}
public void setAction(ActionListener actionListener) {
btnNewButton.addActionListener(actionListener);
}
}

View File

@ -1,48 +1,39 @@
package MVC_Desighn;
// Modell, oder domain
public class Person_Daten {
private String name;
private int alter;
private String name;
public Person_Daten(String name, int alter) {
this.name = name;
setAlter(alter);
public int getAlter() {
return alter;
}
public void setAlter(int alter) {
this.alter = alter;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAlter() {
return alter;
}
public void setAlter(int alter) {
if (alter > 18)
this.alter = alter;
else
System.out.println("Sie sind sehr jung");
}
@Override
public String toString() {
return "Person_Daten [name=" + name + ", alter=" + alter + "]";
}
public static void main(String[] args) {
Person_Daten model = new Person_Daten();
Person_Daten p1 = new Person_Daten("obai" , 22);
GUI_DatenView v1 = new GUI_DatenView();
GUI_DatenView view = new GUI_DatenView();
Controller_DatenPerson c1 = new Controller_DatenPerson(p1,v1);
Controller_DatenPerson controller = new Controller_DatenPerson(model, view);
c1.updateView();
c1.setPersonName("omar");
c1.updateView();
controller.updateView();
controller.setStudentName("Vikram Sharma");
controller.updateView();
}
}
@ -51,14 +42,16 @@ public class Person_Daten {
//View oder GUI
class GUI_DatenView {
public void printDatenPerson(String name, int alter) {
System.out.println("GUI_DatenView [name=" + name + ", alter=" + alter + "]");
public void printStudentDetails(String name, int alter) {
System.out.println("Student:");
System.out.println("Name: " + name);
System.out.println("ALter: " + alter);
}
}
// Controller
class Controller_DatenPerson{
class Controller_DatenPerson {
Person_Daten modell;
GUI_DatenView view;
@ -67,23 +60,25 @@ class Controller_DatenPerson{
this.view = view;
}
public String getPersonName() {
return this.modell.getName();
}
public void setPersonName(String name) {
public void setStudentName(String name) {
modell.setName(name);
}
public int getPersonAlter() {
return this.modell.getAlter();
public String getStudentName() {
return modell.getName();
}
public void setAlter(int alter) {
modell.setAlter(alter);
}
public int getAlter() {
return modell.getAlter();
}
public void updateView() {
view.printDatenPerson(modell.getName(),modell.getAlter());
view.printStudentDetails(modell.getName(), modell.getAlter());
}
}

Binary file not shown.

View File

@ -0,0 +1,41 @@
package SQL.JavaDatenbanken;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// Lade den MySQL JDBC-Treiber
Class.forName("com.mysql.cj.jdbc.Driver");
// Verbindung zur MySQL-Datenbank herstellen
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
if (con == null) {
System.out.println("keine Verbindung!");
}
// Statement erstellen
Statement stmt = con.createStatement();
// SQL-Abfrage ausführen (richtiges Tabellennamen verwenden)
ResultSet rs = stmt.executeQuery("SELECT * FROM books");
// Ergebnisse durchgehen und ausgeben
while (rs.next()) {
System.out.print(rs.getString("title"));
System.out.print(" ");
System.out.print(rs.getString("number_of_pages"));
System.out.println();
}
// Verbindung schließen
con.close();
}
}

View File

@ -8,5 +8,6 @@ module Programmierung2 {
requires java.desktop;
requires org.junit.jupiter.api;
requires junit;
requires java.sql;
exports Übungen.TaschenrechnerGUI to junit; // Exportiere das Paket für das JUnit-Modul
}

View File

@ -1,18 +1,36 @@
package Übungen.TicTac;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*;
public class Controller {
static Scanner scan = new Scanner(System.in);
static String[][] spielField = new String[3][3];
private Modell spieler1;
private Modell spieler2;
private View view;
private String spieler = "X";
public Controller() {}
public Controller(Modell spieler1, Modell spieler2, View view) {
this.spieler1 = spieler1;
this.spieler2 = spieler2;
this.view = view;
initSpielfeld();
zeigeDaten();
}
public void zeigeDaten() {
view.nameSpieler1.setText(spieler1.getName());
view.anzahlgewinn1.setText(spieler1.getAnzahlGewinn() + "");
view.nameSpieler2.setText(spieler2.getName());
view.anzahlgewinn2.setText(spieler2.getAnzahlGewinn() + "");
view.XoderO.setText(spieler);
}
public static boolean checkSet(String X_O, int x, int y) {
@ -27,19 +45,19 @@ public class Controller {
}
public static boolean checkGewinner(String player) {
public static boolean checkGewinner(Modell player) {
for (int i = 0; i < 3; i++) {
if (spielField[i][0].equals(player) && spielField[i][1].equals(player) && spielField[i][2].equals(player))
if (spielField[i][0].equals(player.getZeichen()) && spielField[i][1].equals(player.getZeichen()) && spielField[i][2].equals(player.getZeichen()))
return true;
}
for (int i = 0; i < 3; i++) {
if (spielField[0][i].equals(player) && spielField[1][i].equals(player) && spielField[2][i].equals(player))
if (spielField[0][i].equals(player.getZeichen()) && spielField[1][i].equals(player.getZeichen()) && spielField[2][i].equals(player.getZeichen()))
return true;
}
if (spielField[0][0].equals(player) && spielField[1][1].equals(player) && spielField[2][2].equals(player))
if (spielField[0][0].equals(player.getZeichen()) && spielField[1][1].equals(player.getZeichen()) && spielField[2][2].equals(player.getZeichen()))
return true;
if (spielField[0][2].equals(player) && spielField[1][1].equals(player) && spielField[2][0].equals(player))
if (spielField[0][2].equals(player.getZeichen()) && spielField[1][1].equals(player.getZeichen()) && spielField[2][0].equals(player.getZeichen()))
return true;
return false;
@ -55,20 +73,45 @@ public class Controller {
}
}
public static void printSpielfeld() {
public void initSpielfeld() {
// Spielfeld initialisieren
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 2) {
spielField[i][j] = "-";
System.out.print(spielField[i][j]);
} else {
spielField[i][j] = "-";
System.out.print(spielField[i][j]);
System.out.print("|");
view.x_o[i][j].setText("-");
final int x = i;
final int y = j;
view.x_o[i][j].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
handleMove(x, y);
}
});
}
System.out.println();
}
}
public void handleMove(int x, int y) {
if (spielField[x][y].equals("-")) {
spielField[x][y] = spieler;
view.x_o[x][y].setText(spieler);
if (checkGewinner(spieler1)) {
System.out.println("Spieler " + spieler1.getName() + " hat gewonnen!");
spieler1.setAnzahlGewinn(spieler1.getAnzahlGewinn() + 1);
}
else if (checkGewinner(spieler2)) {
System.out.println("Spieler " + spieler2.getName() + " hat gewonnen!");
spieler2.setAnzahlGewinn(spieler2.getAnzahlGewinn() + 1);
}
spieler = spieler.equals("X") ? "O" : "X";
zeigeDaten();
}
}
}

View File

@ -3,15 +3,13 @@ package Übungen.TicTac;
public class Modell {
private String name;
private String vorname;
private int anzahlGewinn;
private String zeichen;
public Modell(String name, String vorname, int anzahlGewinn,String zeichen) {
public Modell(String name,String zeichen) {
this.name = name;
this.vorname = vorname;
this.anzahlGewinn = anzahlGewinn;
this.zeichen = zeichen;
this.anzahlGewinn = 0;
}
public String getName() {
@ -20,20 +18,18 @@ public class Modell {
public void setName(String name) {
this.name = name;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public int getAnzahlGewinn() {
return anzahlGewinn;
}
public void setAnzahlGewinn(int anzahlGewinn) {
this.anzahlGewinn = anzahlGewinn;
}
public String getZeichen() {
return zeichen;
}
public int getAnzahlGewinn() {
return anzahlGewinn;
}
public void setAnzahlGewinn(int anzahlGewinn) {
this.anzahlGewinn = anzahlGewinn;
}
}

View File

@ -0,0 +1,47 @@
package Übungen.TicTac;
import javax.swing.*;
import java.awt.*;
public class SpielfeldGUI {
private JFrame frame;
private JButton[][] buttons;
public SpielfeldGUI() {
frame = new JFrame("Spielfeld");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
// Erstelle ein 3x3 GridLayout
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 3));
// Erstelle das Spielfeld mit 3x3 Buttons
buttons = new JButton[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j] = new JButton("-");
buttons[i][j].addActionListener(e -> {
JButton clickedButton = (JButton) e.getSource();
clickedButton.setText("X"); // Ändert den Text zu "X" beim Klick
});
panel.add(buttons[i][j]);
}
}
// Füge das Panel zum Frame hinzu
frame.add(panel);
frame.setVisible(true); // Zeige das Fenster an
}
public static void main(String[] args) {
// Starte die GUI
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SpielfeldGUI();
}
});
}
}

View File

@ -5,54 +5,11 @@ public class Tic_Tac_Toe {
public static void main(String[] args){
Modell obai = new Modell("obai","X");
Modell omar = new Modell("omar","O");
//
// printSpielfeld();
// while (true) {
// System.out.println("Bitte geben Sie 'x' und neue Position ein: ");
// String playerx =scan.nextLine();
// int x = scan.nextInt();
// int y = scan.nextInt();
// while (!checkSet(playerx, x, y)) {
// scan.nextLine();
// System.out.println("Falsche Eingabe! Versuchen Sie nochmal: ");
// playerx =scan.nextLine();
// x = scan.nextInt();
// y = scan.nextInt();
// if (checkSet(playerx, x, y))
// break;
// }
// ausgabeSpielFeld(playerx, x, y);
// if (checkGewinner(playerx)) {
// System.out.println("Spieler X hat gewonnen!");
// break;
// }
// scan.nextLine();
//
// System.out.println("Bitte geben Sie 'O' und neue Position ein: ");
// String playero =scan.nextLine();
// x = scan.nextInt();
// y = scan.nextInt();
// while (!checkSet(playero, x, y)) {
// scan.nextLine();
// System.out.println("Falsche Eingabe! Versuchen Sie nochmal: ");
// playero =scan.nextLine();
// x = scan.nextInt();
// y = scan.nextInt();
// }
// ausgabeSpielFeld(playero, x, y);
// if (checkGewinner(playero)) {
// System.out.println("Spieler O hat gewonnen!");
// break;
// }
// scan.nextLine();
// }
// }
//
//
View view = new View();
Controller spiel = new Controller(obai,omar,view);
}
}

View File

@ -1,5 +1,85 @@
package Übungen.TicTac;
public class View {
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class View extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
JLabel nameSpieler1;
JLabel nameSpieler2;
JLabel anzahlgewinn1;
JLabel anzahlgewinn2;
JLabel XoderO;
JButton[][] x_o;
public View() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 626, 709);
this.contentPane = new JPanel();
this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(128, 255, 128), 2));
panel.setBackground(new Color(192, 192, 192));
panel.setBounds(38, 36, 529, 585);
contentPane.add(panel);
panel.setLayout(null);
nameSpieler1 = new JLabel();
nameSpieler1.setBounds(46, 27, 100, 14);
panel.add(nameSpieler1);
anzahlgewinn1 = new JLabel();
anzahlgewinn1.setBounds(46, 52, 100, 14);
panel.add(anzahlgewinn1);
nameSpieler2 = new JLabel();
nameSpieler2.setBounds(391, 27, 100, 14);
panel.add(nameSpieler2);
anzahlgewinn2 = new JLabel();
anzahlgewinn2.setBounds(391, 52, 100, 14);
panel.add(anzahlgewinn2);
XoderO = new JLabel("New label");
XoderO.setBounds(222, 41, 100, 14);
panel.add(XoderO);
JButton btnNewButton = new JButton("New Game");
btnNewButton.setBounds(196, 447, 129, 23);
panel.add(btnNewButton);
JPanel panel_1 = new JPanel();
panel_1.setBounds(101, 125, 312, 291);
panel.add(panel_1);
panel_1.setLayout(new GridLayout(3, 3, 0, 0));
x_o = new JButton[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
x_o[i][j] = new JButton("-");
panel_1.add(x_o[i][j]);
}
panel.add(panel_1);
this.setVisible(true);
}
}