From a030d09a078ae5da1f68d88e1d8fad87b75e7d80 Mon Sep 17 00:00:00 2001 From: teridax Date: Wed, 7 Jun 2023 19:42:19 +0200 Subject: [PATCH] added Aufgabenblatt 13 --- .idea/libraries/formdev_flatlaf.xml | 10 + .idea/modules.xml | 1 + AufgabenBlatt11/src/aufgabe2/Taube.java | 2 + AufgabenBlatt13/.gitignore | 29 +++ AufgabenBlatt13/AufgabenBlatt13.iml | 12 ++ AufgabenBlatt13/res/BuecherCSV.csv | 12 ++ AufgabenBlatt13/src/aufgabe1/Book.java | 15 ++ .../src/aufgabe1/BookDatabase.java | 36 ++++ .../src/aufgabe1/BookDatabaseViewer.form | 160 ++++++++++++++++ .../src/aufgabe1/BookDatabaseViewer.java | 93 +++++++++ AufgabenBlatt13/src/aufgabe1/Main.java | 30 +++ AufgabenBlatt13/src/aufgabe2/Calculate.java | 6 + AufgabenBlatt13/src/aufgabe2/Calculator.form | 177 ++++++++++++++++++ AufgabenBlatt13/src/aufgabe2/Calculator.java | 84 +++++++++ AufgabenBlatt13/src/aufgabe2/Main.java | 10 + 15 files changed, 677 insertions(+) create mode 100644 .idea/libraries/formdev_flatlaf.xml create mode 100644 AufgabenBlatt13/.gitignore create mode 100644 AufgabenBlatt13/AufgabenBlatt13.iml create mode 100644 AufgabenBlatt13/res/BuecherCSV.csv create mode 100644 AufgabenBlatt13/src/aufgabe1/Book.java create mode 100644 AufgabenBlatt13/src/aufgabe1/BookDatabase.java create mode 100644 AufgabenBlatt13/src/aufgabe1/BookDatabaseViewer.form create mode 100644 AufgabenBlatt13/src/aufgabe1/BookDatabaseViewer.java create mode 100644 AufgabenBlatt13/src/aufgabe1/Main.java create mode 100644 AufgabenBlatt13/src/aufgabe2/Calculate.java create mode 100644 AufgabenBlatt13/src/aufgabe2/Calculator.form create mode 100644 AufgabenBlatt13/src/aufgabe2/Calculator.java create mode 100644 AufgabenBlatt13/src/aufgabe2/Main.java diff --git a/.idea/libraries/formdev_flatlaf.xml b/.idea/libraries/formdev_flatlaf.xml new file mode 100644 index 0000000..2d29c33 --- /dev/null +++ b/.idea/libraries/formdev_flatlaf.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 355059e..0f64eca 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -4,6 +4,7 @@ + diff --git a/AufgabenBlatt11/src/aufgabe2/Taube.java b/AufgabenBlatt11/src/aufgabe2/Taube.java index 8308a27..8451a1f 100644 --- a/AufgabenBlatt11/src/aufgabe2/Taube.java +++ b/AufgabenBlatt11/src/aufgabe2/Taube.java @@ -24,3 +24,5 @@ public class Taube implements Fliegen { System.out.println("GuuurrrGuurrr"); } } + + diff --git a/AufgabenBlatt13/.gitignore b/AufgabenBlatt13/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/AufgabenBlatt13/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/AufgabenBlatt13/AufgabenBlatt13.iml b/AufgabenBlatt13/AufgabenBlatt13.iml new file mode 100644 index 0000000..f8639ef --- /dev/null +++ b/AufgabenBlatt13/AufgabenBlatt13.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/AufgabenBlatt13/res/BuecherCSV.csv b/AufgabenBlatt13/res/BuecherCSV.csv new file mode 100644 index 0000000..fe0e6e5 --- /dev/null +++ b/AufgabenBlatt13/res/BuecherCSV.csv @@ -0,0 +1,12 @@ +Titel,Autor,Verlag,Preis,Verfügbar, +Atlas - Die Geschichte von Pa Salt,Lucinda Riley | Harry Whittaker,Goldmann,24,ja, +Das Cafe ohne Namen,Robert Seethaler,Claassen,24,ja, +Melody,Suter, Martin,26,ja +Blue Skies,T.C. Boyle,Hanser,28,nein, +Dark Sigils - Wie die Dunkelheit befiehlt,Anna Benning,Fischer,20,ja, +Eine Frage der Chemie,Bonnie Garmus,Piper,24,ja, +Der letzte Sessellift,John Irving,Diogenes,26,ja, +Liebe oder Eierlikör - Fast eine Romanze,Heldt, Dora,15,ja +Die Unverbesserlichen - Die Revanche des Monsieur Lipaire,Volker Klöpfel | Michael Kobr,Ullstein,20,nein, +Pompeji oder Die fünf Reden des Jowna,Ruge, Eugen,25,nein +Die Liebe an miesen Tagen,Ewald Arenz,Dumont,28,nein, diff --git a/AufgabenBlatt13/src/aufgabe1/Book.java b/AufgabenBlatt13/src/aufgabe1/Book.java new file mode 100644 index 0000000..fb0c16f --- /dev/null +++ b/AufgabenBlatt13/src/aufgabe1/Book.java @@ -0,0 +1,15 @@ +package aufgabe1; + +public record Book(String title, String author, String verlag, double value, boolean available) { + + public static Book parseFromCommaSeparatedString(String line) { + var values = line.split(","); + try { + return new Book(values[0], values[1], values[2], Double.parseDouble(values[3]), Boolean.parseBoolean(values[4])); + } catch (ArrayIndexOutOfBoundsException ignored) { + throw new IllegalArgumentException("not enough entries for book"); + } catch (NumberFormatException ignored) { + throw new IllegalArgumentException("value of book not a number"); + } + } +} \ No newline at end of file diff --git a/AufgabenBlatt13/src/aufgabe1/BookDatabase.java b/AufgabenBlatt13/src/aufgabe1/BookDatabase.java new file mode 100644 index 0000000..c18aa88 --- /dev/null +++ b/AufgabenBlatt13/src/aufgabe1/BookDatabase.java @@ -0,0 +1,36 @@ +package aufgabe1; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; +import java.util.Optional; + +public record BookDatabase(List books) { + + public static BookDatabase readFromFile(File file) { + if (!file.exists() || !file.canRead() || file.isDirectory()) + throw new IllegalArgumentException("Cannot read from file"); + + try { + var source = Files.readString(Paths.get(file.getPath()), StandardCharsets.UTF_8); + + var books = source.lines().skip(1).map(Book::parseFromCommaSeparatedString).toList(); + + return new BookDatabase(books); + } catch (ArrayIndexOutOfBoundsException | IOException e) { + throw new IllegalStateException("Cannot read from file: " + e); + } + } + + public Optional getBook(String title) { + return books.stream().filter(book -> book.title().equals(title)).findFirst(); + } + + public List getTitles() { + return books.stream().map(Book::title).toList(); + } +} diff --git a/AufgabenBlatt13/src/aufgabe1/BookDatabaseViewer.form b/AufgabenBlatt13/src/aufgabe1/BookDatabaseViewer.form new file mode 100644 index 0000000..0d713bf --- /dev/null +++ b/AufgabenBlatt13/src/aufgabe1/BookDatabaseViewer.form @@ -0,0 +1,160 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/AufgabenBlatt13/src/aufgabe1/BookDatabaseViewer.java b/AufgabenBlatt13/src/aufgabe1/BookDatabaseViewer.java new file mode 100644 index 0000000..f8bec9c --- /dev/null +++ b/AufgabenBlatt13/src/aufgabe1/BookDatabaseViewer.java @@ -0,0 +1,93 @@ +package aufgabe1; + +import javax.swing.*; +import java.awt.event.*; +import java.util.Optional; + +public class BookDatabaseViewer extends JDialog { + private JPanel contentPane; + private JButton buttonOK; + private JButton buttonCancel; + private JComboBox comboBoxTitle; + private JTextField textFieldTitle; + private JTextField textFieldAuthor; + private JTextField textFieldVerlag; + private JTextField textFieldValue; + private JButton verfuegbarkeitPruefenButton; + + private final BookDatabase bookDatabase; + + private Optional currentBook = Optional.empty(); + + public BookDatabaseViewer(BookDatabase bookDatabase) { + setContentPane(contentPane); + setModal(true); + getRootPane().setDefaultButton(buttonOK); + setTitle("Book Database"); + + this.bookDatabase = bookDatabase; + + buttonOK.addActionListener(e -> onOK()); + + buttonCancel.addActionListener(e -> onCancel()); + + textFieldValue.setEditable(false); + textFieldVerlag.setEditable(false); + textFieldAuthor.setEditable(false); + textFieldTitle.setEditable(false); + + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + comboBoxTitle.addActionListener(e -> { + SwingUtilities.invokeLater(this::selectTitle); + }); + verfuegbarkeitPruefenButton.addActionListener(this::checkAvailability); + + bookDatabase.getTitles().forEach(comboBoxTitle::addItem); + + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + pack(); + setLocationByPlatform(true); + setVisible(true); + } + + private void checkAvailability(ActionEvent actionEvent) { + if (currentBook.isPresent()) { + var book = currentBook.get(); + + JOptionPane.showMessageDialog(this, "Is the book available? " + (book.available() ? "Yes" : "No")); + } + } + + private void selectTitle() { + var title = (String) this.comboBoxTitle.getSelectedItem(); + currentBook = bookDatabase.getBook(title); + + if (currentBook.isPresent()) { + var book = currentBook.get(); + + this.textFieldTitle.setText(book.title()); + this.textFieldAuthor.setText(book.author()); + this.textFieldValue.setText(String.format("%.2f€", book.value())); + this.textFieldVerlag.setText(book.verlag()); + } + } + + private void onOK() { + // add your code here + dispose(); + } + + private void onCancel() { + // add your code here if necessary + dispose(); + } +} diff --git a/AufgabenBlatt13/src/aufgabe1/Main.java b/AufgabenBlatt13/src/aufgabe1/Main.java new file mode 100644 index 0000000..fdaffac --- /dev/null +++ b/AufgabenBlatt13/src/aufgabe1/Main.java @@ -0,0 +1,30 @@ +package aufgabe1; + +import com.formdev.flatlaf.FlatDarculaLaf; +import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; + +public class Main { + public static void main(String[] args) { + try { + UIManager.setLookAndFeel(new FlatDarculaLaf()); + } catch (UnsupportedLookAndFeelException e) { + throw new RuntimeException(e); + } + SwingUtilities.invokeLater(Main::init); + } + + private static void init() { + var chooser = new JFileChooser(); + chooser.addChoosableFileFilter(new FileNameExtensionFilter("csv files", "csv")); + chooser.setDialogType(JFileChooser.OPEN_DIALOG); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + chooser.setMultiSelectionEnabled(false); + + if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { + var file = chooser.getSelectedFile(); + + new BookDatabaseViewer(BookDatabase.readFromFile(file)); + } + } +} \ No newline at end of file diff --git a/AufgabenBlatt13/src/aufgabe2/Calculate.java b/AufgabenBlatt13/src/aufgabe2/Calculate.java new file mode 100644 index 0000000..a9e85ea --- /dev/null +++ b/AufgabenBlatt13/src/aufgabe2/Calculate.java @@ -0,0 +1,6 @@ +package aufgabe2; + +public interface Calculate { + + Double calculate(Double x, Double y); +} diff --git a/AufgabenBlatt13/src/aufgabe2/Calculator.form b/AufgabenBlatt13/src/aufgabe2/Calculator.form new file mode 100644 index 0000000..68cbea1 --- /dev/null +++ b/AufgabenBlatt13/src/aufgabe2/Calculator.form @@ -0,0 +1,177 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/AufgabenBlatt13/src/aufgabe2/Calculator.java b/AufgabenBlatt13/src/aufgabe2/Calculator.java new file mode 100644 index 0000000..0bdf7d8 --- /dev/null +++ b/AufgabenBlatt13/src/aufgabe2/Calculator.java @@ -0,0 +1,84 @@ +package aufgabe2; + +import javax.swing.*; +import java.awt.event.*; + +public class Calculator extends JDialog { + private JPanel contentPane; + private JButton buttonOK; + private JButton buttonCancel; + private JTextField operandOneTextField; + private JTextField operandTwoTextField; + private JButton addButton; + private JButton subButton; + private JButton MulButton; + private JButton divButton; + private JLabel Result; + private JTextField resultTextField; + + public Calculator() { + setContentPane(contentPane); + setModal(true); + getRootPane().setDefaultButton(buttonOK); + + buttonOK.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onOK(); + } + }); + + buttonCancel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onCancel(); + } + }); + + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onCancel(); + } + }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + + addButton.addActionListener(e -> calculate(Double::sum)); + subButton.addActionListener(e -> calculate((a,b) -> a - b)); + MulButton.addActionListener(e -> calculate((a,b) -> a * b)); + divButton.addActionListener(e -> calculate((a,b) -> a / b)); + + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + pack(); + setLocationByPlatform(true); + setVisible(true); + } + + private void calculate(Calculate operation) { + try { + var op0 = Double.parseDouble(operandOneTextField.getText()); + var op1 = Double.parseDouble(operandTwoTextField.getText()); + + var res = operation.calculate(op0, op1); + + resultTextField.setText(res.toString()); + } catch (NumberFormatException e) { + JOptionPane.showInternalMessageDialog(null, "Operand numbers must be valid", "Calculation", JOptionPane.ERROR_MESSAGE); + } + } + + private void onOK() { + // add your code here + dispose(); + } + + private void onCancel() { + // add your code here if necessary + dispose(); + } +} diff --git a/AufgabenBlatt13/src/aufgabe2/Main.java b/AufgabenBlatt13/src/aufgabe2/Main.java new file mode 100644 index 0000000..4f1a2b4 --- /dev/null +++ b/AufgabenBlatt13/src/aufgabe2/Main.java @@ -0,0 +1,10 @@ +package aufgabe2; + +import javax.swing.*; + +public class Main { + + public static void main(String[] args) { + SwingUtilities.invokeLater(Calculator::new); + } +}