import java.util.Scanner; import javax.swing.JOptionPane; public class PlayerMan extends Player { public static final byte CONSOLE = 0; public static final byte INPUT_DIALOG = 1; private byte controller = 0; PlayerMan(String name, Shared game, byte controller) { super(name, game); this.controller = controller; } public int getFromConsole() { Scanner scanner = new Scanner(System.in); String inputString=""; boolean inputOk=false; Integer liczba=0; while(inputOk == false) { System.out.print(this.name + ", podaj ile zapalek chcesz podniesc (1 - 3):"); inputString = scanner.next(); if(inputString.equals("exit")) { System.exit(1); } try { liczba = Integer.parseInt(inputString); if((liczba instanceof Integer) == true) { if(liczba >= 1 && liczba <= 3) { if(this.game.ileZapalek >= liczba) { inputOk = true; } else { System.err.println("Na stole nie ma " + liczba + " zapalek!"); } } else { System.err.println("Podaj liczbe od 1 do 3!"); } } } catch(NumberFormatException e) { System.err.println("Podaj liczbe!"); } } return liczba; } public int getFromDialog() { String inputString=""; boolean inputOk=false; Integer liczba=0; while(inputOk == false) { inputString = JOptionPane.showInputDialog(this.name + ", podaj ile zapalek chcesz podniesc (1 - 3):"); if(inputString.equals("exit")) { System.exit(1); } try { liczba = Integer.parseInt(inputString); if((liczba instanceof Integer) == true) { if(liczba >= 1 && liczba <= 3) { if(this.game.ileZapalek >= liczba) { inputOk = true; } else { System.err.println("Na stole nie ma " + liczba + " zapalek!"); } } else { System.err.println("Podaj liczbe od 1 do 3!"); } } } catch(NumberFormatException e) { System.err.println("Podaj liczbe!"); } } return liczba; } public int getIlosc() { if(this.controller == PlayerMan.CONSOLE) return this.getFromConsole(); else return this.getFromDialog(); } }