Layout Verfeinerung und einfügen des Zurück Buttons

master
athar 2024-06-30 21:06:59 +02:00
parent 61c4f47588
commit 42453b825f
3 changed files with 193 additions and 96 deletions

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Fakturama>
<Customers>
<Customer>
<Name>Athar</Name>
<Address>Adam</Address>
<ContactPerson>Ali</ContactPerson>
<TaxExempt>false</TaxExempt>
<SmallBusiness>false</SmallBusiness>
</Customer>
</Customers>
<Articles>
<Article>
<ID>1</ID>
<Unit>Milch</Unit>
<Description>1</Description>
<NetPrice>2.0</NetPrice>
<VatRate>3.0</VatRate>
</Article>
</Articles>
<Offers>
<Offer>
<ID>1</ID>
<CustomerName>Athar</CustomerName>
<Date>23.05.2024</Date>
<Status>Rechnung erstellt</Status>
<Articles>
<Article>
<ID>1</ID>
</Article>
</Articles>
</Offer>
</Offers>
<OrderConfirmations>
<OrderConfirmation>
<ID>1</ID>
<OfferID>1</OfferID>
<Date>24.05.2023</Date>
<Status>Rechnung erstellt</Status>
</OrderConfirmation>
</OrderConfirmations>
<DeliveryNotes>
<DeliveryNote>
<ID>1</ID>
<OrderConfirmationID>1</OrderConfirmationID>
<Date>23.05.2023</Date>
<Status>Rechnung erstellt</Status>
</DeliveryNote>
</DeliveryNotes>
<Invoices>
<Invoice>
<ID>1</ID>
<DeliveryNoteID>1</DeliveryNoteID>
<Date>23.05.2023</Date>
<Status>Rechnung erstellt</Status>
</Invoice>
</Invoices>
</Fakturama>

View File

