JavaでConsoleアプリ(その2)
TwitterClientにできるよう、IDとPasswordの入力に対する機能を追加。
Password入力の部分は、いけていないと思います。
import static java.lang.System.in; import static java.lang.System.out; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Console main. * @author itengineer * */ public class ConsoleMain { private static final String CONSOLE_TITLE = "[ConsoleApp]"; private static final String WAIT_SUBJECT = CONSOLE_TITLE + "> "; private static final int ID_INPUT_REQUIRED_MAX_COUNT = 2; private static int ID_INPUT_REQUIRED_COUNT = 0; private static final int PASSWORD_INPUT_REQUIRED_MAX_COUNT = 2; private static int PASSWORD_INPUT_REQUIRED_COUNT = 0; private static BufferedReader input; /** * Entry point. * IDのみ起動パラメータで受け取る。 * @param args */ public static void main(String[] args) { try { input = new BufferedReader(new InputStreamReader(in)); // ID入力確認 String id = getId(args); if ("".equals(id)) { System.exit(0); } // パスワード入力確認 String pw = getPassword(); if (pw.equals("")) { System.exit(0); } // ログイン // 起動 out.println("Hi!\nI'm Console application."); out.print(WAIT_SUBJECT); while (true) { String inval = input.readLine(); String commandCode = inval.split(" ")[0]; if ("bye".equals(commandCode) || "exit".equals(commandCode) || "quit".equals(commandCode)) { break; } else { // ログインとパスワードを表示する。 if ("showme".equals(commandCode)) { out.println("Your information is ...\nID:[" + id + "]\nPW:[" + pw + "]"); } } // 入力値に対する出力 out.print(WAIT_SUBJECT); } out.println("Bye."); } catch (IOException e) { e.printStackTrace(); System.exit(0); } } /** * IDの入力値を得る。 * @param args * @return * @throws IOException */ private static String getId(String[] args) throws IOException { String id = ""; if (args.length == 0 || "".equals(args[0])) { out.println(CONSOLE_TITLE + "Please input your id."); out.print(CONSOLE_TITLE + "Twitter ID:"); id = input.readLine(); if ("".equals(id)) { // ID未入力をカウント。 ID_INPUT_REQUIRED_COUNT++; if (ID_INPUT_REQUIRED_MAX_COUNT <= ID_INPUT_REQUIRED_COUNT) { // disる。 out.println("ゴルア!"); } else { id = getId(args); } } } else { id = args[0]; } return id; } /** * Passwordの入力値を得る。 * @return * @throws IOException */ private static String getPassword() throws IOException { out.println(CONSOLE_TITLE + "Please input your password."); out.print(CONSOLE_TITLE + "PASSWORD: "); String pw = input.readLine(); if ("".equals(pw)) { // Password未入力をカウント PASSWORD_INPUT_REQUIRED_COUNT++; if (PASSWORD_INPUT_REQUIRED_MAX_COUNT <= PASSWORD_INPUT_REQUIRED_COUNT) { // disる。 out.println("ゴルア!!"); } else { pw = getPassword(); } } return pw; } }