카테고리 없음

학생부 프로그램 복습

nsh95 2023. 12. 5. 00:44

사람

이름 name

나이 age

printInfo() : ㅇㅇ님 ㅇㅇ살

 

 

학생

성적 score

printInfo() : ㅇㅇ학생 ㅇㅇ살 ㅇㅇ점

test() : 기존점수이상 100점이하 점수 재설정

 

 

직원 Emp

부서 dep

printInfo() : ㅇㅇ팀 ㅇㅇ님 ㅇㅇ살

 

※ 프로그램

 

C 사람 추가

학생 추가

직원 추가

R 사람목록 출력

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

 

프로그램 종료

package example;

import java.util.Random;
import java.util.Scanner;

class Person {
	String name;
	int age;

	Person(String name, int age) {
		this.name = name;
		this.age = age;
		System.out.println("로그");
	}

	void printInfo() {
		System.out.println(this.name + "님 " + this.age + "살");
	}

}

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;
	}

	@Override
	void printInfo() {
		// TODO Auto-generated method stub
		System.out.println(this.name + "학생 " + this.age + "살 " + this.score + "점");
	}

	void test() {
		this.score = new Random().nextInt((101 - this.score) + this.score);
		// 어떤 범위의 끝값을 비교하는 것 == 경계값 검사 70~100
		System.out.println(this.name + "학생이 재시험을 봤습니다");
	}

}

class Emp extends Person {
	String dep;

	Emp(String name, int age) {
		this(name, age, "인턴");
	}

	Emp(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 Test02 {
	public static void main(String[] args) {
		Person[] datas= new Person[5];
		int index=0;
		
		Scanner sc = new Scanner(System.in);
		
		while(true) {
			System.out.println("1.사람추가");
			System.out.println("2.사람 목록 출력");
			System.out.println("3.전체 재시험");
			System.out.println("4.프로그램 종료");
			int action = sc.nextInt();
			
			if(action==1) {
				while(true) {
					System.out.println("1. 학생추가");
					System.out.println("2. 직원추가");
					System.out.println("입력 >>");
					action = sc.nextInt();
					if(1<=action && action<=2) {
						break;
					}
					System.out.println("다시 입력해주세요");
				}
				if(action==1) {	// 학생추가
					System.out.println("이름 입력 >>");
					String name = sc.next();
					System.out.println("나이>>");
					int age= sc.nextInt();
					// 성적 입력 >>
					Student stu = new Student(name,age);
					datas[index++]=stu;
					
				}else if(action==2) {	// 직원추가
					System.out.println("이름 입력 >>");
					String name = sc.next();
					System.out.println("나이>>");
					int age= sc.nextInt();
					
					// 부서입력>> 
					
					datas[index++] = new Emp(name,age);
				}
			}
			else if(action==2) {
				if(index<=0) {
					System.out.println("데이터 없음");
					continue;
				}
				for(int i=0; i<index; i++) {
					datas[i].printInfo();
				}
				
			}else if(action==3) {
				for(int i=0; i<index; i++) {
					if(datas[i] instanceof Student) {	// 너 학생이니?
						datas[i].printInfo();
						Student student=(Student)datas[i];	// 강제 형변환
						student.test();
					}
				}
			}else {
				break;
			}
		}
	}
}