@ -1,10 +1,13 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class FakturamaGUI { public class FakturamaGUI {
private JFrame frame; private JFrame frame;
private JPanel mainPanel;
private CardLayout cardLayout;
private JTextArea textArea; private JTextArea textArea;
public FakturamaGUI() { public FakturamaGUI() {
@ -15,7 +18,17 @@ public class FakturamaGUI {
frame = new JFrame("Fakturama"); frame = new JFrame("Fakturama");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600); frame.setSize(800, 600);
frame.setLayout(new BorderLayout());
cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
// Startseite
JPanel homePanel = new JPanel(new BorderLayout());
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
homePanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(homePanel, "Home");
// Menüleiste // Menüleiste
JMenuBar menuBar = new JMenuBar(); JMenuBar menuBar = new JMenuBar();
@ -53,6 +66,7 @@ public class FakturamaGUI {
saveDataItem.addActionListener(e -> { saveDataItem.addActionListener(e -> {
Main.saveData(); Main.saveData();
updateTextArea(); updateTextArea();
cardLayout.show(mainPanel, "Home");
}); });
JMenuItem loadDataItem = new JMenuItem("Daten laden"); JMenuItem loadDataItem = new JMenuItem("Daten laden");
@ -60,23 +74,20 @@ public class FakturamaGUI {
loadDataItem.addActionListener(e -> { loadDataItem.addActionListener(e -> {
Main.loadData(); Main.loadData();
updateTextArea(); updateTextArea();
cardLayout.show(mainPanel, "Home");
}); });
JMenuItem exitItem = new JMenuItem("Beenden"); JMenuItem exitItem = new JMenuItem("Beenden");
menu.add(exitItem); menu.add(exitItem);
exitItem.addActionListener(e -> System.exit(0)); exitItem.addActionListener(e -> System.exit(0));
textArea = new JTextArea(); frame.add(mainPanel);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true); frame.setVisible(true);
updateTextArea(); updateTextArea();
} }
private void showAddCustomerPanel() { private void showAddCustomerPanel() {
JPanel panel = new JPanel(new GridLayout(6, 2)); JPanel panel = new JPanel(new GridLayout(7, 2));
JLabel nameLabel = new JLabel("Name:"); JLabel nameLabel = new JLabel("Name:");
JTextField nameField = new JTextField(); JTextField nameField = new JTextField();
JLabel addressLabel = new JLabel("Adresse:"); JLabel addressLabel = new JLabel("Adresse:");
@ -98,11 +109,11 @@ public class FakturamaGUI {
boolean smallBusiness = smallBusinessBox.isSelected(); boolean smallBusiness = smallBusinessBox.isSelected();
Main.customers.add(new Main.Customer(name, address, contactPerson, taxExempt, smallBusiness)); Main.customers.add(new Main.Customer(name, address, contactPerson, taxExempt, smallBusiness));
updateTextArea(); updateTextArea();
cardLayout.show(mainPanel, "Home");
}); });
JButton backButton = new JButton("Zurück"); JButton backButton = new JButton("Zurück");
backButton.setBackground(new Color(255, 182, 193)); // Hellrot backButton.addActionListener(e -> cardLayout.show(mainPanel, "Home"));
backButton.addActionListener(e -> updateTextArea());
panel.add(nameLabel); panel.add(nameLabel);
panel.add(nameField); panel.add(nameField);
@ -114,14 +125,15 @@ public class FakturamaGUI {
panel.add(taxExemptBox); panel.add(taxExemptBox);
panel.add(smallBusinessLabel); panel.add(smallBusinessLabel);
panel.add(smallBusinessBox); panel.add(smallBusinessBox);
panel.add(addButton);
panel.add(backButton); panel.add(backButton);
panel.add(addButton);
showPanel(panel); mainPanel.add(panel, "AddCustomer");
cardLayout.show(mainPanel, "AddCustomer");
} }
private void showAddArticlePanel() { private void showAddArticlePanel() {
JPanel panel = new JPanel(new GridLayout(5, 2)); JPanel panel = new JPanel(new GridLayout(6, 2));
JLabel unitLabel = new JLabel("Einheit:"); JLabel unitLabel = new JLabel("Einheit:");
JTextField unitField = new JTextField(); JTextField unitField = new JTextField();
JLabel descriptionLabel = new JLabel("Bezeichnung:"); JLabel descriptionLabel = new JLabel("Bezeichnung:");
@ -129,7 +141,7 @@ public class FakturamaGUI {
JLabel netPriceLabel = new JLabel("Nettopreis:"); JLabel netPriceLabel = new JLabel("Nettopreis:");
JTextField netPriceField = new JTextField(); JTextField netPriceField = new JTextField();
JLabel vatRateLabel = new JLabel("Mehrwertsteuersatz:"); JLabel vatRateLabel = new JLabel("Mehrwertsteuersatz:");
JTextField vatRateField = new JTextField(); JComboBox<Double> vatRateBox = new JComboBox<>(new Double[]{7.0, 19.0});
JButton addButton = new JButton("Hinzufügen"); JButton addButton = new JButton("Hinzufügen");
addButton.setBackground(new Color(144, 238, 144)); // Hellgrün addButton.setBackground(new Color(144, 238, 144)); // Hellgrün
@ -137,14 +149,14 @@ public class FakturamaGUI {
String unit = unitField.getText(); String unit = unitField.getText();
String description = descriptionField.getText(); String description = descriptionField.getText();
double netPrice = Double.parseDouble(netPriceField.getText()); double netPrice = Double.parseDouble(netPriceField.getText());
double vatRate = Double.parseDouble(vatRateField.getText()); double vatRate = (Double) vatRateBox.getSelectedItem();
Main.articles.add(new Main.Article(unit, description, netPrice, vatRate)); Main.articles.add(new Main.Article(unit, description, netPrice, vatRate));
updateTextArea(); updateTextArea();
cardLayout.show(mainPanel, "Home");
}); });
JButton backButton = new JButton("Zurück"); JButton backButton = new JButton("Zurück");
backButton.setBackground(new Color(255, 182, 193)); // Hellrot backButton.addActionListener(e -> cardLayout.show(mainPanel, "Home"));
backButton.addActionListener(e -> updateTextArea());
panel.add(unitLabel); panel.add(unitLabel);
panel.add(unitField); panel.add(unitField);
@ -153,36 +165,42 @@ public class FakturamaGUI {
panel.add(netPriceLabel); panel.add(netPriceLabel);
panel.add(netPriceField); panel.add(netPriceField);
panel.add(vatRateLabel); panel.add(vatRateLabel);
panel.add(vatRateField); panel.add(vatRateBox);
panel.add(addButton);
panel.add(backButton); panel.add(backButton);
panel.add(addButton);
showPanel(panel); mainPanel.add(panel, "AddArticle");
cardLayout.show(mainPanel, "AddArticle");
} }
private void showCreateOfferPanel() { private void showCreateOfferPanel() {
JPanel panel = new JPanel(new BorderLayout()); JPanel panel = new JPanel(new BorderLayout());
JPanel formPanel = new JPanel(new GridLayout(4, 2));
JPanel formPanel = new JPanel(new GridLayout(4, 2));
JLabel customerLabel = new JLabel("Kunde:"); JLabel customerLabel = new JLabel("Kunde:");
JComboBox<String> customerBox = new JComboBox<>(); JComboBox<String> customerBox = new JComboBox<>();
for (Main.Customer customer : Main.customers) { for (Main.Customer customer : Main.customers) {
customerBox.addItem(customer.name); customerBox.addItem(customer.name);
} }
formPanel.add(customerLabel);
formPanel.add(customerBox);
JLabel dateLabel = new JLabel("Datum:"); JLabel dateLabel = new JLabel("Datum:");
JTextField dateField = new JTextField(); JTextField dateField = new JTextField();
formPanel.add(customerLabel);
formPanel.add(customerBox);
formPanel.add(dateLabel); formPanel.add(dateLabel);
formPanel.add(dateField); formPanel.add(dateField);
panel.add(formPanel, BorderLayout.NORTH);
JLabel articlesLabel = new JLabel("Artikel:"); JLabel articlesLabel = new JLabel("Artikel:");
JList<String> articlesList = new JList<>(Main.articles.stream().map(a -> a.description).toArray(String[]::new)); JList<String> articlesList = new JList<>(Main.articles.stream().map(a -> a.description).toArray(String[]::new));
articlesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); articlesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
JScrollPane articlesScrollPane = new JScrollPane(articlesList); JScrollPane articlesScrollPane = new JScrollPane(articlesList);
panel.add(articlesLabel, BorderLayout.CENTER);
panel.add(articlesScrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton addButton = new JButton("Erstellen"); JButton addButton = new JButton("Erstellen");
addButton.setBackground(new Color(144, 238, 144)); // Hellgrün addButton.setBackground(new Color(144, 238, 144)); // Hellgrün
addButton.addActionListener(e -> { addButton.addActionListener(e -> {
@ -196,26 +214,24 @@ public class FakturamaGUI {
String date = dateField.getText(); String date = dateField.getText();
Main.offers.add(new Main.Offer(Main.nextOfferId++, customer, selectedArticles, date, "Angebot erstellt")); Main.offers.add(new Main.Offer(Main.nextOfferId++, customer, selectedArticles, date, "Angebot erstellt"));
updateTextArea(); updateTextArea();
cardLayout.show(mainPanel, "Home");
}); });
JButton backButton = new JButton("Zurück"); JButton backButton = new JButton("Zurück");
backButton.setBackground(new Color(255, 182, 193)); // Hellrot backButton.addActionListener(e -> cardLayout.show(mainPanel, "Home"));
backButton.addActionListener(e -> updateTextArea());
JPanel buttonPanel = new JPanel(new GridLayout(1, 2));
buttonPanel.add(addButton);
buttonPanel.add(backButton); buttonPanel.add(backButton);
buttonPanel.add(addButton);
panel.add(formPanel, BorderLayout.NORTH);
panel.add(articlesLabel, BorderLayout.CENTER);
panel.add(articlesScrollPane, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH); panel.add(buttonPanel, BorderLayout.SOUTH);
showPanel(panel); mainPanel.add(panel, "CreateOffer");
cardLayout.show(mainPanel, "CreateOffer");
} }
private void showCreateOrderConfirmationPanel() { private void showCreateOrderConfirmationPanel() {
JPanel panel = new JPanel(new GridLayout(4, 2)); JPanel panel = new JPanel(new GridLayout(6, 2));
JLabel offerLabel = new JLabel("Angebot:"); JLabel offerLabel = new JLabel("Angebot:");
JComboBox<String> offerBox = new JComboBox<>(); JComboBox<String> offerBox = new JComboBox<>();
for (Main.Offer offer : Main.offers) { for (Main.Offer offer : Main.offers) {
@ -234,24 +250,27 @@ public class FakturamaGUI {
Main.orderConfirmations.add(new Main.OrderConfirmation(Main.nextOrderConfirmationId++, selectedOffer, date, "Auftragsbestätigung erstellt")); Main.orderConfirmations.add(new Main.OrderConfirmation(Main.nextOrderConfirmationId++, selectedOffer, date, "Auftragsbestätigung erstellt"));
selectedOffer.status = "Auftragsbestätigung erstellt"; selectedOffer.status = "Auftragsbestätigung erstellt";
updateTextArea(); updateTextArea();
cardLayout.show(mainPanel, "Home");
}); });
JButton backButton = new JButton("Zurück"); JButton backButton = new JButton("Zurück");
backButton.setBackground(new Color(255, 182, 193)); // Hellrot backButton.addActionListener(e -> cardLayout.show(mainPanel, "Home"));
backButton.addActionListener(e -> updateTextArea());
panel.add(offerLabel); panel.add(offerLabel);
panel.add(offerBox); panel.add(offerBox);
panel.add(dateLabel); panel.add(dateLabel);
panel.add(dateField); panel.add(dateField);
panel.add(addButton); panel.add(new JLabel());
panel.add(new JLabel());
panel.add(backButton); panel.add(backButton);
panel.add(addButton);
showPanel(panel); mainPanel.add(panel, "CreateOrderConfirmation");
cardLayout.show(mainPanel, "CreateOrderConfirmation");
} }
private void showCreateDeliveryNotePanel() { private void showCreateDeliveryNotePanel() {
JPanel panel = new JPanel(new GridLayout(4, 2)); JPanel panel = new JPanel(new GridLayout(6, 2));
JLabel orderConfirmationLabel = new JLabel("Auftragsbestätigung:"); JLabel orderConfirmationLabel = new JLabel("Auftragsbestätigung:");
JComboBox<String> orderConfirmationBox = new JComboBox<>(); JComboBox<String> orderConfirmationBox = new JComboBox<>();
for (Main.OrderConfirmation orderConfirmation : Main.orderConfirmations) { for (Main.OrderConfirmation orderConfirmation : Main.orderConfirmations) {
@ -271,24 +290,27 @@ public class FakturamaGUI {
selectedOrderConfirmation.status = "Lieferschein erstellt"; selectedOrderConfirmation.status = "Lieferschein erstellt";
selectedOrderConfirmation.offer.status = "Lieferschein erstellt"; selectedOrderConfirmation.offer.status = "Lieferschein erstellt";
updateTextArea(); updateTextArea();
cardLayout.show(mainPanel, "Home");
}); });
JButton backButton = new JButton("Zurück"); JButton backButton = new JButton("Zurück");
backButton.setBackground(new Color(255, 182, 193)); // Hellrot backButton.addActionListener(e -> cardLayout.show(mainPanel, "Home"));
backButton.addActionListener(e -> updateTextArea());
panel.add(orderConfirmationLabel); panel.add(orderConfirmationLabel);
panel.add(orderConfirmationBox); panel.add(orderConfirmationBox);
panel.add(dateLabel); panel.add(dateLabel);
panel.add(dateField); panel.add(dateField);
panel.add(addButton); panel.add(new JLabel());
panel.add(new JLabel());
panel.add(backButton); panel.add(backButton);
panel.add(addButton);
showPanel(panel); mainPanel.add(panel, "CreateDeliveryNote");
cardLayout.show(mainPanel, "CreateDeliveryNote");
} }
private void showCreateInvoicePanel() { private void showCreateInvoicePanel() {
JPanel panel = new JPanel(new GridLayout(4, 2)); JPanel panel = new JPanel(new GridLayout(6, 2));
JLabel deliveryNoteLabel = new JLabel("Lieferschein:"); JLabel deliveryNoteLabel = new JLabel("Lieferschein:");
JComboBox<String> deliveryNoteBox = new JComboBox<>(); JComboBox<String> deliveryNoteBox = new JComboBox<>();
for (Main.DeliveryNote deliveryNote : Main.deliveryNotes) { for (Main.DeliveryNote deliveryNote : Main.deliveryNotes) {
@ -309,34 +331,23 @@ public class FakturamaGUI {
selectedDeliveryNote.orderConfirmation.status = "Rechnung erstellt"; selectedDeliveryNote.orderConfirmation.status = "Rechnung erstellt";
selectedDeliveryNote.orderConfirmation.offer.status = "Rechnung erstellt"; selectedDeliveryNote.orderConfirmation.offer.status = "Rechnung erstellt";
updateTextArea(); updateTextArea();
cardLayout.show(mainPanel, "Home");
}); });
JButton backButton = new JButton("Zurück"); JButton backButton = new JButton("Zurück");
backButton.setBackground(new Color(255, 182, 193)); // Hellrot backButton.addActionListener(e -> cardLayout.show(mainPanel, "Home"));
backButton.addActionListener(e -> updateTextArea());
panel.add(deliveryNoteLabel); panel.add(deliveryNoteLabel);
panel.add(deliveryNoteBox); panel.add(deliveryNoteBox);
panel.add(dateLabel); panel.add(dateLabel);
panel.add(dateField); panel.add(dateField);
panel.add(addButton); panel.add(new JLabel());
panel.add(new JLabel());
panel.add(backButton); panel.add(backButton);
panel.add(addButton);
showPanel(panel); mainPanel.add(panel, "CreateInvoice");
} cardLayout.show(mainPanel, "CreateInvoice");
private void showPanel(JPanel panel) {
frame.getContentPane().removeAll();
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
}
private void showPanel(JScrollPane scrollPane) {
frame.getContentPane().removeAll();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
} }
private void updateTextArea() { private void updateTextArea() {
@ -347,7 +358,7 @@ public class FakturamaGUI {
} }
sb.append("\nArtikel:\n"); sb.append("\nArtikel:\n");
for (Main.Article article : Main.articles) { for (Main.Article article : Main.articles) {
sb.append(article.description).append(" - ").append(article.unit).append(" - ").append(article.netPrice).append(" € - ").append(article.vatRate).append(" %").append("\n"); sb.append(article.id).append(": ").append(article.description).append(" - ").append(article.unit).append(" - ").append(article.netPrice).append(" € - ").append(article.vatRate).append(" %").append("\n");
} }
sb.append("\nAngebote:\n"); sb.append("\nAngebote:\n");
for (Main.Offer offer : Main.offers) { for (Main.Offer offer : Main.offers) {
@ -369,7 +380,6 @@ public class FakturamaGUI {
sb.append("Rechnung #").append(invoice.id).append(" - ").append(invoice.deliveryNote.orderConfirmation.offer.customer.name).append(" - ").append(invoice.date).append(" - ").append(invoice.status).append("\n"); sb.append("Rechnung #").append(invoice.id).append(" - ").append(invoice.deliveryNote.orderConfirmation.offer.customer.name).append(" - ").append(invoice.date).append(" - ").append(invoice.status).append("\n");
} }
textArea.setText(sb.toString()); textArea.setText(sb.toString());
showPanel(new JScrollPane(textArea));
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -665,14 +665,14 @@ public class Main {
for (int i = 0; i < customerList.getLength(); i++) { for (int i = 0; i < customerList.getLength(); i++) {
Node customerNode = customerList.item(i); Node customerNode = customerList.item(i);
if (customerNode.getNodeType() == Node.ELEMENT_NODE) { if (customerNode != null && customerNode.getNodeType() == Node.ELEMENT_NODE) {
Element customerElement = (Element) customerNode; Element customerElement = (Element) customerNode;
String name = customerElement.getElementsByTagName("Name").item(0).getTextContent(); String name = getElementTextContent(customerElement, "Name");
String address = customerElement.getElementsByTagName("Address").item(0).getTextContent(); String address = getElementTextContent(customerElement, "Address");
String contactPerson = customerElement.getElementsByTagName("ContactPerson").item(0).getTextContent(); String contactPerson = getElementTextContent(customerElement, "ContactPerson");
boolean taxExempt = Boolean.parseBoolean(customerElement.getElementsByTagName("TaxExempt").item(0).getTextContent()); boolean taxExempt = Boolean.parseBoolean(getElementTextContent(customerElement, "TaxExempt"));
boolean smallBusiness = Boolean.parseBoolean(customerElement.getElementsByTagName("SmallBusiness").item(0).getTextContent()); boolean smallBusiness = Boolean.parseBoolean(getElementTextContent(customerElement, "SmallBusiness"));
customers.add(new Customer(name, address, contactPerson, taxExempt, smallBusiness)); customers.add(new Customer(name, address, contactPerson, taxExempt, smallBusiness));
} }
@ -684,14 +684,14 @@ public class Main {
for (int i = 0; i < articleList.getLength(); i++) { for (int i = 0; i < articleList.getLength(); i++) {
Node articleNode = articleList.item(i); Node articleNode = articleList.item(i);
if (articleNode.getNodeType() == Node.ELEMENT_NODE) { if (articleNode != null && articleNode.getNodeType() == Node.ELEMENT_NODE) {
Element articleElement = (Element) articleNode; Element articleElement = (Element) articleNode;
int id = Integer.parseInt(articleElement.getElementsByTagName("ID").item(0).getTextContent()); int id = parseIntOrDefault(getElementTextContent(articleElement, "ID"), -1);
String unit = articleElement.getElementsByTagName("Unit").item(0).getTextContent(); String unit = getElementTextContent(articleElement, "Unit");
String description = articleElement.getElementsByTagName("Description").item(0).getTextContent(); String description = getElementTextContent(articleElement, "Description");
double netPrice = Double.parseDouble(articleElement.getElementsByTagName("NetPrice").item(0).getTextContent()); double netPrice = parseDoubleOrDefault(getElementTextContent(articleElement, "NetPrice"), 0.0);
double vatRate = Double.parseDouble(articleElement.getElementsByTagName("VatRate").item(0).getTextContent()); double vatRate = parseDoubleOrDefault(getElementTextContent(articleElement, "VatRate"), 0.0);
Article article = new Article(unit, description, netPrice, vatRate); Article article = new Article(unit, description, netPrice, vatRate);
article.id = id; // Set the ID explicitly article.id = id; // Set the ID explicitly
@ -705,15 +705,15 @@ public class Main {
for (int i = 0; i < offerList.getLength(); i++) { for (int i = 0; i < offerList.getLength(); i++) {
Node offerNode = offerList.item(i); Node offerNode = offerList.item(i);
if (offerNode.getNodeType() == Node.ELEMENT_NODE) { if (offerNode != null && offerNode.getNodeType() == Node.ELEMENT_NODE) {
Element offerElement = (Element) offerNode; Element offerElement = (Element) offerNode;
int id = Integer.parseInt(offerElement.getElementsByTagName("ID").item(0).getTextContent()); int id = parseIntOrDefault(getElementTextContent(offerElement, "ID"), -1);
String customerName = offerElement.getElementsByTagName("CustomerName").item(0).getTextContent(); String customerName = getElementTextContent(offerElement, "CustomerName");
Customer offerCustomer = customers.stream().filter(c -> c.name.equals(customerName)).findFirst().orElse(null); Customer offerCustomer = customers.stream().filter(c -> c.name.equals(customerName)).findFirst().orElse(null);
String date = offerElement.getElementsByTagName("Date").item(0).getTextContent(); String date = getElementTextContent(offerElement, "Date");
String status = offerElement.getElementsByTagName("Status").item(0).getTextContent(); String status = getElementTextContent(offerElement, "Status");
NodeList offerArticles = offerElement.getElementsByTagName("Article"); NodeList offerArticles = offerElement.getElementsByTagName("Article");
List<Article> offerArticleList = new ArrayList<>(); List<Article> offerArticleList = new ArrayList<>();
@ -721,10 +721,10 @@ public class Main {
for (int j = 0; j < offerArticles.getLength(); j++) { for (int j = 0; j < offerArticles.getLength(); j++) {
Node offerArticleNode = offerArticles.item(j); Node offerArticleNode = offerArticles.item(j);
if (offerArticleNode.getNodeType() == Node.ELEMENT_NODE) { if (offerArticleNode != null && offerArticleNode.getNodeType() == Node.ELEMENT_NODE) {
Element offerArticleElement = (Element) offerArticleNode; Element offerArticleElement = (Element) offerArticleNode;
int articleId = Integer.parseInt(offerArticleElement.getElementsByTagName("ID").item(0).getTextContent()); int articleId = parseIntOrDefault(getElementTextContent(offerArticleElement, "ID"), -1);
Article offerArticle = articles.stream().filter(a -> a.id == articleId).findFirst().orElse(null); Article offerArticle = articles.stream().filter(a -> a.id == articleId).findFirst().orElse(null);
if (offerArticle != null) { if (offerArticle != null) {
@ -745,15 +745,15 @@ public class Main {
for (int i = 0; i < orderConfirmationList.getLength(); i++) { for (int i = 0; i < orderConfirmationList.getLength(); i++) {
Node orderConfirmationNode = orderConfirmationList.item(i); Node orderConfirmationNode = orderConfirmationList.item(i);
if (orderConfirmationNode.getNodeType() == Node.ELEMENT_NODE) { if (orderConfirmationNode != null && orderConfirmationNode.getNodeType() == Node.ELEMENT_NODE) {
Element orderConfirmationElement = (Element) orderConfirmationNode; Element orderConfirmationElement = (Element) orderConfirmationNode;
int id = Integer.parseInt(orderConfirmationElement.getElementsByTagName("ID").item(0).getTextContent()); int id = parseIntOrDefault(getElementTextContent(orderConfirmationElement, "ID"), -1);
int offerId = Integer.parseInt(orderConfirmationElement.getElementsByTagName("OfferID").item(0).getTextContent()); int offerId = parseIntOrDefault(getElementTextContent(orderConfirmationElement, "OfferID"), -1);
Offer orderConfirmationOffer = offers.stream().filter(o -> o.id == offerId).findFirst().orElse(null); Offer orderConfirmationOffer = offers.stream().filter(o -> o.id == offerId).findFirst().orElse(null);
String date = orderConfirmationElement.getElementsByTagName("Date").item(0).getTextContent(); String date = getElementTextContent(orderConfirmationElement, "Date");
String status = orderConfirmationElement.getElementsByTagName("Status").item(0).getTextContent(); String status = getElementTextContent(orderConfirmationElement, "Status");
if (orderConfirmationOffer != null) { if (orderConfirmationOffer != null) {
orderConfirmations.add(new OrderConfirmation(id, orderConfirmationOffer, date, status)); orderConfirmations.add(new OrderConfirmation(id, orderConfirmationOffer, date, status));
@ -767,15 +767,15 @@ public class Main {
for (int i = 0; i < deliveryNoteList.getLength(); i++) { for (int i = 0; i < deliveryNoteList.getLength(); i++) {
Node deliveryNoteNode = deliveryNoteList.item(i); Node deliveryNoteNode = deliveryNoteList.item(i);
if (deliveryNoteNode.getNodeType() == Node.ELEMENT_NODE) { if (deliveryNoteNode != null && deliveryNoteNode.getNodeType() == Node.ELEMENT_NODE) {
Element deliveryNoteElement = (Element) deliveryNoteNode; Element deliveryNoteElement = (Element) deliveryNoteNode;
int id = Integer.parseInt(deliveryNoteElement.getElementsByTagName("ID").item(0).getTextContent()); int id = parseIntOrDefault(getElementTextContent(deliveryNoteElement, "ID"), -1);
int orderConfirmationId = Integer.parseInt(deliveryNoteElement.getElementsByTagName("OrderConfirmationID").item(0).getTextContent()); int orderConfirmationId = parseIntOrDefault(getElementTextContent(deliveryNoteElement, "OrderConfirmationID"), -1);
OrderConfirmation deliveryNoteOrderConfirmation = orderConfirmations.stream().filter(oc -> oc.id == orderConfirmationId).findFirst().orElse(null); OrderConfirmation deliveryNoteOrderConfirmation = orderConfirmations.stream().filter(oc -> oc.id == orderConfirmationId).findFirst().orElse(null);
String date = deliveryNoteElement.getElementsByTagName("Date").item(0).getTextContent(); String date = getElementTextContent(deliveryNoteElement, "Date");
String status = deliveryNoteElement.getElementsByTagName("Status").item(0).getTextContent(); String status = getElementTextContent(deliveryNoteElement, "Status");
if (deliveryNoteOrderConfirmation != null) { if (deliveryNoteOrderConfirmation != null) {
deliveryNotes.add(new DeliveryNote(id, deliveryNoteOrderConfirmation, date, status)); deliveryNotes.add(new DeliveryNote(id, deliveryNoteOrderConfirmation, date, status));
@ -789,15 +789,15 @@ public class Main {
for (int i = 0; i < invoiceList.getLength(); i++) { for (int i = 0; i < invoiceList.getLength(); i++) {
Node invoiceNode = invoiceList.item(i); Node invoiceNode = invoiceList.item(i);
if (invoiceNode.getNodeType() == Node.ELEMENT_NODE) { if (invoiceNode != null && invoiceNode.getNodeType() == Node.ELEMENT_NODE) {
Element invoiceElement = (Element) invoiceNode; Element invoiceElement = (Element) invoiceNode;
int id = Integer.parseInt(invoiceElement.getElementsByTagName("ID").item(0).getTextContent()); int id = parseIntOrDefault(getElementTextContent(invoiceElement, "ID"), -1);
int deliveryNoteId = Integer.parseInt(invoiceElement.getElementsByTagName("DeliveryNoteID").item(0).getTextContent()); int deliveryNoteId = parseIntOrDefault(getElementTextContent(invoiceElement, "DeliveryNoteID"), -1);
DeliveryNote invoiceDeliveryNote = deliveryNotes.stream().filter(dn -> dn.id == deliveryNoteId).findFirst().orElse(null); DeliveryNote invoiceDeliveryNote = deliveryNotes.stream().filter(dn -> dn.id == deliveryNoteId).findFirst().orElse(null);
String date = invoiceElement.getElementsByTagName("Date").item(0).getTextContent(); String date = getElementTextContent(invoiceElement, "Date");
String status = invoiceElement.getElementsByTagName("Status").item(0).getTextContent(); String status = getElementTextContent(invoiceElement, "Status");
if (invoiceDeliveryNote != null) { if (invoiceDeliveryNote != null) {
invoices.add(new Invoice(id, invoiceDeliveryNote, date, status)); invoices.add(new Invoice(id, invoiceDeliveryNote, date, status));
@ -811,4 +811,33 @@ public class Main {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static String getElementTextContent(Element parentElement, String tagName) {
NodeList nodeList = parentElement.getElementsByTagName(tagName);
if (nodeList != null && nodeList.getLength() > 0) {
Node node = nodeList.item(0);
if (node != null) {
return node.getTextContent();
}
}
return "";
}
private static int parseIntOrDefault(String str, int defaultValue) {
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) {
return defaultValue;
}
}
private static double parseDoubleOrDefault(String str, double defaultValue) {
try {
return Double.parseDouble(str);
} catch (NumberFormatException e) {
return defaultValue;
}
}
} }