commit e09eabe9b42f68d5b4ce016545283c96d002eb18 Author: elifaltinisik Date: Wed Jun 12 15:52:06 2024 +0200 Projekt diff --git a/Fakturama/.classpath b/Fakturama/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/Fakturama/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/Fakturama/.gitignore b/Fakturama/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/Fakturama/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/Fakturama/.project b/Fakturama/.project new file mode 100644 index 0000000..a528364 --- /dev/null +++ b/Fakturama/.project @@ -0,0 +1,17 @@ + + + Fakturama + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Fakturama/.settings/org.eclipse.core.resources.prefs b/Fakturama/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/Fakturama/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/Fakturama/.settings/org.eclipse.jdt.core.prefs b/Fakturama/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/Fakturama/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/Fakturama/src/Fakturama/Kunde.java b/Fakturama/src/Fakturama/Kunde.java new file mode 100644 index 0000000..c183bad --- /dev/null +++ b/Fakturama/src/Fakturama/Kunde.java @@ -0,0 +1,166 @@ +package Fakturama; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; +import java.io.File; +import java.io.IOException; + +public class Kunde { + private String name; + private String anschrift; + private String email; + private String telefonnummer; + + // Konstruktor + public Kunde(String name, String anschrift, String email, String telefonnummer) { + this.name = name; + this.anschrift = anschrift; + this.email = email; + this.telefonnummer = telefonnummer; + } + + // Leerer Konstruktor + public Kunde() { + } + + // Getter und Setter + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAnschrift() { + return anschrift; + } + + public void setAnschrift(String anschrift) { + this.anschrift = anschrift; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getTelefonnummer() { + return telefonnummer; + } + + public void setTelefonnummer(String telefonnummer) { + this.telefonnummer = telefonnummer; + } + + // Methode zum Anzeigen der Kundendaten + public void anzeigen() { + System.out.println("Kunde: " + name); + System.out.println("Anschrift: " + anschrift); + System.out.println("Email: " + email); + System.out.println("Telefonnummer: " + telefonnummer); + } + + // Methode zum Speichern der Kundendaten im XML-Format + public void speichereAlsXML(String dateiPfad) { + try { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.newDocument(); + + // Root-Element + Element rootElement = doc.createElement("Kunde"); + doc.appendChild(rootElement); + + // Name-Element + Element nameElement = doc.createElement("Name"); + nameElement.appendChild(doc.createTextNode(name)); + rootElement.appendChild(nameElement); + + // Anschrift-Element + Element anschriftElement = doc.createElement("Anschrift"); + anschriftElement.appendChild(doc.createTextNode(anschrift)); + rootElement.appendChild(anschriftElement); + + // Email-Element + Element emailElement = doc.createElement("Email"); + emailElement.appendChild(doc.createTextNode(email)); + rootElement.appendChild(emailElement); + + // Telefonnummer-Element + Element telefonnummerElement = doc.createElement("Telefonnummer"); + telefonnummerElement.appendChild(doc.createTextNode(telefonnummer)); + rootElement.appendChild(telefonnummerElement); + + // Transformieren und Speichern in Datei + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(new File(dateiPfad)); + transformer.transform(source, result); + + System.out.println("Die XML-Datei wurde erfolgreich gespeichert."); + + } catch (ParserConfigurationException | TransformerException e) { + e.printStackTrace(); + } + } + + // Methode zum Auslesen der Kundendaten aus einer XML-Datei + public void leseAusXML(String dateiPfad) { + try { + File xmlDatei = new File(dateiPfad); + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(xmlDatei); + doc.getDocumentElement().normalize(); + + NodeList nList = doc.getElementsByTagName("Kunde"); + + for (int temp = 0; temp < nList.getLength(); temp++) { + Node nNode = nList.item(temp); + + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + + this.name = eElement.getElementsByTagName("Name").item(0).getTextContent(); + this.anschrift = eElement.getElementsByTagName("Anschrift").item(0).getTextContent(); + this.email = eElement.getElementsByTagName("Email").item(0).getTextContent(); + this.telefonnummer = eElement.getElementsByTagName("Telefonnummer").item(0).getTextContent(); + } + } + + System.out.println("Die XML-Datei wurde erfolgreich gelesen."); + } catch (ParserConfigurationException | SAXException | IOException e) { + e.printStackTrace(); + } + } + + // Methode zum Auslesen der Kundendaten aus einer XML-Datei und in der Konsole ausgeben + public void leseUndGibAusXML(String dateiPfad) { + leseAusXML(dateiPfad); + anzeigen(); + } + + // Hauptprogramm zum Testen der Klasse + public static void main(String[] args) { + Kunde kunde = new Kunde("Max Mustermann", "Musterstraße 5555, 12345 Musterstadt", "max@mustermann.de", "0123456789"); + kunde.speichereAlsXML("kunde.xml"); + } +} diff --git a/Fakturama/src/Fakturama/Test.java b/Fakturama/src/Fakturama/Test.java new file mode 100644 index 0000000..bec5e41 --- /dev/null +++ b/Fakturama/src/Fakturama/Test.java @@ -0,0 +1,12 @@ +package Fakturama; + +public class Test { + + public static void main(String[] args) { + + Kunde neuerKunde = new Kunde(); + neuerKunde.leseUndGibAusXML("kunde.xml"); + + } + +} \ No newline at end of file