JAVA
#12 [Java]
dP fla
2022. 10. 27. 14:49
변수 저장하는 시점:
나중에 사용 , 2번이상 사용
while문 고려 : 언제 break?
new : 오른쪽 heap
참조선이 없는 경우 : NULL
not have been initialized : 스택.. 힙.. 참조선 ..........
사용할지 안할지? -> 선언만 ....
무조건 사용 -> 선언과 동시에 할당으로 해결!
new String(); -> 할당
힙에다가 저장하려면 String이란 클래스(heap에 문자를 저장할 수 있는)가 필요하다 ..
클래스를 통해 힙에다가 저장
null : 클래스 참조선이 아니라 데이터 참조선이 없는 경우
💡 모든 테스트 케이스를 파악하는지 ?
message를 null로 초기화
message = null;
💡 Stack 데이터는 참조선이 없음 : null 불가
💡 문제가 생겼을때는 문제가 생긴 시점만 확인하려고 하지말기
거시적인 시선으로 미시적인 문제를 해결, 미시적인 시선으로 거시적인 문제를 해결하기는 어려움
변환이 가능하다면 사용자의 입력값을 숫자로 변환
숫자로 변환이 가능하면 범위에 들어가는지 확인
loopCheck = false;
package front;
import java.util.Scanner;
import back.Back;
public class Front2 {
public Front2(int recordIdx) {
this.controller(recordIdx);
}
public void controller(int recordIndex) {
Scanner scanner = new Scanner(System.in);
String title = this.makeTitle();
String[][] menu = { { "EXIT", "프로그램을 종료합니다.~" }, { "메인", "연산하기", "끝내기" },
{ "연산", "새로운 연산", "이어서 연산", "이전화면" } };
String userData;
String message = new String();
int selectMenu;
boolean loopCheck = true;
while (true) {
this.display(title);
if (message != null)
this.display(message);
this.display(this.makeSubMenu(menu[recordIndex]));
if (!loopCheck)
break;
userData = this.userInput(scanner);
if (this.isInteger(userData)) {
selectMenu = this.convertToInteger(userData);
if (this.isIntegerRange(selectMenu, 0, menu[recordIndex].length - 2)) {
message = null;
recordIndex += (selectMenu == 0) ? -1 : 1;
if (recordIndex == 0)
loopCheck = false;
if (recordIndex == 3) {
this.operator(title, menu[2], scanner);
recordIndex -= 2;
} else {
}
} else {
message = "[ 0~" + (menu[recordIndex].length - 2) + " 범위내 숫자를 입력해주세요]";
}
} else {
message = "[ 숫자로 입력해 주세요 ]\n";
}
}
scanner.close();
}
public void operator(String title, String[] menu, Scanner scanner) {
String num1;
String operatormenu;
String num2;
String operator;
int[] data = new int[4];
boolean run = true;
String printNum = "\t\t숫자를 입력해주세요.\n";
String printOpe = "\t\t연산자를 입력해주세요.\n";
while (true) {
this.display(title);
if (run) { // 새로 연산
this.display(this.subtitle("연산"));
this.display(printNum);
this.display(" __________________________________ select : ");
num1 = userInput(scanner);
} else {// 이어 연산
num1 = "" + data[3];
}
if (this.isInteger(num1)) {
data[0] = this.convertToInteger(num1);
this.display(title);
this.display(this.subtitle("연산"));
this.display(printOpe);
this.display("\t\t" + data[0] + "\n\t[선택: 1. 더하기. 2 빼기. 3. 곱하기. 4. 나누기]\n");
this.display(" __________________________________ select :");
operatormenu = userInput(scanner);
if (this.isInteger(operatormenu)) {
data[1] = this.convertToInteger(operatormenu);
operator = this.convertToString(data[1]);
this.display(title);
this.display(this.subtitle("연산"));
this.display(printNum);
this.display("\t\t" + data[0] + operator + "\n");
this.display(" __________________________________ select : ");
num2 = userInput(scanner);
if (this.isInteger(num2)) {
data[2] = this.convertToInteger(num2);
this.display(title);
this.display(this.subtitle("연산결과"));
Back back = new Back();
data[3] = back.controller(data[1], data[0], data[2]);
this.display("\t\t\t" + data[0] + operator + data[2] + "=" + data[3] + "\n\n");
this.display(this.makeSubMenu(menu));
String num3 = userInput(scanner);
if (num3.equals("1")) { // 새로 연산
run = true;
} else if (num3.equals("2")) {// 이어서 연산
run = false;
} else {// 0일때
break;
}
} else {
this.display("숫자를 입력해주세요");
}
} else {
this.display("숫자를 입력해주세요");
}
} else {
this.display("숫자를 입력해주세요");
}
}
}
public String convertToString(int value) {
String operator;
if (value == 1) {
operator = "+";
} else if (value == 2) {
operator = "-";
} else if (value == 3) {
operator = "*";
} else {
operator = "/";
}
return operator;
}
public String subtitle(String subtitle) {
StringBuffer subsubject = new StringBuffer();
subsubject.append(" [ ");
subsubject.append(subtitle);
subsubject.append(" ] _______________________________________ \n\n");
return subsubject.toString();
}
public String makeTitle() {
StringBuffer title = new StringBuffer();
title.append("****************************************************\n\n");
title.append(" JS Framework Calculator v1.0\n");
title.append(" Designed By HoonZzang\n\n");
title.append("****************************************************\n\n");
return title.toString();
}
public String makeMessage(String text) {
StringBuffer message = new StringBuffer();
message.append("[ ");
message.append(text);
message.append(" ]");
return message.toString();
}
public String makeSubMenu(String[] menu) {
StringBuffer subMenu = new StringBuffer();
subMenu.append(" [ " + menu[0] + " ]");
subMenu.append(" _____________________________________");
for (int underBar = 0; underBar <= 4 - menu[0].length(); underBar++) {
subMenu.append("_");
}
subMenu.append("\n\n");
if (menu.length <= 2) {
subMenu.append(" " + menu[menu.length - 1] + " \n");
subMenu.append(" ________________________________________________\n\n");
} else {
for (int colIdx = 1; colIdx < menu.length; colIdx++) {
if (colIdx == menu.length - 1) {
subMenu.append("0. " + menu[colIdx] + " \n");
} else {
subMenu.append(" " + colIdx + ". " + menu[colIdx] + " ");
}
}
subMenu.append(" __________________________________ select :");
}
return subMenu.toString();
}
/*
* 사용자 입력 전용 메서드 고려사항 : 1. 숫자로 변환이 불가능한 데이터가 입력 되어질 경우 해결방안 2. Scanner Class의
* Life Cycle과 Performance
*/
public String userInput(Scanner scanner) {
return scanner.next();
}
/* 정수 변환여부 체크 */
private boolean isInteger(String value) {
boolean isResult = true;
try {
Integer.parseInt(value);
} catch (Exception e) {
isResult = false;// e.printStackTrace();
}
return isResult;
}
/* 문자 >> 정수 변환 */
private int convertToInteger(String value) {
return Integer.parseInt(value);
}
/* 정수의 범의 체크 */
private boolean isIntegerRange(int value, int starting, int last) {
return (value >= starting && value <= last) ? true : false;
}
/* 출력 전용 메서드1 */
public void display(String text) {
System.out.print(text);
}
/* 출력 전용 메서드2 */
public void display(String[] text) {
for (int idx = 0; idx < text.length; idx++) {
System.out.print((idx + 1) + ". " + text[idx] + " ");
}
}
}
반응형