JAVA/JAVA_복습문제

JAVA_상속_#3_Person

onivv 2023. 12. 3. 21:40

문제

▶ 사람 클래스 형성 (상위 클래스)
     < 멤버변수 >
      - 이름
      - 나이
      < 메소드 >
       - printInfo() : ㅇㅇ님 ㅇㅇ살


      ▶ 학생 (하위 클래스)
            - 성적
            - printInfo() : ㅇㅇ님 ㅇㅇ살 ㅇㅇ점
            - test() : 기존 점수이상 100점 이하 점수 재설정
      ▶ 직원 (하위 클래스)
            - 부서
            - printInfo() : ㅇㅇ팀 ㅇㅇ님 ㅇㅇ살

========================================

[ 프로그램 ] - 사람을 최대 5명 저장할 수 있는 공간

C     사람 추가 ( 학생 / 직원)

R     전체 목록 출력

U     학생 목력 출력 후 전체 재시험

프로그램 종료

========================================

 

코드 ver1.

package inheritance;

import java.util.Scanner;

class Person1 {
	String name;
	int age;
	//생성자 : 사람에겐 이름과 나이가 무조건 있다!
	Person1(String name, int age){
		this.name = name;
		this.age = age;
	}
	void printInfo() {
		System.out.println(this.name + "님 " + this.age + "살");
	}
}

class Student1 extends Person1 {
	int score;
	//부모 클래스는 기본 생성자 없는 상태
	//이름과 나이를 무조건 받는 생성자가 있어야함
	Student1(String name, int age){
		this(name, age, 0);
	}
	Student1(String name, int age, int score){
		super(name, age);
		this.score = score;
	}
	@Override
	void printInfo() {
		System.out.println(this.name + "님 " + this.age + "살 " + this.score + "점");
	}
	//[1] - 사용자 점수 입력
	void test() {
		Scanner sc = new Scanner(System.in);
		System.out.println("점수를 입력하세요.");
		while(true) {
			int score = sc.nextInt();
			if(score >= this.score && score <= 100) {
				this.score = score;
				break;
			}
			System.out.println("재시험 후 점수를 다시 입력하세요.");
		}
	}
	//[2] - 랜덤 점수
//	class Student extends Person {
//	   int score;
//	   Student(String name,int age){
//	      this(name,age,new Random().nextInt(101));
//	   }
//	   Student(String name,int age,int score){
//	      super(name,age);
//	      this.score=score;
//	   }
//	   void test() {
//	      this.score=new Random().nextInt(101-this.score)+this.score;
//	      System.out.println("재시험 점수 : " + this.score);
//	   }
//	}
}

class Emp1 extends Person1 {
	String dep;
	Emp1(String name, int age, String dep){
		super(name, age);
		this.dep = dep;
	}
	@Override
	void printInfo() {
		System.out.println(this.dep + "팀 " + this.name + "님 " + this.age + "살");
	}
}

public class freefree {
	public static void main(String[] args) {
		Student1 s1 = new Student1("홍길동", 27);
		Emp1 e1 = new Emp1("홍길동", 36, "개발");
		s1.test();
		s1.printInfo();
		e1.printInfo();
	}
}

 

코드 ver2.

package inheritance;

import java.util.Scanner;

class Person1 {
	String name;
	int age;
	// 생성자 : 사람에겐 이름과 나이가 무조건 있다!
	Person1(String name, int age) {
		this.name = name;
		this.age = age;
	}
	void printInfo() {
		System.out.println(this.name + "님 " + this.age + "살");
	}
}
class Student1 extends Person1 {
	int score;
	// 부모 클래스는 기본 생성자 없는 상태
	// 이름과 나이를 무조건 받는 생성자가 있어야함
	Student1(String name, int age) {
		this(name, age, 0);
	}
	Student1(String name, int age, int score) {
		super(name, age);
		this.score = score;
	}
	@Override
	void printInfo() {
		System.out.println(this.name + "님 " + this.age + "살 " + this.score + "점");
	}
	// [1] - 사용자 점수 입력
	void test() {
		Scanner sc = new Scanner(System.in);
		System.out.println("점수를 입력하세요.");
		while (true) {
			int score = sc.nextInt();
			if (score >= this.score && score <= 100) {
				this.score = score;
				break;
			}
			System.out.println("재시험 후 점수를 다시 입력하세요.");
		}
	}
}
class Emp1 extends Person1 {
	String dep;
	Emp1(String name, int age) {
		this(name, age, "인턴");
	}
	Emp1(String name, int age, String dep) {
		super(name, age);
		this.dep = dep;
	}
	@Override
	void printInfo() {
		System.out.println(this.dep + "팀 " + this.name + "님 " + this.age + "살");
	}
}
public class freefree {
	public static void main(String[] args) {
		Person1[] datas = new Person1[5];
		int index = 0;
		Scanner sc = new Scanner(System.in);

		while (true) {
			System.out.println(" =====목록출력===== ");
			System.out.println("1. 사람 추가");
			System.out.println("2. 사람 목록 출력");
			System.out.println("3. 전체 재시험");
			System.out.println("4. 프로그램 종료");
			System.out.println(" ============== ");
			System.out.println("입력 >>> ");
			int action = sc.nextInt();
			if (action == 1) {
				if (index >= 5) {
					System.out.println("목록이 가득찼습니다.");
					continue; // 아래 소스코드 무시하고 while문 처음으로 돌아가라
				}
				while (true) {
					System.out.println("1. 학생 추가");
					System.out.println("2. 직원 추가");
					System.out.println("입력 >>> ");
					action = sc.nextInt();
					if (action >= 1 && action <= 2) {
						break;
					}
					System.out.println("다시 입력해주세요!");
				}
				if (action == 1) { // 학생 추가
					System.out.println("이름 입력 >> ");
					String name = sc.next();
					System.out.println("나이 입력 >> ");
					int age = sc.nextInt();
					System.out.println("성적 입력 >> ");
					int score = sc.nextInt();
					datas[index++] = new Student1(name, age, score);
				} else if (action == 2) { // 직원 추가
					System.out.println("이름 입력 >> ");
					String name = sc.next();
					System.out.println("나이 입력 >> ");
					int age = sc.nextInt();
					System.out.println("부서 입력 >> ");
					String dep = sc.next();
					datas[index++] = new Emp1(name, age, dep);
				}
			}
			else if (action == 2) {
				// 목록에 사람이 없을 경우
				if (index <= 0) {
					System.out.println("데이터 없음!");
					continue; // 프로그램 종료가 되면 안되니 continue
				}
				// i<datas.length로 하게되면
				// datas에 모든 공간에 객체가 저장돼있지 않으면 printInfo를 찾을 수 없어 error
				// 따라서 객체가 형성된 방(index)만큼만 돌리기
				for (int i = 0; i < index; i++) {
					datas[i].printInfo();
				}
			} else if (action == 3) {
				for (int i = 0; i < index; i++) {
					// datas[i] 객체가 Student에 포함되니?
					if (datas[i] instanceof Student1) {
						datas[i].printInfo();
						// 시험은 학생만 칠 수 있음
						// 강제 형변환, 캐스팅
						// 예시...
						// double a = 3;
						// int b = (int)a;
						Student1 student = (Student1) datas[i];
						student.test();
					}
				}
			} else {
				break;
			}
		}
	}
}