Obliczenia brydżowe (Java ME)
Jacek Kowalski
2012-08-08 fb3d21dd264151dce808d7698ee0d8a28d6f227e
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;
fb3d21 7     protected BrydzPomocForm help = null;
JK 8     protected BrydzPomocForm about = null;
2e729d 9     
JK 10     protected TextField kontraktField;
11     protected ChoiceGroup kontraktChoice;
12     protected ChoiceGroup kontraChoice;
13     protected ChoiceGroup partiaChoice;
14     protected TextField punktyField;
15     protected TextField lewyField;
16     protected StringItem resultText;
17     
18     protected BrydzLicz liczenie;
19     
20     public BrydzForm(BrydzMIDlet midlet) {
21         super("Obliczenia brydżowe");
22         
23         this.midlet = midlet;
24         this.liczenie = new BrydzLicz();
25         
26         this.kontraktField = new TextField("Kontrakt:", null, 1, TextField.NUMERIC);
27         this.kontraktChoice = new ChoiceGroup(null, List.EXCLUSIVE);
28         this.kontraktChoice.append("♣/♦", null);
29         this.kontraktChoice.append("♥/♠", null);
30         this.kontraktChoice.append("NT", null);
31         
32         this.kontraChoice = new ChoiceGroup(null, ChoiceGroup.EXCLUSIVE);
33         this.kontraChoice.append("norm.", null);
34         this.kontraChoice.append("ktr.", null);
35         this.kontraChoice.append("rektr.", null);
36         
37         this.partiaChoice = new ChoiceGroup(null, ChoiceGroup.EXCLUSIVE);
dcd918 38         this.partiaChoice.append("przed / przed", null);
JK 39         this.partiaChoice.append("przed / po", null);
40         this.partiaChoice.append("po / przed", null);
41         this.partiaChoice.append("po / po", null);
2e729d 42         
JK 43         this.punktyField = new TextField("PC:", null, 2, TextField.NUMERIC);
44         this.lewyField = new TextField("Lew:", null, 2, TextField.NUMERIC);
45         
46         this.resultText = new StringItem(null, "Podaj wszystkie dane");
47         
48         this.append(this.kontraktField);
49         this.append(this.kontraktChoice);
50         this.append(this.kontraChoice);
51         this.append(this.partiaChoice);
52         this.append(this.punktyField);
53         this.append(this.lewyField);
54         this.append(this.resultText);
55         
bf2df0 56         this.addCommand(new Command("Wyczyść", Command.BACK, 1));
fb3d21 57         this.addCommand(new Command("Pomoc", Command.HELP, 2));
JK 58         this.addCommand(new Command("O programie", Command.HELP, 3));
59         this.addCommand(new Command("Wyjście", Command.EXIT, 4));
bf2df0 60         
JK 61         this.setItemStateListener(this);
62         this.setCommandListener(this);
2e729d 63     }
JK 64     
65     public void commandAction(Command command, Displayable displayable) {
fb3d21 66         switch(command.getCommandType()) {
JK 67             case Command.EXIT:
68                 this.midlet.destroyApp(true);
69             break;
70             case Command.CANCEL:
71             case Command.BACK:
72                 this.kontraktField.setString(null);
73                 this.kontraktChoice.setSelectedIndex(0, true);
74                 this.kontraChoice.setSelectedIndex(0, true);
75                 this.partiaChoice.setSelectedIndex(0, true);
76                 this.punktyField.setString(null);
77                 this.lewyField.setString(null);
78                 this.resultText.setText("Podaj wszystkie dane.");
79
80                 this.midlet.display.setCurrentItem(this.kontraktField);
81             break;
82             case Command.HELP:
83                 if(command.getPriority() == 2) {
84                     if(this.help == null) {
85                         this.help = new BrydzPomocForm(this.midlet, this, false);
86                     }
87                     this.midlet.display.setCurrent(this.help);
88                 }
89                 else if(command.getPriority() == 3) {
90                     if(this.about == null) {
91                         this.about = new BrydzPomocForm(this.midlet, this, true);
92                     }
93                     this.midlet.display.setCurrent(this.about);
94                 }
95             break;
2e729d 96         }
JK 97     }
98     
99     public void itemStateChanged(Item item) {
100         int kontrakt, typ, kontra, partia, punkty, lewy;
101         
102         try {
103             kontrakt = Integer.parseInt(this.kontraktField.getString());
104             typ = this.kontraktChoice.getSelectedIndex();
105             kontra = this.kontraChoice.getSelectedIndex();
106             partia = this.partiaChoice.getSelectedIndex();
107             punkty = Integer.parseInt(this.punktyField.getString());
108             lewy = Integer.parseInt(this.lewyField.getString());
109         }
110         catch(NumberFormatException e) {
111             this.resultText.setText("Podaj wszystkie dane.");
112             return;
113         }
114         
115         BrydzWynik wynik;
dcd918 116                 
2e729d 117         if( kontrakt < 0  || kontrakt > 7 ) {
JK 118             this.resultText.setText("Niepoprawny kontrakt.");
119             return;
120         }
121         
122         if( typ != 0 && typ != 1 && typ != 2 ) {
123             this.resultText.setText("Niepoprawny typ kontraktu.");
124             return;
125         }
126         
127         if( kontra < 0 || kontra > 2 ) {
128             this.resultText.setText("Niepoprawna informacja o kontrze.");
129             return;
130         }
131         
dcd918 132         if( partia < 0 || partia > 3) {
2e729d 133             this.resultText.setText("Niepoprawna informacja o partii.");
JK 134             return;
135         }
136         
137         if( punkty < 0 || punkty > 40 ) {
138             this.resultText.setText("Niepoprawna ilość punktów");
139             return;
140         }
141         
142         if( lewy < 0 || lewy > 13 ) {
143             this.resultText.setText("Niepoprawna ilość wziętych lew.");
144             return;
145         }
146         
147         if( kontrakt == 0 && punkty < 20 ) {
148             this.resultText.setText("Przy czterech pasach podaj większą liczę punktów.");
149             return;
150         }
151         
152         wynik = this.liczenie.policz(kontrakt, typ, kontra, partia, punkty, lewy);
153         
154         this.resultText.setText((wynik.dla == 0 ? "Zapis dla rozgrywających" : "Zapis dla przeciwników.")+
155                 "\nPunktów (bez PC): "+wynik.punkty_przed+
156                 "\nPunktów (z PC): "+wynik.punkty+
157                 "\nIMP-ów: "+wynik.IMP+
158                 "\nProcent: "+wynik.procent);
159     }
160 }