✅ 답안과 비교하여 스스로 코드 개선점 짚어보기 완료(2022.01.03)
HARD1. 기본 생성자 활용 설정자(setter) 통한 필드값 변경과 접근자(getter) 이용한 필드값 출력 ✅
답안에서는 Application.class 맨 마지막에 scanner.close(); 구문을 넣어 스캐너를 종료시켰다.
입력한 임의의 값
package com.greedy.level03.hard.emp.model.dto;
public class EmployeeDTO {
private int number;
private String name;
private String dept;
private String job;
private int age;
private char gender;
private int salary;
private double bonusPoint;
private String phone;
private String address;
/* 기본 생성자 */
public EmployeeDTO() {}
/* 설정자(setter) 메소드 */
public void setNumber(int number) {
this.number = number;
}
public void setName(String name) {
this.name = name;
}
public void setDept(String dept) {
this.dept = dept;
}
public void setJob(String job) {
this.job = job;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(char gender) {
this.gender = gender;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void setBonusPoint(double bonusPoint) {
this.bonusPoint = bonusPoint;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setAddress(String address) {
this.address = address;
}
/* 접근자(getter) 메소드 */
public int getNumber() {
return number;
}
public String getName() {
return name;
}
public String getDept() {
return dept;
}
public String getJob() {
return job;
}
public int getAge() {
return age;
}
public char getGender() {
return gender;
}
public int getSalary() {
return salary;
}
public double getBonusPoint() {
return bonusPoint;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
}
package com.greedy.level03.hard.emp.run;
import java.util.Scanner;
import com.greedy.level03.hard.emp.model.dto.EmployeeDTO;
public class Application {
public static void main(String[] args) {
EmployeeDTO emp = new EmployeeDTO();
Scanner scanner = new Scanner(System.in);
System.out.print("사번을 입력하세요 : ");
int num = scanner.nextInt();
emp.setNumber(num);
System.out.print("이름을 입력하세요 : "); //개행
scanner.nextLine();
String name = scanner.nextLine();
emp.setName(name);
System.out.print("부서를 입력하세요 : ");
String dept = scanner.nextLine();
emp.setDept(dept);
System.out.print("직급을 입력하세요 : ");
String job = scanner.nextLine();
emp.setJob(job);
System.out.print("나이를 입력하세요 : ");
int age = scanner.nextInt();
emp.setAge(age);
System.out.print("성별을 입력하세요 : ");
char gender = scanner.next().charAt(0);
emp.setGender(gender);
System.out.print("급여를 입력하세요 : ");
int salary = scanner.nextInt();
emp.setSalary(salary);
System.out.print("보너스포인트를 입력하세요 : ");
double bonusPoint = scanner.nextDouble();
emp.setBonusPoint(bonusPoint);
System.out.print("휴대폰 번호를 입력하세요 : "); //개행
scanner.nextLine();
String phone = scanner.nextLine();
emp.setPhone(phone);
System.out.print("주소를 입력하세요 : ");
String address = scanner.nextLine();
emp.setAddress(address);
System.out.println(emp.getNumber());
System.out.println(emp.getName());
System.out.println(emp.getDept());
System.out.println(emp.getJob());
System.out.println(emp.getAge());
System.out.println(emp.getGender());
System.out.println(emp.getSalary());
System.out.println(emp.getBonusPoint());
System.out.println(emp.getPhone());
System.out.println(emp.getAddress());
scanner.close();
}
}
ADVANCED1. 삼각형-사각형 도형 정보 출력하기 ✅
========= 삼각형 계산기 =========
1. 삼각형 둘레 구하기
2. 삼각형 면적 구하기
3. 선택한 도형 정보 출력하기
4. 도형 색상 칠하기
9. 메인으로 돌아가기
메뉴를 선택하세요. > 3
선택하신 도형 타입 : 삼각형
높이 : 20.0
넓이 : 20.0
색상 : blue
ShapeDTO.class
package com.reminder.class_and_object_practice.shape.model.dto;
public class ShapeDTO {
private int type;
private double height;
private double width;
private String color;
public ShapeDTO(int type, double height, double width) { //String만 빠짐
this.type = type;
this.height = height;
this.width = width;
this.color = "white"; //color값 초기화
}
public void setType(int type) {
this.type = type;
}
public void setHeight(double height) {
this.height = height;
}
public void setWidth(double width) {
this.width = width;
}
public void setColor(String color) {
this.color = color;
}
public int getType() {
return type;
}
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
public String getColor() {
return color;
}
}
ShapeMenu.class
package com.reminder.class_and_object_practice.shape.views;
import java.util.Scanner;
import com.reminder.class_and_object_practice.shape.manager.SquareManager;
import com.reminder.class_and_object_practice.shape.manager.TriangleManager;
import com.reminder.class_and_object_practice.shape.model.dto.ShapeDTO;
public class ShapeMenu {
Scanner scanner = new Scanner(System.in);
public void mainMenu() {
double height=0;
double width=0;
int choice=0;
while(true) {
System.out.println("========== 도형 계산기 ==========");
System.out.println("3. 삼각형");
System.out.println("4. 사각형");
System.out.println("9. 프로그램 종료");
System.out.println("==============================");
System.out.print("계산하려는 도형을 선택하세요. > ");
choice = scanner.nextInt();
if(choice != 3 && choice != 4 && choice != 9) {
System.out.println("계산할 수 없는 도형입니다. 다시 입력하세요.");
continue;
} else if(choice == 9) {
System.out.println("프로그램을 종료합니다.");
return;
}
System.out.print("도형의 높이를 입력하세요. > ");
height = scanner.nextDouble();
System.out.print("도형의 너비를 입력하세요. > ");
width = scanner.nextDouble();
ShapeDTO shape = new ShapeDTO(choice, width, width); //type자리가 choice로 자동 변환됨
switch(shape.getType()) {
case 3: triangleMenu(shape); break;
case 4: squareMenu(shape); break;
}
}
}
private void triangleMenu(ShapeDTO shape) {
TriangleManager triangleManager = new TriangleManager();
while(true) { //반복문에 넣어야 변경한 color 값이 반영됨
System.out.println("========= 삼각형 계산기 =========");
System.out.println("1. 삼각형 둘레 구하기");
System.out.println("2. 삼각형 면적 구하기");
System.out.println("3. 선택한 도형 정보 출력하기");
System.out.println("4. 도형 색상 칠하기");
System.out.println("9. 메인으로 돌아가기");
System.out.print("메뉴를 선택하세요. > ");
int choice = scanner.nextInt();
switch(choice) {
case 1: triangleManager.calcPerimeter(shape); break;
case 2: triangleManager.calcArea(shape); break;
case 3: triangleManager.printShape(shape); break;
case 4: triangleManager.paintColor(shape, inputColor()); break; //color 대신 하단 inputColor() 메소드로 변경해야 컴파일 에러 사라짐
case 9: System.out.println("메인으로 돌아갑니다."); return; //mainMenu 호출 아닌 return;
default: System.out.println("잘못된 번호입니다. 메뉴를 다시 선택해 주세요."); break;
}
}
}
private void squareMenu(ShapeDTO shape) {
SquareManager squareManager = new SquareManager();
while(true) {
System.out.println("========= 사각형 계산기 =========");
System.out.println("1. 사각형 둘레 구하기");
System.out.println("2. 사각형 면적 구하기");
System.out.println("3. 선택한 도형 정보 출력하기");
System.out.println("4. 도형 색상 칠하기");
System.out.println("9. 메인으로 돌아가기");
System.out.print("메뉴를 선택하세요. > ");
int choice = scanner.nextInt();
switch(choice) {
case 1: squareManager.calcPerimeter(shape); break;
case 2: squareManager.calcArea(shape); break;
case 3: squareManager.printShape(shape); break;
case 4: squareManager.paintColor(shape, inputColor()); break; //color 대신 하단 inputColor() 메소드로 변경해야 컴파일 에러 사라짐
case 9: System.out.println("메인으로 돌아갑니다."); return; //mainMenu 호출 아닌 return;
default: System.out.println("잘못된 번호입니다. 메뉴를 다시 선택해 주세요."); break;
}
}
}
private String inputColor() {
System.out.print("어떤 색으로 도형을 칠할까요? > ");
scanner.nextLine(); //개행
String color = scanner.nextLine();
return color;
}
}
TriangleManager.class
package com.reminder.class_and_object_practice.shape.manager;
import com.reminder.class_and_object_practice.shape.model.dto.ShapeDTO;
public class TriangleManager {
public void calcPerimeter(ShapeDTO shape) {
double height = shape.getHeight();
double width = shape.getWidth();
double hypotenuse = Math.sqrt(height * height + width * width); //빗변
double perimeter = height + width + hypotenuse; //둘레
System.out.println("삼각형의 둘레는 " + perimeter + "입니다.");
}
public void calcArea(ShapeDTO shape) {
double height = shape.getHeight();
double width = shape.getWidth();
double area = height * width / 2; //면적
System.out.println("삼각형의 면적은 " + area + "입니다.");
}
public void printShape(ShapeDTO shape) {
System.out.println("선택하신 도형 타입 : 삼각형");
System.out.println("높이 : " + shape.getHeight());
System.out.println("넓이 : " + shape.getWidth());
System.out.println("색상 : " + shape.getColor());
}
public void paintColor(ShapeDTO shape, String color) {
shape.setColor(color);
System.out.println("선택하신 삼각형의 색상을 " + color + "로 반영합니다.");
}
}
SquareManager.class
package com.reminder.class_and_object_practice.shape.manager;
import com.reminder.class_and_object_practice.shape.model.dto.ShapeDTO;
public class SquareManager {
public void calcPerimeter(ShapeDTO shape) {
double height = shape.getHeight();
double width = shape.getWidth();
double perimeter = (height + width) * 2;
System.out.println("사각형의 둘레는 " + perimeter + "입니다.");
}
public void calcArea(ShapeDTO shape) {
double height = shape.getHeight();
double width = shape.getWidth();
double area = height * width;
}
public void printShape(ShapeDTO shape) {
System.out.println("선택하신 도형 타입 : 사각형");
System.out.println("높이 : " + shape.getHeight());
System.out.println("넓이 : " + shape.getWidth());
System.out.println("색상 : " + shape.getColor());
}
public void paintColor(ShapeDTO shape, String color) {
shape.setColor(color);
System.out.println("선택하신 사각형의 색상을 " + color + "로 반영합니다.");
}
}
Application.class
package com.reminder.class_and_object_practice.shape.run;
import com.reminder.class_and_object_practice.shape.views.ShapeMenu;
public class Application {
public static void main(String[] args) {
ShapeMenu shapeMenu = new ShapeMenu();
shapeMenu.mainMenu();
}
}
'Java' 카테고리의 다른 글
[자바의 정석] Ch 5. 배열 연습문제 풀이 (0) | 2022.01.01 |
---|---|
[JAVA/수업 Quiz] 클래스와 객체 | 오버로딩 | DTO (0) | 2021.12.31 |
[JAVA] 6-3. 오버로딩, 파라미터, static, final, 싱글톤 패턴 (0) | 2021.12.30 |
[JAVA/수업 과제 practice] 클래스와 객체 Lv. 1~2 (0) | 2021.12.29 |
[JAVA] 6-2. 객체 지향 언어, 캡슐화, 추상화, 생성자 (0) | 2021.12.29 |