-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClaimController.java
More file actions
85 lines (79 loc) · 3.12 KB
/
ClaimController.java
File metadata and controls
85 lines (79 loc) · 3.12 KB
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
/**
* Einfacher Controller, der ein `Claim` und die zugehörige `ClaimView`
* verbindet. Kapselt zukünftige Logik (z. B. Abbauen per UI).
*/
public class ClaimController {
private Claim claim;
private ClaimView view;
public ClaimController(Claim pClaim) {
this.claim = pClaim;
this.view = new ClaimView(pClaim, 48);
// Key binding: 'a' -> abbauen
this.view.addKeyAction("typed a", "abbauen", new javax.swing.AbstractAction() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
int r = view.gibSelectedZeile();
int c = view.gibSelectedSpalte();
if (r < 0 || c < 0) {
view.appendOutput("Keine Parzelle selektiert.");
return;
}
String input = javax.swing.JOptionPane.showInputDialog(view, "Gewünschte Abbautiefe:", "Abbauen",
javax.swing.JOptionPane.PLAIN_MESSAGE);
if (input == null) {
// abgebrochen
return;
}
double tiefe;
try {
tiefe = Double.parseDouble(input.trim());
} catch (NumberFormatException ex) {
view.appendOutput("Ungültige Zahl.");
return;
}
if (tiefe <= 0.0) {
view.appendOutput("Abbautiefe muss > 0 sein.");
return;
}
Erdschicht entnommen = claim.baueParzelleAb(r, c, tiefe);
if (entnommen == null) {
view.appendOutput("Nichts zum Abbauen oder ungültige Parzelle.");
} else {
view.appendOutput(
String.format("Abgebaut: %.2f von %s", entnommen.gibDicke(), entnommen.gibSorte()));
view.repaintView();
}
}
});
// Key binding: 's' -> stability check
this.view.addKeyAction("typed s", "stabilitaet", new javax.swing.AbstractAction() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
String input = javax.swing.JOptionPane.showInputDialog(view, "Maximale Tiefendifferenz (pMax):",
"Stabilitätsprüfung", javax.swing.JOptionPane.PLAIN_MESSAGE);
if (input == null) {
return;
}
double pMax;
try {
pMax = Double.parseDouble(input.trim());
} catch (NumberFormatException ex) {
view.appendOutput("Ungültige Zahl für pMax.");
return;
}
boolean stabil = claim.istStabil(pMax);
if (stabil) {
view.appendOutput("Claim stabil");
} else {
view.appendOutput("Claim unstabil");
}
}
});
}
public Claim getClaim() {
return claim;
}
public ClaimView getView() {
return view;
}
}