commit | author | age
|
2e729d
|
1 |
package brydz; |
JK |
2 |
|
|
3 |
import javax.microedition.lcdui.*; |
|
4 |
|
|
5 |
public class BrydzForm extends Form implements CommandListener, ItemStateListener { |
|
6 |
protected BrydzMIDlet midlet; |
|
7 |
|
|
8 |
protected TextField kontraktField; |
|
9 |
protected ChoiceGroup kontraktChoice; |
|
10 |
protected ChoiceGroup kontraChoice; |
|
11 |
protected ChoiceGroup partiaChoice; |
|
12 |
protected TextField punktyField; |
|
13 |
protected TextField lewyField; |
|
14 |
protected StringItem resultText; |
|
15 |
|
|
16 |
protected BrydzLicz liczenie; |
|
17 |
|
|
18 |
public BrydzForm(BrydzMIDlet midlet) { |
|
19 |
super("Obliczenia brydżowe"); |
|
20 |
|
|
21 |
this.midlet = midlet; |
|
22 |
this.liczenie = new BrydzLicz(); |
|
23 |
|
|
24 |
this.kontraktField = new TextField("Kontrakt:", null, 1, TextField.NUMERIC); |
|
25 |
this.kontraktChoice = new ChoiceGroup(null, List.EXCLUSIVE); |
|
26 |
this.kontraktChoice.append("♣/♦", null); |
|
27 |
this.kontraktChoice.append("♥/♠", null); |
|
28 |
this.kontraktChoice.append("NT", null); |
|
29 |
|
|
30 |
this.kontraChoice = new ChoiceGroup(null, ChoiceGroup.EXCLUSIVE); |
|
31 |
this.kontraChoice.append("norm.", null); |
|
32 |
this.kontraChoice.append("ktr.", null); |
|
33 |
this.kontraChoice.append("rektr.", null); |
|
34 |
|
|
35 |
this.partiaChoice = new ChoiceGroup(null, ChoiceGroup.EXCLUSIVE); |
|
36 |
this.partiaChoice.append("przed", null); |
|
37 |
this.partiaChoice.append("po", null); |
|
38 |
|
|
39 |
this.punktyField = new TextField("PC:", null, 2, TextField.NUMERIC); |
|
40 |
this.lewyField = new TextField("Lew:", null, 2, TextField.NUMERIC); |
|
41 |
|
|
42 |
this.resultText = new StringItem(null, "Podaj wszystkie dane"); |
|
43 |
|
|
44 |
this.append(this.kontraktField); |
|
45 |
this.append(this.kontraktChoice); |
|
46 |
this.append(this.kontraChoice); |
|
47 |
this.append(this.partiaChoice); |
|
48 |
this.append(this.punktyField); |
|
49 |
this.append(this.lewyField); |
|
50 |
this.append(this.resultText); |
|
51 |
|
|
52 |
setItemStateListener(this); |
|
53 |
setCommandListener(this); |
|
54 |
addCommand(new Command("Wyczyść", Command.OK, 1)); |
|
55 |
addCommand(new Command("Wyjście", Command.EXIT, 1)); |
|
56 |
} |
|
57 |
|
|
58 |
public void commandAction(Command command, Displayable displayable) { |
|
59 |
if (command.getCommandType() == Command.OK) { |
|
60 |
this.kontraktField.setString(null); |
|
61 |
this.kontraktChoice.setSelectedIndex(0, true); |
|
62 |
this.kontraChoice.setSelectedIndex(0, true); |
|
63 |
this.partiaChoice.setSelectedIndex(0, true); |
|
64 |
this.punktyField.setString(null); |
|
65 |
this.lewyField.setString(null); |
|
66 |
this.resultText.setText("Podaj wszystkie dane."); |
|
67 |
} |
|
68 |
else if (command.getCommandType() == Command.EXIT) { |
|
69 |
this.midlet.destroyApp(true); |
|
70 |
} |
|
71 |
} |
|
72 |
|
|
73 |
public void itemStateChanged(Item item) { |
|
74 |
int kontrakt, typ, kontra, partia, punkty, lewy; |
|
75 |
|
|
76 |
try { |
|
77 |
kontrakt = Integer.parseInt(this.kontraktField.getString()); |
|
78 |
typ = this.kontraktChoice.getSelectedIndex(); |
|
79 |
kontra = this.kontraChoice.getSelectedIndex(); |
|
80 |
partia = this.partiaChoice.getSelectedIndex(); |
|
81 |
punkty = Integer.parseInt(this.punktyField.getString()); |
|
82 |
lewy = Integer.parseInt(this.lewyField.getString()); |
|
83 |
} |
|
84 |
catch(NumberFormatException e) { |
|
85 |
this.resultText.setText("Podaj wszystkie dane."); |
|
86 |
return; |
|
87 |
} |
|
88 |
|
|
89 |
BrydzWynik wynik; |
|
90 |
|
|
91 |
String data = ""; |
|
92 |
|
|
93 |
if( kontrakt < 0 || kontrakt > 7 ) { |
|
94 |
this.resultText.setText("Niepoprawny kontrakt."); |
|
95 |
return; |
|
96 |
} |
|
97 |
|
|
98 |
if( typ != 0 && typ != 1 && typ != 2 ) { |
|
99 |
this.resultText.setText("Niepoprawny typ kontraktu."); |
|
100 |
return; |
|
101 |
} |
|
102 |
|
|
103 |
if( kontra < 0 || kontra > 2 ) { |
|
104 |
this.resultText.setText("Niepoprawna informacja o kontrze."); |
|
105 |
return; |
|
106 |
} |
|
107 |
|
|
108 |
if( partia != 0 && partia != 1) { |
|
109 |
this.resultText.setText("Niepoprawna informacja o partii."); |
|
110 |
return; |
|
111 |
} |
|
112 |
|
|
113 |
if( punkty < 0 || punkty > 40 ) { |
|
114 |
this.resultText.setText("Niepoprawna ilość punktów"); |
|
115 |
return; |
|
116 |
} |
|
117 |
|
|
118 |
if( lewy < 0 || lewy > 13 ) { |
|
119 |
this.resultText.setText("Niepoprawna ilość wziętych lew."); |
|
120 |
return; |
|
121 |
} |
|
122 |
|
|
123 |
if( kontrakt == 0 && punkty < 20 ) { |
|
124 |
this.resultText.setText("Przy czterech pasach podaj większą liczę punktów."); |
|
125 |
return; |
|
126 |
} |
|
127 |
|
|
128 |
wynik = this.liczenie.policz(kontrakt, typ, kontra, partia, punkty, lewy); |
|
129 |
|
|
130 |
this.resultText.setText((wynik.dla == 0 ? "Zapis dla rozgrywających" : "Zapis dla przeciwników.")+ |
|
131 |
"\nPunktów (bez PC): "+wynik.punkty_przed+ |
|
132 |
"\nPunktów (z PC): "+wynik.punkty+ |
|
133 |
"\nIMP-ów: "+wynik.IMP+ |
|
134 |
"\nProcent: "+wynik.procent); |
|
135 |
} |
|
136 |
} |