Obliczenia brydżowe (Java ME)
Jacek Kowalski
2012-08-06 93a300758c7abc7e907b0caf1f4ae4ea87a95b2e
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.");
93a300 67             
JK 68             Display.getDisplay(this.midlet).setCurrentItem(this.kontraktField);
2e729d 69         }
JK 70         else if (command.getCommandType() == Command.EXIT) {
71             this.midlet.destroyApp(true);
72         }
73     }
74     
75     public void itemStateChanged(Item item) {
76         int kontrakt, typ, kontra, partia, punkty, lewy;
77         
78         try {
79             kontrakt = Integer.parseInt(this.kontraktField.getString());
80             typ = this.kontraktChoice.getSelectedIndex();
81             kontra = this.kontraChoice.getSelectedIndex();
82             partia = this.partiaChoice.getSelectedIndex();
83             punkty = Integer.parseInt(this.punktyField.getString());
84             lewy = Integer.parseInt(this.lewyField.getString());
85         }
86         catch(NumberFormatException e) {
87             this.resultText.setText("Podaj wszystkie dane.");
88             return;
89         }
90         
91         BrydzWynik wynik;
92         
93         String data = "";
94         
95         if( kontrakt < 0  || kontrakt > 7 ) {
96             this.resultText.setText("Niepoprawny kontrakt.");
97             return;
98         }
99         
100         if( typ != 0 && typ != 1 && typ != 2 ) {
101             this.resultText.setText("Niepoprawny typ kontraktu.");
102             return;
103         }
104         
105         if( kontra < 0 || kontra > 2 ) {
106             this.resultText.setText("Niepoprawna informacja o kontrze.");
107             return;
108         }
109         
110         if( partia != 0 && partia != 1) {
111             this.resultText.setText("Niepoprawna informacja o partii.");
112             return;
113         }
114         
115         if( punkty < 0 || punkty > 40 ) {
116             this.resultText.setText("Niepoprawna ilość punktów");
117             return;
118         }
119         
120         if( lewy < 0 || lewy > 13 ) {
121             this.resultText.setText("Niepoprawna ilość wziętych lew.");
122             return;
123         }
124         
125         if( kontrakt == 0 && punkty < 20 ) {
126             this.resultText.setText("Przy czterech pasach podaj większą liczę punktów.");
127             return;
128         }
129         
130         wynik = this.liczenie.policz(kontrakt, typ, kontra, partia, punkty, lewy);
131         
132         this.resultText.setText((wynik.dla == 0 ? "Zapis dla rozgrywających" : "Zapis dla przeciwników.")+
133                 "\nPunktów (bez PC): "+wynik.punkty_przed+
134                 "\nPunktów (z PC): "+wynik.punkty+
135                 "\nIMP-ów: "+wynik.IMP+
136                 "\nProcent: "+wynik.procent);
137     }
138 }