/* * Copyright (c) 2012, Jacek Kowalski (http://jacekk.info) * Wszystkie prawa zastrzeżone * * Licencja w pliku BSD-3.txt */ package brydz; import javax.microedition.lcdui.*; public class BrydzForm extends Form implements CommandListener, ItemStateListener { protected BrydzMIDlet midlet; protected BrydzPomocForm help = null; protected BrydzPomocForm about = null; protected TextField kontraktField; protected ChoiceGroup kontraktChoice; protected ChoiceGroup kontraChoice; protected ChoiceGroup partiaChoice; protected TextField punktyField; protected TextField lewyField; protected StringItem resultText; protected BrydzLicz liczenie; public BrydzForm(BrydzMIDlet midlet) { super("Obliczenia brydżowe"); this.midlet = midlet; this.liczenie = new BrydzLicz(); this.kontraktField = new TextField("Kontrakt:", null, 1, TextField.NUMERIC); this.kontraktChoice = new ChoiceGroup(null, List.EXCLUSIVE); this.kontraktChoice.append("♣/♦", null); this.kontraktChoice.append("♥/♠", null); this.kontraktChoice.append("NT", null); this.kontraChoice = new ChoiceGroup(null, ChoiceGroup.EXCLUSIVE); this.kontraChoice.append("norm.", null); this.kontraChoice.append("ktr.", null); this.kontraChoice.append("rektr.", null); this.partiaChoice = new ChoiceGroup(null, ChoiceGroup.EXCLUSIVE); this.partiaChoice.append("przed / przed", null); this.partiaChoice.append("przed / po", null); this.partiaChoice.append("po / przed", null); this.partiaChoice.append("po / po", null); this.punktyField = new TextField("PC:", null, 2, TextField.NUMERIC); this.lewyField = new TextField("Lew:", null, 2, TextField.NUMERIC); this.resultText = new StringItem(null, "Podaj wszystkie dane"); this.append(this.kontraktField); this.append(this.kontraktChoice); this.append(this.kontraChoice); this.append(this.partiaChoice); this.append(this.punktyField); this.append(this.lewyField); this.append(this.resultText); this.addCommand(new Command("Wyczyść", Command.BACK, 1)); this.addCommand(new Command("Pomoc", Command.HELP, 2)); this.addCommand(new Command("O programie", Command.HELP, 3)); this.addCommand(new Command("Wyjście", Command.EXIT, 4)); this.setItemStateListener(this); this.setCommandListener(this); } public void commandAction(Command command, Displayable displayable) { switch(command.getCommandType()) { case Command.EXIT: this.midlet.destroyApp(true); break; case Command.CANCEL: case Command.BACK: this.kontraktField.setString(null); this.kontraktChoice.setSelectedIndex(0, true); this.kontraChoice.setSelectedIndex(0, true); this.partiaChoice.setSelectedIndex(0, true); this.punktyField.setString(null); this.lewyField.setString(null); this.resultText.setText("Podaj wszystkie dane."); this.midlet.display.setCurrentItem(this.kontraktField); break; case Command.HELP: if(command.getPriority() == 2) { if(this.help == null) { this.help = new BrydzPomocForm(this.midlet, this, false); } this.midlet.display.setCurrent(this.help); } else if(command.getPriority() == 3) { if(this.about == null) { this.about = new BrydzPomocForm(this.midlet, this, true); } this.midlet.display.setCurrent(this.about); } break; } } public void itemStateChanged(Item item) { int kontrakt, typ, kontra, partia, punkty, lewy; try { kontrakt = Integer.parseInt(this.kontraktField.getString()); typ = this.kontraktChoice.getSelectedIndex(); kontra = this.kontraChoice.getSelectedIndex(); partia = this.partiaChoice.getSelectedIndex(); punkty = Integer.parseInt(this.punktyField.getString()); lewy = Integer.parseInt(this.lewyField.getString()); } catch(NumberFormatException e) { this.resultText.setText("Podaj wszystkie dane."); return; } BrydzWynik wynik; if( kontrakt < 0 || kontrakt > 7 ) { this.resultText.setText("Niepoprawny kontrakt."); return; } if( typ != 0 && typ != 1 && typ != 2 ) { this.resultText.setText("Niepoprawny typ kontraktu."); return; } if( kontra < 0 || kontra > 2 ) { this.resultText.setText("Niepoprawna informacja o kontrze."); return; } if( partia < 0 || partia > 3) { this.resultText.setText("Niepoprawna informacja o partii."); return; } if( punkty < 0 || punkty > 40 ) { this.resultText.setText("Niepoprawna ilość punktów"); return; } if( lewy < 0 || lewy > 13 ) { this.resultText.setText("Niepoprawna ilość wziętych lew."); return; } if( kontrakt == 0 && punkty < 20 ) { this.resultText.setText("Przy czterech pasach podaj większą liczę punktów."); return; } wynik = this.liczenie.policz(kontrakt, typ, kontra, partia, punkty, lewy); this.resultText.setText((wynik.dla == 0 ? "Zapis dla rozgrywających" : "Zapis dla przeciwników.")+ "\nPunktów (bez PC): "+wynik.punkty_przed+ "\nPunktów (z PC): "+wynik.punkty+ "\nIMP-ów: "+wynik.IMP+ "\nProcent: "+wynik.procent); } }