- 추상클래스의 특징
1) 추상메서드를 한 개 이상 포함하고 있다.
2) 추상메서드는 구현이 되지않은 메서드이다.
3) 추상클래스는 객체를 만들수 없다.
4) 추상클래스를 상속받은 자식클래스가 추상메서드를 구현하지않으면 자식 클래스도 추상 클래스가 된다.
- 추상클래스의 목적
1) 추상 메서드에서는 필드와 메서드를 정의 할 수 없다. (상속만 가능) 자식클래스로 하여금 구현을 강요하도록 한다.
2) 부모인 추상클래스 변수를 통해 매개 변수로 전달하여 메서드의 양을 줄이는 효과가 있다.
3) 부모 추상클래스 변수의 배열에 자식 클래스들의 객체들을 담아서 각각 다른 클래스 객체를 통일성있게 관리할 수 있다.
Main class
public class Main {
public static void main(String[] args) {
View view=new View();
view.menuLoop();
}
}
Model
상속을 전제로한 추상클래스.
사원의 종류에 따라 급여가 다르게 구현되도록 추상클래스와 추상메서드를 사용하였다.
public abstract class Employee {
protected String emNum;
protected String name;
protected double pay;
public Employee(String emNum, String name, double pay) {
this.emNum=emNum;
this.name=name;
this.pay=pay;
}
public void showEmployeeInfo() {
System.out.println("사번: " + emNum);
System.out.println("이름: " + name);
System.out.println("급여: " + pay);
}
//
public abstract double getMonthPay();
public void showMonthPayInfo() {
System.out.println("급여: "+pay);
}
}
public class FullEmployee extends Employee {
protected double bonus;
public FullEmployee(String emNum, String name, double pay, double bonus) {
super(emNum, name, pay);
this.bonus=bonus;
}
public void showEmployeeInfo(){
super.showEmployeeInfo();
System.out.println("보너스:"+ bonus);
}
public double getMonthPay() {
return (pay/12+bonus/12);
}
}
public class ContracEmployee extends Employee {
private int hiredYear;
public ContracEmployee(String emNum, String name, double pay, int hiredYear) {
super(emNum,name,pay);
this.hiredYear=hiredYear;
}
public void showEmployeeInfo(){
super.showEmployeeInfo();
System.out.println("입사년도:"+ hiredYear);
}
public double getMonthPay() {
return pay/12;
}
}
public class TempEmployee extends Employee {
protected double workDay;
public TempEmployee(String emNum, String name, double pay, double workDay) {
super(emNum,name,pay);
this.workDay=workDay;
}
public void showEmployeeInfo() {
super.showEmployeeInfo();
System.out.println("출근일: "+workDay);
}
public double getMonthPay() {
return pay/12/20*workDay;
}
}
View
class MENU {
final static int FULL = 1;
final static int CONTRACT = 2;
final static int TEMP = 3;
final static int SHOW_ALLDATA = 4;
final static int GET_MONTHPAY = 5;
final static int EXIT = 6;
//메뉴라는 클래스를 설정해서 상수에 이름 을 붙혀서 가독성이 더좋아졌다,
}
public class View {
static Scanner sc = new Scanner(System.in);
Controller controller;
public View() {
int num = getEmployeeNum();
controller = new Controller(num);
}
public static Scanner getScanner() {
return sc;
}
int getEmployeeNum() {
System.out.println("몇명의 사원의 정보를 입력하시겠습니까?");
int num = sc.nextInt();
return num;
}
void viewMenu() {
System.out.println("----메뉴 선택----");
System.out.println("1. 정규직 저장");
System.out.println("2. 계약직 저장");
System.out.println("3. 임시직 저장");
System.out.println("4. 전체 정보 출력");
System.out.println("5. 월급 계산 출력");
System.out.println("6. 프로그램 종료");
}
int getUserSel() {
System.out.println("선택>>");
int selNum = sc.nextInt();
return selNum;
}
boolean runSel(int selNum) {//숫자를 쓰는 대신에 단어로 바꿔서 더읽기쉽게 가독성을 높혔습니다.
switch (selNum) {
case MENU.FULL:
System.out.println("--정사원정보저장--");
controller.addEmp(selNum);
break;
case MENU.CONTRACT:
System.out.println("--계약사원정보저장--");
controller.addEmp(selNum);
break;
case MENU.TEMP:
System.out.println("--임시사원정보저장--");
controller.addEmp(selNum);
break;
case MENU.SHOW_ALLDATA:
System.out.println("--모든직원정보보기--");
controller.showEmployeeInfo();
break;
case MENU.GET_MONTHPAY:
System.out.println("--급여정보보기--");
controller.showMonthPay();
break;
case MENU.EXIT:
System.out.println("--프로그램종료--");
return false;
default:
System.out.println("오류");
break;
}
return true;
}
void menuLoop() {
boolean isRun = true;
while (isRun) {
viewMenu();
int selNum = getUserSel();
isRun = runSel(selNum);
}
}
}
Controller
화면에서 사용자의 선택에 대한 처리 기능들의 역할
- 처리를 담당하는 클래스
- (비지니스 로직을 담당)
- Controller
- Service
- Manager
- Handler
public class Controller {
private Employee[] myEmp;
private int numOfEmp;
private final static int EMP_NUM = 200;
public Controller() {
myEmp = new Employee[EMP_NUM];
numOfEmp = 0;
}
public Controller(int num) {
myEmp = new Employee[num];
numOfEmp = 0;
}
void addEmpInfo(Employee emp) {
if (numOfEmp < myEmp.length)
myEmp[numOfEmp++] = emp;
else
System.out.println("저장공간이 부족합니다.");
}
void addEmp(int selNum) {
String emNum, name;
int pay, bonus, hiredYear, workDay;
Scanner sc = View.getScanner();
System.out.println("사번: ");
emNum = sc.next();
System.out.println("이름: ");
name = sc.next();
System.out.println("연봉: ");
pay = sc.nextInt();
if (selNum == MENU.FULL) {
System.out.println("보너스:");
bonus = sc.nextInt();
addEmpInfo(new FullEmployee(emNum, name, pay, bonus));
} else if (selNum == MENU.CONTRACT) {
System.out.println("입사년도: ");
hiredYear = sc.nextInt();
addEmpInfo(new ContracEmployee(emNum, name, pay, hiredYear));
} else if (selNum==MENU.TEMP) {
System.out.println("출근일수: ");
workDay=sc.nextInt();
addEmpInfo(new TempEmployee(emNum, name, pay,workDay));
}
}
void showEmployeeInfo() {
for (int i = 0; i < numOfEmp; i++) {
myEmp[i].showEmployeeInfo();
System.out.println();
}
}
void showMonthPay() {
for (int i = 0; i < numOfEmp; i++) {
System.out.println((i+1)+"번 째 사원의 월급은 "+myEmp[i].getMonthPay()+"만원 입니다.");
}
}
}
'java' 카테고리의 다른 글
Java/ Math & Random (0) | 2020.02.28 |
---|---|
Java/ Interface의 개념 (0) | 2020.02.10 |
Java/ MVC패턴과 추상클래스를 이용한 연락처 관리 프로그램 (0) | 2020.02.10 |
Java/ equals와 toString의 사용 (0) | 2020.02.10 |
Java/ 상속 (0) | 2020.02.10 |