This commit is contained in:
lukas 2020-05-15 13:20:52 +02:00
commit 0cb25ef1ce
8 changed files with 203 additions and 0 deletions

BIN
Uebungsklausur 2018.pdf Normal file

Binary file not shown.

9
products.csv Normal file
View File

@ -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
1 Bleisitft h2 0.75
2 Bleistift h3 0.75
3 Bleistift h4 0.75
4 Kugelschreiber rot fein 1.11
5 Kugelschreiber blau fein 1.11
6 Textmarker gelb 1.23
7 Textmarker grün 1.25
8 Permanentmarker sw 2.11
9 Permanentmarker rt 2.11

5
products2.csv Normal file
View File

@ -0,0 +1,5 @@
Keyboard de; 12.55
Keyboard en; 13.55
Mouse bluetooth; 32.12
Mouse standard; 7.22
Mouse wireless; 14.44
1 Keyboard de 12.55
2 Keyboard en 13.55
3 Mouse bluetooth 32.12
4 Mouse standard 7.22
5 Mouse wireless 14.44

8
src/Application.java Normal file
View File

@ -0,0 +1,8 @@
public class Application {
public static void main(String[] args) {
OrderFrame bf = new OrderFrame();
}
}

106
src/OrderFrame.java Normal file
View File

@ -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<Product> 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();
}
}
}

24
src/Product.java Normal file
View File

@ -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;
}
}

46
src/ProductPanel.java Normal file
View File

@ -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);
}
}

View File

@ -0,0 +1,5 @@
public class WrongFileFormatException extends RuntimeException{
public WrongFileFormatException(String message) {
super(message);
}
}