package brydz; 
 | 
  
 | 
import javax.microedition.lcdui.*; 
 | 
  
 | 
public class BrydzForm extends Form implements CommandListener, ItemStateListener { 
 | 
    protected BrydzMIDlet midlet; 
 | 
     
 | 
    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); 
 | 
         
 | 
        setItemStateListener(this); 
 | 
        setCommandListener(this); 
 | 
        addCommand(new Command("Wyczyść", Command.OK, 1)); 
 | 
        addCommand(new Command("Wyjście", Command.EXIT, 1)); 
 | 
    } 
 | 
     
 | 
    public void commandAction(Command command, Displayable displayable) { 
 | 
        if (command.getCommandType() == Command.OK) { 
 | 
            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."); 
 | 
             
 | 
            Display.getDisplay(this.midlet).setCurrentItem(this.kontraktField); 
 | 
        } 
 | 
        else if (command.getCommandType() == Command.EXIT) { 
 | 
            this.midlet.destroyApp(true); 
 | 
        } 
 | 
    } 
 | 
     
 | 
    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); 
 | 
    } 
 | 
} 
 |