package day17_io;

/*

2.

- 회원아이디,비밀번호,이메일에 대한 정보를 갖는 클래스를 만들고 member.data파일로 저장하세요.

- 조회할 회원의 비밀번호와 이메일주소를 입력받아 회원아이디를 출력해 보세요.

회원아이디는 앞글자 3자만 보여지고 나머지는 *로 채워집니다.

==> ObjectInputStream,ObjectOutputStream을 사용합니다.

*/

import java.io.Serializable;

import java.util.Scanner;

import java.io.EOFException;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

class Account implements Serializable {

private String id;

private String pw;

private String email;

 

 

public Account(String id, String pw, String email) {

this.id=id;

this.pw=pw;

this.email=email;

}

public String getId() {return id;}

public String getPw() {return pw;}

public String getEmail() {return email;}

}

public class Task2 {

public static void main(String[] args) {

try {

output();

input();

}catch(ClassNotFoundException ce) {

System.out.println(ce.getMessage());

}catch(IOException ie) {

System.out.println(ie.getMessage());

}

}

public static void output() throws IOException {

 

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("member.data"));

oos.writeObject(new Account("jhta123","a12345","java@jhta.net"));

oos.writeObject(new Account("jhta1234","b12345","jsp@jhta.net"));

oos.writeObject(new Account("jhta12345","c12345","spring@jhta.net"));

oos.close();

System.out.println("회원아이디,비밀번호,이메일 정보를 담는 객체 저장완료..");

}

public static void input() throws IOException,ClassNotFoundException{

Scanner scan = new Scanner(System.in);

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("member.data"));

System.out.println("조회 할 회원님의 아이디를 찾기위해 비밀번호와 이메일주소를 입력하세요.");

System.out.print("검색할 ID의 비밀번호:");

String searchPw = scan.next();

System.out.print("검색할 ID의 이메일주소:");

String searchEmail = scan.next();

 

while(true) {

try {

Account ob = (Account)ois.readObject();

//꺼내오는 내용의 데이터 타입이 Account 타입인데 Object타입으로 받았으므로 자식메소드못씀->형변환필요

String fullId = ob.getId(); // 아이디 전체 문자열

String showingId = fullId.substring(0,3);// 앞 세자리 3자리(뒤에 '3'은 범위에 포함안됨)

//substring: 문자열에서 해당범위에있는 문자열을 반환

int markId = fullId.length()-3; // 3자리 아이디표시 이후 4번째 자리부터의 문자열길이

char[] ch = new char[markId]; //* 표시를 저장하기위한 공간(배열)을 생성

for(int i =0; i<markId; i++){

ch[i]='*';

}

String star = String.valueOf(ch);

//valueOf() 메소드는 ()괄호 안의 해당 객체를 String 객체로 변환시키는 역활을 합니다. 말그대로 String의 객체로 형변환입니다.

if(searchPw.equals(ob.getPw()) && searchEmail.equals(ob.getEmail())) { //논리연산자 and(&&)

System.out.println("회원님의 아이디:"+showingId+ star+" "+"입니다.");

break;

}

}catch(EOFException ee) {

System.out.println("일치하는 회원이 존재하지 않습니다...");

//java.io.EOFException; (IOExeption에 포함)파일의 끝에 도달했을 때 발생

break;

}

}

}

}

Task2.java
0.00MB
Task1.java
0.00MB

+ Recent posts