728x90
반응형
로그인 메인 만들기
회원가입, 아이디 찾기, 비밀번호 찾기 등
//로그인 메인
public class LoginMain {
Map<String, Member> memberMap = new HashMap<>();
public static void main(String[] args) {
// id파일 초기화(삭제)
String path = "id.txt";
File del = new File(path);
del.delete();
new GameMain().setVisible(true);
}
public static void ShowMessage(String str) {
JOptionPane.showMessageDialog(null, str);
}
public void fileSave(String str, Member member) {
try(ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("member.dat")));
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("member.dat")));
){
memberMap = (Map<String, Member>)ois.readObject();
memberMap.put(str, member);
oos.writeObject(memberMap);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
public Map<String, Member> fileRead() {
try(ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("member.dat")));){
memberMap = (Map<String, Member>)ois.readObject();
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return memberMap;
}
public void idSave(String str) {
try(OutputStream os = new FileOutputStream("id.txt");){
byte[] b = str.getBytes();
os.write(b);
}catch(IOException e) {
e.printStackTrace();
}
}
public String idRead() {
String str = null;
String str2 = null;
try(BufferedReader br = new BufferedReader(new FileReader("id.txt"));){
while((str=br.readLine()) != null) {
str2 = str;
}
} catch (IOException e) {
e.printStackTrace();
}
return str2;
}
public int resultScore() {
int score = 0;
try(ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("member.dat")));){
memberMap = (Map<String, Member>)ois.readObject();
List<Member> list = new ArrayList<>(memberMap.values());
for(int i=0; i<list.size(); i++) {
if(list.get(i).getUserId().equals(new LoginMain().idRead()))
score = list.get(i).getCoin();
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
return score;
}
}
//로그인 화면
public class Login extends JFrame{
private Map<String, Member> member = new HashMap<>();
private JTextField textIdField;
private JPasswordField passwordField;
public Login(){
super("로그인");
setSize(470,164);
setLocationRelativeTo(null); //화면 가운데 출력
JPanel newWindowContainer = new JPanel();
setContentPane(newWindowContainer); //새창 연결
newWindowContainer.setLayout(null);
JLabel labelId = new JLabel("ID");
labelId.setBounds(40,30,47,15);
add(labelId);
textIdField = new JTextField(10); //아이디 컬럼수 10
textIdField.setText(""); //it works == textIdField.getText();
textIdField.setBounds(100, 27, 167, 21);
add(textIdField);
JLabel labelPwd = new JLabel("PW");
labelPwd.setBounds(40,55,47,15);
add(labelPwd);
passwordField = new JPasswordField(20); //아이디 컬럼수 10
passwordField.setText(""); //it works == textIdField.getText();
passwordField.setBounds(100, 52, 167, 21);
add(passwordField);
JButton loginBtn = new JButton("로그인");
loginBtn.setBounds(274, 27, 102, 46);
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { //로그인 버튼 클릭시 실행될 이벤트
if(!textIdField.getText().equals("")) {
if(new LoginMain().fileRead().containsValue(new Member(textIdField.getText(), passwordField.getText(),null,null, 0 ))) {
LoginMain.ShowMessage("환영합니다!");
dispose();
new LoginMain().idSave(textIdField.getText());
}else { LoginMain.ShowMessage("잘못 입력하셨습니다. 정확히 입력해 주세요!");}
}else { LoginMain.ShowMessage("아이디, 비밀번호를 입력해주세요");}
}
});
add(loginBtn);
JButton signBtn = new JButton("회원 가입");
signBtn.setBounds(274, 80, 102, 30);
signBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SignUp().setVisible(true);
}
});
add(signBtn);
JButton findIdBtn = new JButton("ID 찾기");
findIdBtn.setBounds(40, 80, 109, 30);
findIdBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindId().setVisible(true);
}
});
add(findIdBtn);
JButton findPwBtn = new JButton("PW 찾기");
findPwBtn.setBounds(152, 80, 118, 30);
findPwBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindPw().setVisible(true);
}
});
add(findPwBtn);
}
}
//회원가입
public class SignUp extends JFrame {
private Map<String, Member> member = new HashMap<>();
private JTextField textField_Id;
private JPasswordField textField_PW;
private JPasswordField textField_PwChk;
private JTextField textField_Bday;
private JTextField textField_Email;
private boolean flag = false;
public SignUp() {
super("회원가입");
setSize(450,250);
setLocationRelativeTo(null);
JPanel newWindowContainer = new JPanel();
setContentPane(newWindowContainer);
newWindowContainer.setLayout(null);
JLabel labelId = new JLabel("ID");
labelId.setBounds(30,26,57,15);
add(labelId);
textField_Id = new JTextField(10);
textField_Id.setBounds(110, 23, 155, 21);
add(textField_Id);
JButton idCheck = new JButton("중복 확인");
idCheck.setBounds(277, 23, 145, 21);
idCheck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!textField_Id.getText().equals("")) {
if (new LoginMain().fileRead().containsKey(textField_Id.getText())) {
LoginMain.ShowMessage("이미 있는 아이디 입니다.\n사용이 불가능합니다.");
textField_Id.setText("");
} else
LoginMain.ShowMessage("사용 가능한 아이디 입니다.");
flag = true;
} else {
LoginMain.ShowMessage("아이디를 입력해주세요.");
}
}
});
add(idCheck);
JLabel labelPw = new JLabel("PW");
labelPw.setBounds(30,57,57,15);
add(labelPw);
textField_PW = new JPasswordField(20);
textField_PW.setBounds(110, 54, 155, 21);
add(textField_PW);
JLabel pwMessage = new JLabel("※8자 이상 입력해주세요");
pwMessage.setBounds(277, 58, 145, 15);
add(pwMessage);
JLabel labelPwCheck = new JLabel("PW 확인");
labelPwCheck.setBounds(30,88,57,15);
add(labelPwCheck);
textField_PwChk = new JPasswordField(20);
textField_PwChk.setBounds(110, 85, 155, 21);
add(textField_PwChk);
JButton pwChk = new JButton("패스워드 확인");
pwChk.setBounds(277, 88, 145, 15);
add(pwChk);
JTextField textField_PW2 = new JTextField(20);
textField_PW2.setBounds(110, 54, 155, 21);
JTextField textField_PwChk2 = new JTextField(20);
textField_PwChk2.setBounds(110, 85, 155, 21);
pwChk.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
String str = textField_PW.getText();
String str2 = textField_PwChk.getText();
textField_PW.setText(str);
textField_PwChk.setText(str);
add(textField_PW);
add(textField_PwChk);
}
@Override
public void mousePressed(MouseEvent e) {
String str = textField_PW.getText();
String str2 = textField_PwChk.getText();
textField_PW2.setText(str);
textField_PwChk2.setText(str);
add(textField_PW2);
add(textField_PwChk2);
}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
});
JLabel labelBday = new JLabel("Birth");
labelBday.setBounds(30,122,57,15);
add(labelBday);
textField_Bday = new JTextField();
textField_Bday.setBounds(110, 119, 155, 21);
textField_Bday.setText("YYMMDD");
textField_Bday.setEditable(true);
textField_Bday.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {}
public void focusGained(FocusEvent e) { textField_Bday.setText(""); }
});
add(textField_Bday);
JLabel labelEmail = new JLabel("Email");
labelEmail.setBounds(30,153,57,15);
add(labelEmail);
textField_Email = new JTextField();
textField_Email.setColumns(10);
textField_Email.setBounds(110, 150, 155, 21);
add(textField_Email);
JButton cancelBtn = new JButton("취소");
cancelBtn.setBounds(120,181,145,21);
add(cancelBtn);
cancelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JButton signUpBtn = new JButton("확인");
signUpBtn.setBounds(277, 181, 145, 21);
add(signUpBtn);
signUpBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(flag);
IdCheck idCheck = new IdCheck();
if (textField_Id.getText().equals("") || textField_Bday.getText().equals("") || textField_Email.getText().equals("")) {
// 한칸이라도 빈값이 있으면,
LoginMain.ShowMessage("빈값 없이 모두 입력해주세요.");
return;
}
if(!flag){
LoginMain.ShowMessage("아이디 중복확인을 해주세요.");
return;
}
if (textField_PW.getText().length()<8 || textField_PwChk.getText().length()<8) { //비밀번호 길이
LoginMain.ShowMessage("비밀 번호는 문자, 숫자로 8자 이상로 입력해주세요.");
return;
}
if (!textField_PW.getText().equals(textField_PwChk.getText())) {
LoginMain.ShowMessage("비밀번호가 일치하지 않습니다. 비밀번호를 다시 확인해주세요.");
return;
}
Member m = new Member(textField_Id.getText(), textField_PW.getText(), textField_Bday.getText(), textField_Email.getText(), 100000);
member.put(m.getUserId(), m);
new LoginMain().fileSave(textField_Id.getText(), m);
LoginMain.ShowMessage("회원 가입 되었습니다. 로그인 해주세요");
dispose();
}
});
}
}
//아이디 찾기
public class FindId extends JFrame{
private Map<String, Member> member = new HashMap<>();
Member m = new Member();
private JTextField textField_Bday;
private JTextField textField_Email;
public FindId() {
super("아이디 찾기");
setSize(350,150);
setLocationRelativeTo(null);
JPanel newWindowContainer = new JPanel();
setContentPane(newWindowContainer);
newWindowContainer.setLayout(null);
JLabel labelBday = new JLabel("Birth");
labelBday.setBounds(30,26,57,15);
add(labelBday);
textField_Bday = new JTextField();
textField_Bday.setBounds(110,23,160,21);
add(textField_Bday);
JLabel labelEmail = new JLabel("Email");
labelEmail.setBounds(30,57,57,15);
add(labelEmail);
textField_Email = new JTextField();
textField_Email.setBounds(110,54,160,21);
add(textField_Email);
JButton cancelBtn = new JButton("취소");
cancelBtn.setBounds(30,85,135,21);
add(cancelBtn);
cancelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JLabel findResult = new JLabel("");
findResult.setHorizontalAlignment(SwingConstants.CENTER);
findResult.setBounds(26,113,435,15);
add(findResult);
JButton findPwBtn = new JButton("확인");
findPwBtn.setBounds(175,85,135,21);
add(findPwBtn);
findPwBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str = "";
if (textField_Email.getText().equals("") || textField_Bday.getText().equals("")) {
LoginMain.ShowMessage("생년월일과 이메일을 입력해주세요.");
return;
} else
str = FindIdValue(textField_Email.getText(), textField_Bday.getText());
System.out.println(str);
if (str.equals("")) {
LoginMain.ShowMessage("해당하는 아이디가 존재하지 않습니다.");
} else {
LoginMain.ShowMessage("고객님의 아이디는 " + str + " 입니다.");
dispose();
}
}
});
}
private String FindIdValue(String eMail, String bDay) {
String str = "";
List<Member> list = new ArrayList<>(new LoginMain().fileRead().values());
for(int i=0; i<list.size(); i++) {
if(list.get(i).geteMail().equals(eMail) && list.get(i).getBirthDay().equals(bDay)) {
System.out.println(list.get(i).getUserId());
str = list.get(i).getUserId();
break;
}
}
return str;
}
}
//비밀번호 찾기
public class FindPw extends JFrame{
private Map<String, Member> member = new HashMap<>();
Member m = new Member();
private JTextField textField_Id;
private JTextField textField_Bday;
public FindPw() {
super("비밀번호 찾기");
setSize(350,150);
setLocationRelativeTo(null);
JPanel newWindowContainer = new JPanel();
setContentPane(newWindowContainer);
newWindowContainer.setLayout(null);
JLabel labelId = new JLabel("ID");
labelId.setBounds(30,26,57,15);
add(labelId);
textField_Id = new JTextField();
textField_Id.setBounds(110,23,160,21);
add(textField_Id);
JLabel labelBday = new JLabel("Birth");
labelBday.setBounds(30,57,57,15);
add(labelBday);
textField_Bday = new JTextField();
textField_Bday.setBounds(110,54,160,21);
add(textField_Bday);
JButton cancelBtn = new JButton("취소");
cancelBtn.setBounds(30,85,135,21);
add(cancelBtn);
cancelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JLabel findResult = new JLabel("");
findResult.setHorizontalAlignment(SwingConstants.CENTER);
findResult.setBounds(26,113,435,15);
add(findResult);
JButton findPwBtn = new JButton("확인");
findPwBtn.setBounds(175,85,135,21);
add(findPwBtn);
findPwBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str = "";
if (textField_Id.getText().equals("") || textField_Bday.getText().equals("")) {
LoginMain.ShowMessage("아이디와 생년월일을 입력해주세요.");
return;
} else
str = FindPwValue(textField_Id.getText(), textField_Bday.getText());
System.out.println(str);
if (str.equals("")) {
LoginMain.ShowMessage("해당하는 아이디가 존재하지 않습니다.");
} else {
LoginMain.ShowMessage("고객님의 비밀번호는 " + str + " 입니다.");
dispose();
}
}
});
}
private String FindPwValue(String userId, String bDay) {
String str = "";
List<Member> list = new ArrayList<>(new LoginMain().fileRead().values());
for(int i=0; i<list.size(); i++) {
if(list.get(i).getUserId().equals(userId) && list.get(i).getBirthDay().equals(bDay)) {
System.out.println(list.get(i).getUserPwd());
str = list.get(i).getUserPwd();
break;
}
}
return str;
}
}
//회원가입시 확인항목
public class IdCheck{
public IdCheck() {}
public boolean PwLengthChk(String pw) {
if(pw.length()<8)
return false;
else {
return true;
}
}
public boolean PwMatch(JPasswordField textField_PW, JPasswordField textField_PwChk) {
if(textField_PW.equals(textField_PwChk))
return true;
else
return false;
}
}
//멤버
public class Member implements Serializable{
private static final long serialVersionUID = 1L;
private String userId;
private String userPwd;
private String birthDay;
private String eMail;
private int coin;
public Member() {}
public Member(String userId, String userPwd) {
this.userId = userId;
this.userPwd = userPwd;
}
public Member(String userId, String userPwd, String birthDay, String eMail) {
this.userId = userId;
this.userPwd = userPwd;
this.birthDay = birthDay;
this.eMail = eMail;
}
public Member(String userId, String userPwd, String birthDay, String eMail, int coin) {
this.userId = userId;
this.userPwd = userPwd;
this.birthDay = birthDay;
this.eMail = eMail;
this.coin = coin;
}
public String getUserId() {return userId;}
public void setUserId(String userId) {this.userId = userId;}
public String getUserPwd() {return userPwd;}
public void setUserPwd(String userPwd) {this.userPwd = userPwd;}
public String getBirthDay() {return birthDay;}
public void setBirthDay(String birthDay) {this.birthDay = birthDay;}
public String geteMail() {return eMail;}
public void seteMail(String eMail) {this.eMail = eMail;}
public int getCoin() {return coin;}
public void setCoin(int coin) {this.coin = coin;}
@Override
public String toString() {
return "아이디 : " + userId + ", 패스워드 : " + userPwd + ", 생년월일 : " + birthDay + ", eMail : " + eMail + "코인 : " + coin;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((birthDay == null) ? 0 : birthDay.hashCode());
result = prime * result + ((eMail == null) ? 0 : eMail.hashCode());
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
result = prime * result + ((userPwd == null) ? 0 : userPwd.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Member other = (Member) obj;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
if (userPwd == null) {
if (other.userPwd != null)
return false;
} else if (!userPwd.equals(other.userPwd))
return false;
return true;
}
}
반응형
'프로그래밍 > JAVA' 카테고리의 다른 글
05.08(Mini Project - 자바 스윙을 이용하여 게임 만들기3) (0) | 2020.05.08 |
---|---|
05.07(Mini Project - 자바 스윙을 이용하여 게임 만들기2) (0) | 2020.05.07 |
05.05(IO, List예제) (0) | 2020.05.05 |
05.04(BufferedReader 예제) (0) | 2020.05.04 |
05.01(API, 예외처리, map예제) (0) | 2020.05.01 |