Add Lottery, ComponentFrame, CurrencyCalculator, TextFrame, and TextfileViewer classes
This commit is contained in:
parent
c41409b9d9
commit
d0a8edecfe
6 changed files with 363 additions and 1 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
*.class
|
*.class
|
||||||
*.jar
|
*.jar
|
||||||
.devcontainer
|
.devcontainer
|
||||||
|
/*.txt
|
30
src/de/dhbwka/java/exercise/collections/Lottery.java
Normal file
30
src/de/dhbwka/java/exercise/collections/Lottery.java
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package de.dhbwka.java.exercise.collections;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class Lottery {
|
||||||
|
Set<Integer> lotteryNumbers = new HashSet<>();
|
||||||
|
Integer zusatzZahl;
|
||||||
|
|
||||||
|
public Lottery() {
|
||||||
|
while (lotteryNumbers.size() < 7) {
|
||||||
|
int number = (int) (Math.random() * 49) + 1;
|
||||||
|
zusatzZahl = number;
|
||||||
|
lotteryNumbers.add(number);
|
||||||
|
}
|
||||||
|
lotteryNumbers.remove(zusatzZahl);
|
||||||
|
|
||||||
|
String[] lotteryNumbersArray = lotteryNumbers.stream()
|
||||||
|
.sorted()
|
||||||
|
.map(String::valueOf)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
|
||||||
|
System.out.println("Die Lottozahlen sind: " + String.join(", ", lotteryNumbersArray));
|
||||||
|
System.out.println("Die Zusatzzahl ist: " + zusatzZahl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Lottery();
|
||||||
|
}
|
||||||
|
}
|
52
src/de/dhbwka/java/exercise/ui/ComponentFrame.java
Normal file
52
src/de/dhbwka/java/exercise/ui/ComponentFrame.java
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package de.dhbwka.java.exercise.ui;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class ComponentFrame extends JFrame {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new ComponentFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ComponentFrame() {
|
||||||
|
this.setTitle("Title");
|
||||||
|
this.setSize(600, 200);
|
||||||
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
JTabbedPane tabbedPane = new JTabbedPane();
|
||||||
|
|
||||||
|
JPanel panel1 = new JPanel();
|
||||||
|
panel1.add(new JLabel("Label"));
|
||||||
|
panel1.add(new JTextField("TextField", 15));
|
||||||
|
panel1.add(new JPasswordField("Password", 15));
|
||||||
|
panel1.add(new JButton("Btn"));
|
||||||
|
panel1.add(new JToggleButton("Toggle"));
|
||||||
|
panel1.add(new JCheckBox("Box"));
|
||||||
|
panel1.add(new JComboBox<String>(new String[] { "Item 1", "Item 2", "Item 3" }));
|
||||||
|
JRadioButton radioButton1 = new JRadioButton("Radio 1");
|
||||||
|
panel1.add(radioButton1);
|
||||||
|
JRadioButton radioButton2 = new JRadioButton("Radio 2");
|
||||||
|
panel1.add(radioButton2);
|
||||||
|
JRadioButton radioButton3 = new JRadioButton("Radio 3");
|
||||||
|
panel1.add(radioButton3);
|
||||||
|
ButtonGroup group = new ButtonGroup();
|
||||||
|
group.add(radioButton1);
|
||||||
|
group.add(radioButton2);
|
||||||
|
group.add(radioButton3);
|
||||||
|
panel1.add(new JTextArea("TextArea", 5, 20));
|
||||||
|
tabbedPane.addTab("Tab 1", panel1);
|
||||||
|
|
||||||
|
JPanel panel2 = new JPanel();
|
||||||
|
panel2.add(new JLabel("Label 2"));
|
||||||
|
panel2.add(new JColorChooser());
|
||||||
|
panel2.add(new JSpinner());
|
||||||
|
panel2.add(new JSlider());
|
||||||
|
panel2.add(new JProgressBar());
|
||||||
|
panel2.add(new JList<String>(new String[] { "List 1", "List 2", "List 3" }));
|
||||||
|
tabbedPane.addTab("Tab 2", panel2);
|
||||||
|
|
||||||
|
this.add(tabbedPane);
|
||||||
|
this.pack();
|
||||||
|
this.setLocationRelativeTo(null);
|
||||||
|
this.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
128
src/de/dhbwka/java/exercise/ui/CurrencyCalculator.java
Normal file
128
src/de/dhbwka/java/exercise/ui/CurrencyCalculator.java
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
package de.dhbwka.java.exercise.ui;
|
||||||
|
|
||||||
|
import java.awt.Component;
|
||||||
|
import java.awt.Container;
|
||||||
|
import java.awt.GridBagConstraints;
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class CurrencyCalculator extends JFrame {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new CurrencyCalculator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CurrencyCalculator() {
|
||||||
|
initComponents();
|
||||||
|
addEventListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addComponent(Container cont, Component c,
|
||||||
|
int x, int y, int width, int height, double weightx, double weighty) {
|
||||||
|
GridBagConstraints gbc = new GridBagConstraints();
|
||||||
|
gbc.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc.gridx = x;
|
||||||
|
gbc.gridy = y;
|
||||||
|
gbc.gridwidth = width;
|
||||||
|
gbc.gridheight = height;
|
||||||
|
gbc.weightx = weightx;
|
||||||
|
gbc.weighty = weighty;
|
||||||
|
cont.add(c, gbc);
|
||||||
|
}
|
||||||
|
|
||||||
|
JTextField amountField = new JTextField();
|
||||||
|
JButton cancelButton = new JButton("Cancel");
|
||||||
|
JButton convertButton = new JButton("Convert");
|
||||||
|
|
||||||
|
String[] currencies = { "EUR", "USD", "GBP", "CHF", "DM" };
|
||||||
|
double[] rates = { 1.0, 0.90054095, 1.1896249, 1.0711047, 0.51129 };
|
||||||
|
|
||||||
|
JComboBox<String> currencyBoxFrom = new JComboBox<>(currencies);
|
||||||
|
JComboBox<String> currencyBoxTo = new JComboBox<>(currencies);
|
||||||
|
|
||||||
|
private void initComponents() {
|
||||||
|
setTitle("Currency Calculator");
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setSize(400, 300);
|
||||||
|
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
panel.setLayout(new GridBagLayout());
|
||||||
|
|
||||||
|
addComponent(panel, amountField, 0, 0, 4, 1, 1.0, 0.0);
|
||||||
|
addComponent(panel, currencyBoxFrom, 0, 1, 1, 1, 0.0, 0.0);
|
||||||
|
addComponent(panel, currencyBoxTo, 1, 1, 1, 1, 0.0, 0.0);
|
||||||
|
addComponent(panel, convertButton, 2, 1, 1, 1, 0.0, 0.0);
|
||||||
|
addComponent(panel, cancelButton, 3, 1, 1, 1, 0.0, 0.0);
|
||||||
|
|
||||||
|
add(panel);
|
||||||
|
pack();
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addEventListeners() {
|
||||||
|
convertButton.addActionListener(e -> {
|
||||||
|
String from = (String) currencyBoxFrom.getSelectedItem();
|
||||||
|
String to = (String) currencyBoxTo.getSelectedItem();
|
||||||
|
convertCurrency(from, to);
|
||||||
|
});
|
||||||
|
cancelButton.addActionListener(e -> {
|
||||||
|
this.dispose();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
private void convertCurrency(String from, String to) {
|
||||||
|
// get the amount from the text field
|
||||||
|
String amountText = amountField.getText();
|
||||||
|
double amount = 0;
|
||||||
|
try {
|
||||||
|
amount = Double.parseDouble(amountText);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Invalid amount", "Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// convert the amount to intermediate currency (EUR)
|
||||||
|
double intermediateAmount = 0;
|
||||||
|
int fromIndex = -1;
|
||||||
|
for (int i = 0; i < currencies.length; i++) {
|
||||||
|
if (currencies[i].equals(from)) {
|
||||||
|
fromIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fromIndex == -1) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Invalid currency", "Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// convert the amount to EUR
|
||||||
|
intermediateAmount = amount * rates[fromIndex];
|
||||||
|
|
||||||
|
// convert the intermediate amount to the target currency
|
||||||
|
double targetAmount = 0;
|
||||||
|
int toIndex = -1;
|
||||||
|
for (int i = 0; i < currencies.length; i++) {
|
||||||
|
if (currencies[i].equals(to)) {
|
||||||
|
toIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (toIndex == -1) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Invalid currency", "Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// convert the intermediate amount to the target currency
|
||||||
|
targetAmount = intermediateAmount / rates[toIndex];
|
||||||
|
|
||||||
|
// show the result in a message dialog
|
||||||
|
String message = String.format("Converted amount: %.2f %s", targetAmount, to);
|
||||||
|
JOptionPane.showMessageDialog(this, message, "Result",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
// clear the text field
|
||||||
|
amountField.setText("");
|
||||||
|
// set focus to the text field
|
||||||
|
amountField.requestFocus();
|
||||||
|
}
|
||||||
|
}
|
62
src/de/dhbwka/java/exercise/ui/TextFrame.java
Normal file
62
src/de/dhbwka/java/exercise/ui/TextFrame.java
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
package de.dhbwka.java.exercise.ui;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class TextFrame extends JFrame {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if (args.length != 3 || args[0].equals("-h")) {
|
||||||
|
System.err.println("Usage: java TextFrame <filename> <width> <height>");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
int width = Integer.parseInt(args[1]);
|
||||||
|
int height = Integer.parseInt(args[2]);
|
||||||
|
new TextFrame(width, height, args[0]);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.err.println("Width and height must be integers.");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextFrame(int width, int height, String fileName) {
|
||||||
|
// Load the text file
|
||||||
|
String text = loadTextFile(fileName);
|
||||||
|
if (text == null) {
|
||||||
|
System.err.println("Error loading file: " + fileName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Create the text area
|
||||||
|
JTextArea textArea = new JTextArea(text);
|
||||||
|
textArea.setLineWrap(true);
|
||||||
|
textArea.setWrapStyleWord(true);
|
||||||
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
||||||
|
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
||||||
|
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
||||||
|
|
||||||
|
// Create the frame
|
||||||
|
this.setTitle("TextFrame");
|
||||||
|
this.setSize(width, height);
|
||||||
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
this.setLocationRelativeTo(null);
|
||||||
|
this.add(scrollPane);
|
||||||
|
this.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String loadTextFile(String fileName) {
|
||||||
|
StringBuilder text = new StringBuilder();
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
text.append(line).append("\n");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return text.toString();
|
||||||
|
}
|
||||||
|
}
|
89
src/de/dhbwka/java/exercise/ui/TextfileViewer.java
Normal file
89
src/de/dhbwka/java/exercise/ui/TextfileViewer.java
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
package de.dhbwka.java.exercise.ui;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.filechooser.*;
|
||||||
|
|
||||||
|
public class TextfileViewer {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new TextfileViewer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextfileViewer() {
|
||||||
|
initComponents();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initComponents() {
|
||||||
|
JFileChooser fc = new javax.swing.JFileChooser();
|
||||||
|
fc.setFileFilter(new FileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File f) {
|
||||||
|
return f.isDirectory() ||
|
||||||
|
f.getName().toLowerCase().endsWith(".txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "Text Files";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
int state = fc.showOpenDialog(null);
|
||||||
|
if (state == JFileChooser.APPROVE_OPTION) {
|
||||||
|
this.openFile(fc.getSelectedFile(), "labels");
|
||||||
|
this.openFile(fc.getSelectedFile(), "textArea");
|
||||||
|
} else {
|
||||||
|
this.error("No file selected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openFile(File file, String mode) {
|
||||||
|
JFrame frame = new JFrame("Textfile Viewer - " + file.getName());
|
||||||
|
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
|
frame.setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
if (mode.equals("labels")) {
|
||||||
|
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
panel.setLayout(new GridLayout(10, 1, 2, 2));
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||||
|
String line;
|
||||||
|
int i = 0;
|
||||||
|
while ((line = br.readLine()) != null && i++ < 10) {
|
||||||
|
JLabel label = new JLabel(line);
|
||||||
|
panel.add(label);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
frame.add(panel);
|
||||||
|
frame.setSize(400, 300);
|
||||||
|
frame.setVisible(true);
|
||||||
|
} else if (mode.equals("textArea")) {
|
||||||
|
JTextArea textArea = new JTextArea();
|
||||||
|
textArea.setEditable(false);
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
textArea.append(line + "\n");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
||||||
|
frame.add(scrollPane);
|
||||||
|
frame.setSize(400, 300);
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void error(String message) {
|
||||||
|
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue