//主程式
import java.util.Scanner;
public class login {
static Scanner sc = new Scanner(System.in);
static loginDAO log;
public static void main(String[] args) {
loginDAOFactory factory = new loginDAOFactory();
log = factory.creatLoginDAO();
log.setACCOUNT();
log.setPASSWORD();
while (log.Login() == false) {
System.out.println("Please agin");
}
}
}
public interface loginDAO {
void setACCOUNT();
void setPASSWORD();
Boolean Login();
}
public class loginDAOFactory {
public loginDAO creatLoginDAO(){
return new loginImp2();
}
}
第一組使用System.in
import java.util.Scanner;
public class loginImp implements loginDAO {
Scanner sc = new Scanner(System.in);
private String account;
private String password;
@Override
public void setACCOUNT() {
while (account == null) {
System.out.print("設定您的帳號: ");
String a = sc.next();
System.out.print("您所輸入的是: " + a + "\n是否確認?1.確認,2.重新設定,3.結束程式");
int b = sc.nextInt();
if (b == 1) {
account = a;
}else if (b==3) {
System.exit(0);
}
}
}
@Override
public void setPASSWORD() {
while (password == null) {
System.out.println("設定您的密碼: ");
String a = sc.next();
System.out.print("您所輸入的是: " + a + "\n是否確認?1.確認,2.重新設定,3.結束程式");
int b = sc.nextInt();
if (b == 1) {
password = a;
}else if (b==3) {
System.exit(0);
}
}
}
@Override
public Boolean Login() {
System.out.println("輸入您的帳號: ");
String a = sc.next();
System.out.println("輸入您的密碼: ");
String b = sc.next();
if (a.equals(account) != true || b.equals(password) != true) {
System.out.println("登入失敗,帳號或密碼錯誤。");
return false;
}
System.out.println("登入成功!");
System.out.println("-------------------------");
return true;
}
}
第二組使用 JOptionPane 浮動視窗
import javax.swing.JOptionPane;
public class loginImp2 implements loginDAO {
private String account;
private String password;
@Override
public void setACCOUNT() {
while (account == null) {
String a = JOptionPane.showInputDialog("設定您的帳號:");
int b = JOptionPane.showConfirmDialog(null, "確定 " + a + " 為您的帳號嗎? ",
"帳號確認", +JOptionPane.YES_NO_CANCEL_OPTION);
if (b == JOptionPane.YES_OPTION) {
account = a;
} else if (b == JOptionPane.CANCEL_OPTION) {
System.exit(0);//結束程式
}
}
}
@Override
public void setPASSWORD() {
while (password == null) {
String a = JOptionPane.showInputDialog("設定您的密碼:");
int b = JOptionPane.showConfirmDialog(null, "確定 " + a + " 為您的密碼嗎? ",
"密碼確認", +JOptionPane.YES_NO_CANCEL_OPTION);
if (b == JOptionPane.YES_OPTION) {
password = a;
} else if (b == JOptionPane.CANCEL_OPTION) {
System.exit(0);//結束程式
}
}
}
@Override
public Boolean Login() {
String a = JOptionPane.showInputDialog("輸入您的帳號");
String b = JOptionPane.showInputDialog("輸入您的密碼");
if (a.equals(account) != true || b.equals(password) != true) {
JOptionPane.showMessageDialog(null, "登入失敗,帳號或密碼錯誤。",
"登入訊息", JOptionPane.ERROR_MESSAGE);
return false;
}
JOptionPane.showMessageDialog(null, "登入成功!", "登入訊息",
JOptionPane.PLAIN_MESSAGE);
return true;
}
}
留言列表