commit 0cb25ef1ce957ddb38fb19d512351545017cd461 Author: lukas Date: Fri May 15 13:20:52 2020 +0200 init diff --git a/Uebungsklausur 2018.pdf b/Uebungsklausur 2018.pdf new file mode 100644 index 0000000..6377265 Binary files /dev/null and b/Uebungsklausur 2018.pdf differ diff --git a/products.csv b/products.csv new file mode 100644 index 0000000..44458ab --- /dev/null +++ b/products.csv @@ -0,0 +1,9 @@ +Bleisitft h2; 0.75 +Bleistift h3; 0.75 +Bleistift h4; 0.75 +Kugelschreiber rot fein; 1.11 +Kugelschreiber blau fein; 1.11 +Textmarker gelb; 1.23 +Textmarker grĂ¼n; 1.25 +Permanentmarker sw; 2.11 +Permanentmarker rt; 2.11 \ No newline at end of file diff --git a/products2.csv b/products2.csv new file mode 100644 index 0000000..0058d0a --- /dev/null +++ b/products2.csv @@ -0,0 +1,5 @@ +Keyboard de; 12.55 +Keyboard en; 13.55 +Mouse bluetooth; 32.12 +Mouse standard; 7.22 +Mouse wireless; 14.44 diff --git a/src/Application.java b/src/Application.java new file mode 100644 index 0000000..0c14bf0 --- /dev/null +++ b/src/Application.java @@ -0,0 +1,8 @@ + +public class Application { + + public static void main(String[] args) { + OrderFrame bf = new OrderFrame(); + } + +} diff --git a/src/OrderFrame.java b/src/OrderFrame.java new file mode 100644 index 0000000..6e4d678 --- /dev/null +++ b/src/OrderFrame.java @@ -0,0 +1,106 @@ +import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.filechooser.FileNameExtensionFilter; +import java.awt.*; +import java.io.*; +import java.util.Vector; + +public class OrderFrame extends JFrame { + private static final long serialVersionUID = 1L; + + private ProductPanel productPanel = new ProductPanel(); + private JButton buttonOpen = new JButton("Open"); + + private Vector bla = new Vector(); + private JList list; + + // TODO: Add property for products + + public OrderFrame() { + this.setTitle("Lukas HEiligenbrunner"); + this.add(buttonOpen, BorderLayout.NORTH); + this.add(productPanel); + + JFrame _this = this; + buttonOpen.addActionListener(e -> { + System.out.println("Btn clicked!"); + + JFileChooser chooser = new JFileChooser(new File(".")); + FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV Images", "csv"); + chooser.setFileFilter(filter); + + int returnVal = chooser.showOpenDialog(_this); + if (returnVal == JFileChooser.APPROVE_OPTION) { + System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); + try { + openFileAndFillToListBox(chooser.getSelectedFile().getName()); + list.repaint(); + } catch (WrongFileFormatException ee) { + ee.printStackTrace(); + bla.clear(); + } + } + }); + + try { + openFileAndFillToListBox("products.csv"); + } catch (WrongFileFormatException e) { + e.printStackTrace(); + bla.clear(); + } + + + list = new JList(bla); + list.addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + + productPanel.setData(bla.get(e.getLastIndex())); + System.out.println("something was selected" + e.getLastIndex()); + + } + }); + this.add(list, BorderLayout.WEST); + + + this.setSize(600, 400); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setVisible(true); + } + + public void openFileAndFillToListBox(String filename) { + bla.clear(); + File myfile = new File(filename); + try { + BufferedReader reader = new BufferedReader(new FileReader(myfile)); + + while (reader.ready()) { + String str = reader.readLine(); + String[] stringsiarri = str.split(";"); + + if (stringsiarri.length != 2) { + System.out.println("error occured"); + throw new WrongFileFormatException("too many rows"); + } + + + try { + Float.parseFloat(stringsiarri[1]); + } catch (NumberFormatException e) { + // no float in csv + throw new WrongFileFormatException("Wrong data type in csv"); + } + + Product tempproduct = new Product(stringsiarri[0], stringsiarri[1]); + bla.add(tempproduct); + } + + reader.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/Product.java b/src/Product.java new file mode 100644 index 0000000..6ba66e1 --- /dev/null +++ b/src/Product.java @@ -0,0 +1,24 @@ +public class Product { + private String Name; + private String Price; + + public Product(String name, String price) { + Name = name; + Price = price; + } + + public Product() {} + + public String getName() { + return Name; + } + + public String getPrice() { + return Price; + } + + @Override + public String toString() { + return Name; + } +} diff --git a/src/ProductPanel.java b/src/ProductPanel.java new file mode 100644 index 0000000..e63052e --- /dev/null +++ b/src/ProductPanel.java @@ -0,0 +1,46 @@ +import java.awt.BorderLayout; +import java.awt.GridLayout; + +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; + +class ProductDetails extends JPanel { + private static final long serialVersionUID = 8069232875557223123L; + + private JLabel lblName = new JLabel("Name:"); + private JLabel lblPreis = new JLabel("Preis:"); + private JTextField tfName = new JTextField(); + private JTextField tfPrice = new JTextField(); + + public ProductDetails() { + this.setLayout(new GridLayout(2,2)); + tfName.setHorizontalAlignment(JTextField.RIGHT); + tfPrice.setHorizontalAlignment(JTextField.RIGHT); + this.add(lblName); + this.add(tfName); + this.add(lblPreis); + this.add(tfPrice); + } + + public void setData(Product p){ + tfName.setText(p.getName()); + tfPrice.setText(p.getPrice()); + } +} + +public class ProductPanel extends JPanel { + + private static final long serialVersionUID = -8222745310953362748L; + + private ProductDetails pProduktDetails = new ProductDetails(); + + public ProductPanel() { + this.setLayout(new BorderLayout()); + this.add(pProduktDetails, BorderLayout.NORTH); + } + + public void setData(Product p){ + this.pProduktDetails.setData(p); + } +} diff --git a/src/WrongFileFormatException.java b/src/WrongFileFormatException.java new file mode 100644 index 0000000..5c6269a --- /dev/null +++ b/src/WrongFileFormatException.java @@ -0,0 +1,5 @@ +public class WrongFileFormatException extends RuntimeException{ + public WrongFileFormatException(String message) { + super(message); + } +}