Skip to content

Instantly share code, notes, and snippets.

@mrlin0518
Last active July 5, 2018 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrlin0518/4c209e35894d1009a678b5f04ccf915f to your computer and use it in GitHub Desktop.
Save mrlin0518/4c209e35894d1009a678b5f04ccf915f to your computer and use it in GitHub Desktop.
Game-使用DAO設計模式
package Create;
import Judge.JUDGE;
public interface CreateDAO {
void CREATE();
String getName();
void count(JUDGE j);
int getWincount();
int getLosecount();
void Information();
void setACCOUNT();
void setPASSWORD();
String getAccount();
String getPassword();
}
package Create;
public class CreateDAOFactory {
public CreateDAO createDAO(){
return new CreateDAOImp();
}
}
package Create;
import Judge.JUDGE;
import Judge.JudgeDAO;
import java.text.ParseException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
public class CreateDAOImp implements CreateDAO {
Scanner sc = new Scanner(System.in);
private static int Wincount;
private static int Losecount;
private static String account;
private static String password;
private static String name;
@Override
public void setACCOUNT() {
do {
System.out.println("設定您的帳號: ");
String a = sc.next();
System.out.println("您所輸入的是: " + a + "\n是否確認?1.確認,2.重新設定,3.結束程式");
int b = sc.nextInt();
if (b == 1) {
account = a;
} else if (b == 2) {
continue;
} else if (b == 3) {
System.out.println("程式結束");
System.exit(0);
}
} while (account == null);
}
@Override
public void setPASSWORD() {
while (password == null) {
System.out.println("設定您的密碼: ");
String a = sc.next();
System.out.println("您所輸入的是: " + a + "\n是否確認?1.確認,2.重新設定,3.結束程式");
int b = sc.nextInt();
if (b == 1) {
password = a;
} else if (b == 2) {
continue;
} else if (b == 3) {
System.exit(0);
}
}
}
@Override
public void CREATE() {
while (name == null) {
try {
System.out.println("請輸入您的ID");
String a = sc.next();
System.out.println("確認ID為 " + a + " 嗎?\n1.Y\t2.N\t3.程式結束");
int b = sc.nextInt();
if (b == 1) {
this.name = a;
break;
} else if (b == 2) {
System.out.println("重新輸入您的ID");
break;
} else if (b == 3) {
System.exit(0);
}
} catch (Exception e) {
}
}
}
@Override
public void Information() {
System.out.println("Players\tName=" + name + "\n贏:\t" + Wincount
+ "\n輸:\t" + Losecount);
}
@Override
public void count(JUDGE j) {
switch (j) {
case 贏:
System.out.println(name + "贏");
Wincount++;
break;
case 輸:
System.out.println(name + "輸");
Losecount++;
break;
case 平手:
System.out.println(name + "平手");
break;
}
}
@Override
public String getAccount() {
return account;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getName() {
return name;
}
@Override
public int getWincount() {
return Wincount;
}
@Override
public int getLosecount() {
return Losecount;
}
}
package Create;
import Judge.JUDGE;
import javax.swing.JOptionPane;
public class CreateDAOImp2 implements CreateDAO {
private static int Wincount;
private static int Losecount;
private static String account;
private static String password;
private static String name;
@Override
public void setACCOUNT() {
do {
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) {
JOptionPane.showMessageDialog(null, "程式結束");
System.exit(0);//結束程式
}
} while (account == null);
}
@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) {
JOptionPane.showMessageDialog(null, "程式結束");
System.exit(0);//結束程式
}
}
}
@Override
public void CREATE() {
while (name == null) {
String a = JOptionPane.showInputDialog("請輸入您的ID");
int b = JOptionPane.showConfirmDialog(null, "確定" + a + "為您的ID嗎?",
"ID確認", JOptionPane.YES_NO_CANCEL_OPTION);
switch (b) {
case JOptionPane.YES_OPTION:
this.name = a;
break;
case JOptionPane.NO_OPTION:
this.name = null;
break;
case JOptionPane.CANCEL_OPTION:
System.exit(0);
break;
}
}
}
public void Information() {
JOptionPane.showMessageDialog(null, "Players\tName=" + name + "\n贏:\t" + Wincount
+ "\n輸:\t" + Losecount);
}
public void count(JUDGE j) {
switch (j) {
case 贏:
JOptionPane.showMessageDialog(null, name + "贏");
Wincount++;
break;
case 輸:
JOptionPane.showMessageDialog(null, name + "輸");
Losecount++;
break;
case 平手:
JOptionPane.showMessageDialog(null, name + "平手");
break;
}
}
@Override
public String getName() {
return name;
}
public int getWincount() {
return Wincount;
}
public int getLosecount() {
return Losecount;
}
public String getAccount() {
return account;
}
public String getPassword() {
return password;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment