스포츠마케터의 초보 개발자 도전기
JAVA study 19 본문
objStream
package ex1_objStream;
import java.io.Serializable;
//implements Serializable을 통해 메모리에 흩어져있던 멤버변수들을 한번에
//쓰고 받을 수 있도록 새로운 영역에 일렬로 만들어 복사해둔다 (직렬화)
public class RspInfo implements Serializable {
private int win, lose, draw;
private String id;
public int getWin() {
return win;
}
public void setWin(int win) {
this.win = win;
}
public int getLose() {
return lose;
}
public void setLose(int lose) {
this.lose = lose;
}
public int getDraw() {
return draw;
}
public void setDraw(int draw) {
this.draw = draw;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
--
package ex1_objStream;
import java.util.Random;
import java.util.Scanner;
public class RspMain {
public static void main(String[] args) {
// id : aaa
// 0승 0무 0패
// 가위(s) | 바위(r) | 보(p)
// 당신이 이겼습니다
// 1승 0무 0패
// 다시한판? y | n
// 게임종료
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
RspInfo rinfo = new RspInfo();
System.out.print("id : ");
String id = sc.next();
rinfo.setId(id);
// 로드
ScoreLoader sl = new ScoreLoader(rinfo);
rinfo = sl.info;
int win = 0;
int draw = 0;
int lose = 0;
win = rinfo.getWin();
draw = rinfo.getDraw();
lose = rinfo.getLose();
System.out.printf("%d승 %d무 %d패\n", rinfo.getWin(), rinfo.getDraw(), rinfo.getLose());
while (true) {
int random = rnd.nextInt(3);
// 0:가위, 1:바위, 2:보
System.out.println("가위(s) | 바위(r) | 보(p)");
String user = sc.next();
int usercnt = 0;
if (user.equals("s")) {
usercnt = 0;
} else if (user.equals("r")) {
usercnt = 1;
} else if (user.equals("p")) {
usercnt = 2;
}
// 경우의 수
if (usercnt - random == -2 || usercnt - random == 1) {
System.out.println("이겼습니다");
rinfo.setWin(++win);
} else if (usercnt - random == 0) {
System.out.println("비겼습니다");
rinfo.setDraw(++draw);
} else {
System.out.println("졌습니다");
rinfo.setLose(++lose);
}
System.out.printf("%d승 %d무 %d패\n", rinfo.getWin(), rinfo.getDraw(), rinfo.getLose());
System.out.println("한판 더 ? y | n : ");
if (!sc.next().equals("y")) {
break;
}
} // while
// 게임이 정상적으로 끝나면 user별 기록을 저장
new ScoreWriter(rinfo);
}// main
}
--
package ex1_objStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ScoreWriter {
RspInfo info;
public ScoreWriter(RspInfo info) {
this.info = info;
save();
}
private void save() {
String path = "C:/java_kms/RspGame/" + info.getId() + "/UserInfo.sav";
File f1 = new File("C:/java_kms/RspGame/" + info.getId());
if (!f1.exists()) {
f1.mkdirs();
}
// 파일쓰키
// ObjectStream은 클래스를 저장하기 위한 스트림
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
oos = new ObjectOutputStream(fos);
oos.writeObject(info);
System.out.println("기록저장");
} catch (Exception e) {
System.out.println("저장실패");
e.printStackTrace();
} finally {
try {
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
--
package ex1_objStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
public class ScoreLoader {
RspInfo info;
public ScoreLoader(RspInfo info) {
this.info = info;
info = new RspInfo();
load();
}
private void load() {
String path = "C:/java_kms/RspGame/" + info.getId() + "/UserInfo.sav";
File f = new File(path);
if (f.exists()) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
info = (RspInfo) ois.readObject();
System.out.println("로드성공");
} catch (Exception e) {
System.out.println("로드실패");
e.printStackTrace();
} finally {
try {
ois.close();
fis.close();
} catch (Exception e2) {
}
}
} else {
System.out.println("새로운 회원");
}
}
}
frame
package ex2_frame;
import java.awt.Frame;
public class Ex1_Frame {
public static void main(String[] args) {
Frame f = new Frame("나는 제목입니다.");
f.setSize(400, 400);
f.setLocation(800, 100);
f.setVisible(true);
}// main
}
--
package ex2_frame;
import java.awt.Color;
import java.awt.Frame;
public class FrameMain {
public static void main(String[] args) {
MyFrame f1 = new MyFrame();
f1.setTitle("f1의 타이틀");
f1.setBackground(Color.CYAN);
MyFrame f2 = new MyFrame();
f2.setTitle("f2의 타이틀");
f2.setBackground(Color.YELLOW);
}// mainR
}
--
package ex2_frame;
import java.awt.Frame;
public class MyFrame extends Frame {
public MyFrame() {
// setSize(400, 400);
// setLocation(500, 200);
// size와 location 한번에
setBounds(500, 200, 400, 400);
setVisible(true);
}
}
window_listener
package ex3_window_listener;
import java.awt.Frame;
public class FrameMain {
public static void main(String[] args) {
Frame f = new Frame();
f.setBounds(400, 200, 300, 200);
//frame에서 감지자 등록
}// main
}
'develop > JAVA' 카테고리의 다른 글
JAVA study 21 (0) | 2024.05.09 |
---|---|
JAVA study 20 (0) | 2024.05.09 |
JAVA study 18 (0) | 2024.03.21 |
JAVA study 17 (0) | 2024.03.20 |
JAVA study 16 (0) | 2024.03.19 |