First commit
commit
1adb295085
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
|
@ -0,0 +1 @@
|
|||
/bin/
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>KI-Projekt-Warenwirtschaftssystem</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,2 @@
|
|||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
|
@ -0,0 +1,40 @@
|
|||
public class Artikel {
|
||||
private int artikelId;
|
||||
private String bezeichnung;
|
||||
private double preis;
|
||||
|
||||
public Artikel(int artikelId, String bezeichnung, double preis) {
|
||||
this.artikelId = artikelId;
|
||||
this.bezeichnung = bezeichnung;
|
||||
this.preis = preis;
|
||||
}
|
||||
|
||||
public int getArtikelId() {
|
||||
return artikelId;
|
||||
}
|
||||
|
||||
public String getBezeichnung() {
|
||||
return bezeichnung;
|
||||
}
|
||||
|
||||
public double getPreis() {
|
||||
return preis;
|
||||
}
|
||||
|
||||
public void setArtikelId(int artikelId) {
|
||||
this.artikelId = artikelId;
|
||||
}
|
||||
|
||||
public void setBezeichnung(String bezeichnung) {
|
||||
this.bezeichnung = bezeichnung;
|
||||
}
|
||||
|
||||
public void setPreis(double preis) {
|
||||
this.preis = preis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Artikel-ID: " + artikelId + ", Bezeichnung: " + bezeichnung + ", Preis: " + preis;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
private static Stammdaten stammdaten = XMLProcessor.loadFromXML();
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
boolean running = true;
|
||||
|
||||
while (running) {
|
||||
System.out.println("1. Stammdaten anzeigen");
|
||||
System.out.println("2. Stammdaten hinzufügen");
|
||||
System.out.println("3. Artikel hinzufügen");
|
||||
System.out.println("4. Stammdaten speichern");
|
||||
System.out.println("5. Programm beenden");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine(); // Clear the buffer
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
displayStammdaten();
|
||||
break;
|
||||
case 2:
|
||||
addStammdaten(scanner);
|
||||
break;
|
||||
case 3:
|
||||
addArtikel(scanner);
|
||||
break;
|
||||
case 4:
|
||||
XMLProcessor.saveToXML(stammdaten);
|
||||
break;
|
||||
case 5:
|
||||
running = false;
|
||||
break;
|
||||
default:
|
||||
System.out.println("Ungültige Wahl, bitte erneut versuchen.");
|
||||
}
|
||||
}
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
private static void displayStammdaten() {
|
||||
System.out.println("Personen:");
|
||||
for (Person person : stammdaten.getPersonen()) {
|
||||
System.out.println(person);
|
||||
}
|
||||
System.out.println("Artikel:");
|
||||
for (Artikel artikel : stammdaten.getArtikel()) {
|
||||
System.out.println(artikel);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addStammdaten(Scanner scanner) {
|
||||
System.out.print("ID: ");
|
||||
int id = scanner.nextInt();
|
||||
scanner.nextLine(); // Clear the buffer
|
||||
|
||||
System.out.print("Name: ");
|
||||
String name = scanner.nextLine();
|
||||
|
||||
System.out.print("Alter: ");
|
||||
int alter = scanner.nextInt();
|
||||
scanner.nextLine(); // Clear the buffer
|
||||
|
||||
stammdaten.getPersonen().add(new Person(id, name, alter));
|
||||
}
|
||||
|
||||
private static void addArtikel(Scanner scanner) {
|
||||
System.out.print("Artikel-ID: ");
|
||||
int artikelId = scanner.nextInt();
|
||||
scanner.nextLine(); // Clear the buffer
|
||||
|
||||
System.out.print("Bezeichnung: ");
|
||||
String bezeichnung = scanner.nextLine();
|
||||
|
||||
System.out.print("Preis: ");
|
||||
double preis = scanner.nextDouble();
|
||||
scanner.nextLine(); // Clear the buffer
|
||||
|
||||
stammdaten.getArtikel().add(new Artikel(artikelId, bezeichnung, preis));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
public class Person {
|
||||
private int id;
|
||||
private String name;
|
||||
private int alter;
|
||||
|
||||
public Person(int id, String name, int alter) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.alter = alter;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAlter() {
|
||||
return alter;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setAlter(int alter) {
|
||||
this.alter = alter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ID: " + id + ", Name: " + name + ", Alter: " + alter;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Stammdaten {
|
||||
private List<Person> personen = new ArrayList<>();
|
||||
private List<Artikel> artikel = new ArrayList<>();
|
||||
|
||||
public List<Person> getPersonen() {
|
||||
return personen;
|
||||
}
|
||||
|
||||
public void setPersonen(List<Person> personen) {
|
||||
this.personen = personen;
|
||||
}
|
||||
|
||||
public List<Artikel> getArtikel() {
|
||||
return artikel;
|
||||
}
|
||||
|
||||
public void setArtikel(List<Artikel> artikel) {
|
||||
this.artikel = artikel;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
import org.w3c.dom.*;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class XMLProcessor {
|
||||
|
||||
private static final String XML_FILE = "stammdaten.xml";
|
||||
|
||||
public static Stammdaten loadFromXML() {
|
||||
Stammdaten stammdaten = new Stammdaten();
|
||||
List<Person> personen = new ArrayList<>();
|
||||
List<Artikel> artikel = new ArrayList<>();
|
||||
try {
|
||||
File file = new File(XML_FILE);
|
||||
if (!file.exists()) {
|
||||
return stammdaten; // Return empty stammdaten if file doesn't exist
|
||||
}
|
||||
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
Document doc = dBuilder.parse(file);
|
||||
doc.getDocumentElement().normalize();
|
||||
|
||||
NodeList personList = doc.getElementsByTagName("person");
|
||||
for (int temp = 0; temp < personList.getLength(); temp++) {
|
||||
Node nNode = personList.item(temp);
|
||||
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element eElement = (Element) nNode;
|
||||
int id = Integer.parseInt(eElement.getElementsByTagName("id").item(0).getTextContent());
|
||||
String name = eElement.getElementsByTagName("name").item(0).getTextContent();
|
||||
int alter = Integer.parseInt(eElement.getElementsByTagName("alter").item(0).getTextContent());
|
||||
personen.add(new Person(id, name, alter));
|
||||
}
|
||||
}
|
||||
|
||||
NodeList artikelList = doc.getElementsByTagName("artikel");
|
||||
for (int temp = 0; temp < artikelList.getLength(); temp++) {
|
||||
Node nNode = artikelList.item(temp);
|
||||
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element eElement = (Element) nNode;
|
||||
int artikelId = Integer.parseInt(eElement.getElementsByTagName("artikelId").item(0).getTextContent());
|
||||
String bezeichnung = eElement.getElementsByTagName("bezeichnung").item(0).getTextContent();
|
||||
double preis = Double.parseDouble(eElement.getElementsByTagName("preis").item(0).getTextContent());
|
||||
artikel.add(new Artikel(artikelId, bezeichnung, preis));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
stammdaten.setPersonen(personen);
|
||||
stammdaten.setArtikel(artikel);
|
||||
return stammdaten;
|
||||
}
|
||||
|
||||
public static void saveToXML(Stammdaten stammdaten) {
|
||||
try {
|
||||
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
||||
|
||||
// root elements
|
||||
Document doc = docBuilder.newDocument();
|
||||
Element rootElement = doc.createElement("stammdaten");
|
||||
doc.appendChild(rootElement);
|
||||
|
||||
for (Person person : stammdaten.getPersonen()) {
|
||||
Element personElement = doc.createElement("person");
|
||||
rootElement.appendChild(personElement);
|
||||
|
||||
Element id = doc.createElement("id");
|
||||
id.appendChild(doc.createTextNode(String.valueOf(person.getId())));
|
||||
personElement.appendChild(id);
|
||||
|
||||
Element name = doc.createElement("name");
|
||||
name.appendChild(doc.createTextNode(person.getName()));
|
||||
personElement.appendChild(name);
|
||||
|
||||
Element alter = doc.createElement("alter");
|
||||
alter.appendChild(doc.createTextNode(String.valueOf(person.getAlter())));
|
||||
personElement.appendChild(alter);
|
||||
}
|
||||
|
||||
for (Artikel artikel : stammdaten.getArtikel()) {
|
||||
Element artikelElement = doc.createElement("artikel");
|
||||
rootElement.appendChild(artikelElement);
|
||||
|
||||
Element artikelId = doc.createElement("artikelId");
|
||||
artikelId.appendChild(doc.createTextNode(String.valueOf(artikel.getArtikelId())));
|
||||
artikelElement.appendChild(artikelId);
|
||||
|
||||
Element bezeichnung = doc.createElement("bezeichnung");
|
||||
bezeichnung.appendChild(doc.createTextNode(artikel.getBezeichnung()));
|
||||
artikelElement.appendChild(bezeichnung);
|
||||
|
||||
Element preis = doc.createElement("preis");
|
||||
preis.appendChild(doc.createTextNode(String.valueOf(artikel.getPreis())));
|
||||
artikelElement.appendChild(preis);
|
||||
}
|
||||
|
||||
// write the content into xml file
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMSource source = new DOMSource(doc);
|
||||
StreamResult result = new StreamResult(new File(XML_FILE));
|
||||
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.transform(source, result);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue