-
Notifications
You must be signed in to change notification settings - Fork 0
/
GasStations.java
132 lines (111 loc) · 3.92 KB
/
GasStations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package de.niklas.exercise.collections;
import java.util.*;
import javax.swing.*;
import java.awt.*;
/**
* <strong>Tankstellenpreise</strong><br>
* Eine GUI um Tankstellenpreise in einer HashMap zu speichern
*
* @see "25_Datenstrukturen_Aufgaben-3.pdf"
* @author Niklas Buse
*/
public class GasStations extends JFrame {
private JTextField stationField, dieselField, e5Field, e10Field;
Map<String, GasPrices> gasPricesMap = new HashMap<>();
public GasStations() {
renderGUI();
}
/**
* Erstellen der GUI für die Anwendung
*/
private void renderGUI() {
JFrame frame = new JFrame("GasStations");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel main_panel = new JPanel();
main_panel.setLayout(new GridLayout(5, 2));
main_panel.add(new Label("Station Name:"));
stationField = new JTextField(15);
main_panel.add(stationField);
main_panel.add(new Label("Diesel:"));
dieselField = new JTextField(15);
main_panel.add(dieselField);
main_panel.add(new Label("Super E5:"));
e5Field = new JTextField(15);
main_panel.add(e5Field);
main_panel.add(new Label("Super E10:"));
e10Field = new JTextField(15);
main_panel.add(e10Field);
JButton saveButton = new JButton("Save");
saveButton.addActionListener(e -> save());
main_panel.add(saveButton);
JButton showButton = new JButton("Show all");
showButton.addActionListener(e -> showAll());
main_panel.add(showButton);
frame.add(main_panel);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
/**
* Speichern der in der GUI eingetragen Werte inkl. Fehlerbehandlung
*/
public void save() {
String stationName = this.stationField.getText();
if (stationName.equals("") | stationName.isBlank() | stationName.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please provide a Name!");
} else {
double diesel, e5, e10;
try {
diesel = Double.parseDouble(this.dieselField.getText().replace(",", "."));
} catch (NumberFormatException e) {
diesel = -1;
}
try {
e5 = Double.parseDouble(this.e5Field.getText().replace(",", "."));
} catch (Exception e) {
e5 = -1;
}
try {
e10 = Double.parseDouble(this.e10Field.getText().replace(",", "."));
} catch (Exception e) {
e10 = -1;
}
gasPricesMap.put(stationName, new GasPrices(diesel, e5, e10));
JOptionPane.showMessageDialog(null, String.format("Saved: %s", gasPricesMap.get(this.stationField.getText())));
stationField.setText("");
dieselField.setText("");
e5Field.setText("");
e10Field.setText("");
}
}
/**
* Ausgeben der aktuell in der Map gespeicherten Einträge im MessageDialog
*/
public void showAll() {
StringBuilder stringBuilder = new StringBuilder();
gasPricesMap.forEach((key, value) -> stringBuilder.append(key).append(": ").append(value));
JOptionPane.showMessageDialog(null, stringBuilder.toString());
}
public static void main(String[] args) {
new GasStations();
}
}
/**
* Basis Klasse für die Gaspreise
*/
class GasPrices {
double diesel, superE5, superE10;
public GasPrices(double diesel, double superE5, double superE10) {
this.diesel = diesel;
this.superE5 = superE5;
this.superE10 = superE10;
}
/**
* Überschreibt die toString() für anderen Stil
* @return Gaspreise als String
*/
@Override
public String toString() {
return String.format("Diesel: %.2f, SuperE5: %.2f, SuperE10: %.2f\n", diesel, superE5, superE10);
}
}