Adventure Time - Finn 3
본문 바로가기
JAVA/프로그램 설계 및 제작

연예인 정보 저장 코딩 모듈화,유효성

by hyun9_9 2023. 12. 5.
package test;

import java.time.LocalDate;
import java.util.Random;
import java.util.Scanner;

class Celebrity {

	String name;
	int birthYear;
	String agency;
	int hight;
	String type;

	
	Celebrity(String name, int birthYear, String agency, int hight, String type) {
		this.name = name;
		this.birthYear = birthYear;
		this.agency = agency;
		this.hight = hight;
		this.type = type;
	}

	void printInfo() {
		int now = LocalDate.now().getYear();
		System.out.println("이름 : " + this.name);
		System.out.println("나이 : " + (now - this.birthYear));
		System.out.println("소속사 : " + this.agency);
		System.out.println("키 : " + this.hight);
	}

	void simplePrint() {
		int now = LocalDate.now().getYear();
		System.out.print(name + " / " + (now - birthYear));
	}

//	void agencyChange() {
//		Scanner sc = new Scanner(System.in);
//		System.out.println(agency + "(을)를 변경하시겠습니까? ");
//		System.out.print("(예 / 아니오) >>>");
//		String choose = sc.next();
//		if (choose.equals("예")) {
//			System.out.println("변경할 소속사를 입력하세요 : ");
//			agency = sc.next();
//			System.out.println("변경이 완료 되었습니다");
//		} else if (choose.equals("아니오")) {
//			System.out.println("변경을 취소합니다");
//		}
//
//	}

}




class Entertainer extends Celebrity {
	int entertainmentAwardCount;

	Entertainer(String name, int birthYear, int hight) {
		this(name, birthYear, "무소속", hight);
	}

	Entertainer(String name, int birthYear, String agency, int hight) {
		super(name, birthYear, agency, hight, "예능인");
		this.entertainmentAwardCount = 0;
	}

	@Override
	void printInfo() {
		int now = LocalDate.now().getYear();
		System.out.println("직업 : " + this.type);
		System.out.println("이름 : " + this.name);
		System.out.println("나이 : " + (now - this.birthYear));
		System.out.println("소속사 : " + this.agency);
		System.out.println("키 : " + this.hight);
		if (entertainmentAwardCount > 0) {
			System.out.println("연예 대상 횟수 : " + this.entertainmentAwardCount);
		}
	}

}




class Actor extends Celebrity {
	int actingAwardCount;
	String famousLine;

	Actor(String name, int birthYear, int hight, String famousLine) {
		this(name, birthYear, "무소속", hight, famousLine);
	}

	Actor(String name, int birthYear, String agency, int hight, String famousLine) {
		super(name, birthYear, agency, hight, "배우");
		this.actingAwardCount = 0;
		this.famousLine=famousLine;
	}

	@Override
	void printInfo() {
		int now = LocalDate.now().getYear();
		System.out.println("직업 : " + this.type);
		System.out.println("이름 : " + this.name);
		System.out.println("나이 : " + (now - this.birthYear));
		System.out.println("소속사 : " + this.agency);
		System.out.println("키 : " + this.hight);
		if (actingAwardCount > 0) {
			System.out.println("연기 대상 횟수 : " + this.actingAwardCount);
		}
	}

	void famousLinePrint() {
		System.out.print(" / "+this.famousLine);
	}
}



class Singer extends Celebrity {
	int musicAwardsCount;

	Singer(String name, int birthYear, int hight) {
		this(name, birthYear, "무소속", hight);
	}

	Singer(String name, int birthYear, String agency, int hight) {
		super(name, birthYear, agency, hight, "가수");
		this.musicAwardsCount = 0;
	}

	@Override
	void printInfo() {
		int now = LocalDate.now().getYear();
		System.out.println("직업 : " + this.type);
		System.out.println("이름 : " + this.name);
		System.out.println("나이 : " + (now - this.birthYear));
		System.out.println("소속사 : " + this.agency);
		System.out.println("키 : " + this.hight);
		if (musicAwardsCount > 0) {
			System.out.println("가요 대상 횟수 : " + this.musicAwardsCount);
		}
	}
}

public class Test01 {
	static Scanner sc = new Scanner(System.in);
	public static int mainMenu() {
		System.out.println("1. 추가");// 1. 연예인 추가
		System.out.println("2. 조회");// 2. 조회
		System.out.println("3. 시상");// 3. 시상
		System.out.println("0. 종료");// 4. 종료
		
		int action = numCk(0,3);
		return action;
	}
	public static int typeMenu() {
		System.out.println("1. 가수");// 1. 가수
		System.out.println("2. 배우");// 2. 배우
		System.out.println("3. 예능인"); // 3. 예능인

		int action = numCk(1,3);
		return action;
	}
	public static String s_Input(String title) {
		System.out.print(title+"을(를) 입력하세요 : ");
		String input = sc.next();
		return input;
	}
	public static int i_Input(String title) {
		System.out.print(title+"을(를) 입력하세요 : ");
		int input = sc.nextInt();
		return input;
	}
	
//	public static String nameInput() {
//		System.out.print("이름을 입력하세요 : ");
//		String name = sc.next();
//		return name;
//	}
//	
//	public static int birthYearInput() {
//		System.out.print("출생년도를 입력하세요 : ");
//		int birthYear = sc.nextInt();
//		return birthYear;
//	}
//	public static String agencyInput() {
//		System.out.print("소속사를 입력해주세요 : ");
//		String agency = sc.next();
//		return agency;
//	}
//	public static int hightInput() {
//		System.out.print("키를 입력하세요 : ");
//		int hight = sc.nextInt();
//		return hight;
//	}
//	public static String famousLineInput() {
//		System.out.print("명대사를 입력하세요 : ");// 명대사를 입력하세요
//		String famousLine = sc.next();
//		return famousLine;
//	}
	public static int hasAgency() {
		System.out.println("소속사가 있습니까?(1. 예 / 2. 아니오)");// 소속사가 있습니까?
		int choose = numCk(1, 2);
		return choose;
	}
	public static void singerInput(Celebrity[] datas,int index) {
		String name=s_Input("이름");
		int birthYear=i_Input("출생년도");
		int hight=i_Input("키");
		int choose=hasAgency();
		if (choose==1) {// 1.예
			String agency=s_Input("소속사");
			datas[index] = new Singer(name, birthYear, agency, hight);
		} else if (choose==2) {// 2.아니오
			datas[index] = new Singer(name, birthYear,  hight);
		}
	}
	public static void actorInput(Celebrity[] datas,int index) {
		String name=s_Input("이름");
		int birthYear=i_Input("출생년도");
		int hight=i_Input("키");
		int choose=hasAgency();
		if (choose==1) {// 1.예
			String agency=s_Input("소속사");
			datas[index] = new Actor(name, birthYear, agency, hight,s_Input("명대사"));
		} else if (choose==2) {// 2.아니오
			datas[index] = new Actor(name, birthYear,  hight,s_Input("명대사"));
		}
	}
	public static void entertainerInput(Celebrity[] datas,int index) {
		String name=s_Input("이름");
		int birthYear=i_Input("출생년도");
		int hight=i_Input("키");
		int choose=hasAgency();
		if (choose==1) {// 1.예
			String agency=s_Input("소속사");
			datas[index] = new Entertainer(name, birthYear, agency, hight);
		} else if (choose==2) {// 2.아니오
			datas[index] = new Entertainer(name, birthYear,  hight);
		}
	}
	
	public static int searchMenu() {
		System.out.println("1. 모든 연예인 조회");// 1. 모든 연예인 조회
		System.out.println("2. 각 분야 별 연예인 조회");// 2. 각 분야 별 연예인 조회
		System.out.println("3. 이름으로 연예인 조회");// 3. 이름으로 연예인 조회
		System.out.println("4. 키 순서 조회");// 4. 키 순서 조회
		
		int action = numCk(1,4);
		return action;
	}
	public static void searchAll(Celebrity [] datas,int index) {
		for (int i = 0; i < index; i++) {// 배열의 모든 연예인 출력
			datas[i].simplePrint();
			if (datas[i] instanceof Actor) {
				((Actor) datas[i]).famousLinePrint();
			}
			System.out.println();
		}
	}
	public static void searchType(Celebrity [] datas,int index,int action) {
		for (int i = 0; i < index; i++) {
			if (action == 1 && datas[i] instanceof Singer) {// 1번 선택시
				datas[i].simplePrint();
				System.out.println();
				// 배열에서 가수만 찾아서 출력
			} else if (action == 2 && datas[i] instanceof Actor) {// 2번 선택시
				datas[i].simplePrint();
				if (datas[i] instanceof Actor) {
					((Actor) datas[i]).famousLinePrint();
				}
				System.out.println();
				// 배열에서 배우만 찾아서 출력
			} else if (action == 3 && datas[i] instanceof Entertainer) {// 3번 선택시
				datas[i].simplePrint();
				// 배열에서 예능인만 찾아서 출력
				System.out.println();
			}
			
		}
		
	}
	
	public static void searchName(Celebrity [] datas,int index) {
		String name =s_Input("이름");
		for (int i = 0; i < index; i++) {// 검색 받은 연예인의 이름을 찾아서 출력
			if (datas[i].name.equals(name)) {
				datas[i].printInfo();
			}
		}
	}
	public static void searchHight(Celebrity [] datas,int index) {

		
		String[] name=new String[index];
		int[] hight=new int[index];
		for(int i=0;i<index;i++){
			name[i]=datas[i].name;
			hight[i] =datas[i].hight;	
		}
		for(int j=0;j<hight.length;j++){
			for (int i = 0; i < hight.length-1-j; i++) {//배열을 1회 정렬할떄 사용되는 반복문
				String nameTemp="";
				int hightTemp=0;
				if(hight[i]<hight[i+1]) {
					hightTemp=hight[i];
					hight[i]=hight[i+1];
					hight[i+1]=hightTemp;
					
					nameTemp=name[i];
					name[i]=name[i+1];
					name[i+1]=nameTemp;
					
				}
			}//1번 정렬을 마친 상태 == 1회전 정렬이 끝났다.
		}
		for(int i=0;i<name.length;i++){
			System.out.println(name[i]+" / "+hight[i]);
		}

	}
	
	public static int awardsMenu() {
		
		System.out.println("1.가요대상");// 1.가요대상
		System.out.println("2.연기대상");// 2.연기대상
		System.out.println("3.연예대상");// 3.연예대상
		int action = numCk(1,3);
		return action;
	}
	public static void victoryPrint(Celebrity[]numberOfPeople,int winner,String type ) {
		System.out.println(type+" 대상은 !!!");
		System.out.println(numberOfPeople[winner].name+"입니다");
		System.out.println("축하합니다");
	}
	
	
	
	public static int numCk(int min, int max) {

		int input;
		while (true) {
			System.out.print(">>> ");
			input = sc.nextInt();
			if (min <= input && input <= max) {
				break;
			}
			else {
				System.out.println("입력이 잘못 되었습니다");
				System.out.println(min+"~"+max+" 사이의 숫자를 입력해 주세요");
			}
		}
		return input;

	}
	
	public static boolean numberOfPeople(int index) {
		if(index>0) {
			return true;
		}
		System.out.println("조회할 대상이 없습니다");
		return false;
	}
	
	public static int dummyDatas( Celebrity datas[],int index) {
		datas[index++]=new Singer("박현구", 1999, 178);
		datas[index++]=new Actor("박수연", 1999, 157, "캬");
		datas[index++]=new Entertainer("안승준", 1999, 177);
		datas[index++]=new Singer("김성민", 2000, 177);
		//datas[index++]=new Actor("정하연", 1999, 165, "청꼬미와 산책 가야해");
		return index;
	}
	
	
	public static void main(String[] args) {

		Celebrity[] datas = new Celebrity[5];
		int index = 0;
		// 연예인을 저장하고 시상식을 진행 할 수 있는 프로그램을 만들어주세요.
		
		index+=dummyDatas(datas,index);
		
		while (true) {
			// =====저장 프로그램=====
			int action=mainMenu();
			if (action == 0) {// 종료 선택시
				// 프로그램 종료
				System.out.println("프로그램 종료");
				break;
			} else if (action == 1) {// 추가 선택시
				if(index>=5) {
					System.out.println("더는 입력이 불가능 합니다.");
					continue;
				}
							
				action=typeMenu();//직업 메뉴
				if (action == 1) { // 가수 선택시
					singerInput(datas,index++);
					
				} else if (action == 2) {//배우 선택시
					actorInput(datas,index++);
					
				} else if (action == 3) { //예능인 선택시
					entertainerInput(datas,index++);
				}
			} else if (action == 2) {// 조회 선택시
				
				if(!numberOfPeople(index)) {
					continue;
				}
				
				action=searchMenu();//조회 메뉴
				if (action == 1) {// 모든 연예인 조회 선택시
					searchAll(datas, index);

				} else if (action == 2) {//각 분야별 연예인 조회 선택시
					
					action = typeMenu();
					
					searchType(datas, index,action);
					
					
				} else if (action == 3) {// 이름으로 연예인 조회 선택시
					
					searchName(datas, index);
					
				} else if (action == 4) {// 키 순서 조회 선택시
					searchHight(datas, action);
				}
			} else if (action == 3) {// 시상 선택시
				if(!numberOfPeople(index)) {
					continue;
				}
				Random rand=new Random();
				Celebrity[] numberOfPeople = new Celebrity[5];// 인원 수와 동일한 길이의 배열 생성
				int cnt=0;
				action=awardsMenu();//시상 종류 메뉴 출력
				
				//비슷한 것이 3개 인데 모듈화가 가능한지?
				if (action == 1) {// 1번 선택시
					for(int i=0;i<index;i++) {// 배열 내의 가수 인원수 파악
						if(datas[i] instanceof Singer) {
							numberOfPeople[cnt++]=datas[i];// 배열에 해당 객체 저장
						}
					}
					int winner=rand.nextInt(cnt);
					((Singer)numberOfPeople[winner]).musicAwardsCount++;
					victoryPrint(numberOfPeople, winner, "가요");
					
				} else if (action == 2) {// 2번 선택시
					for(int i=0;i<index;i++) {// 배열 내의 배우 인원수 파악
						if(datas[i] instanceof Actor) {
							numberOfPeople[cnt++]=datas[i];// 배열에 해당 객체 저장
						}
					}
					int winner=rand.nextInt(cnt);
					((Actor)numberOfPeople[winner]).actingAwardCount++;
					victoryPrint(numberOfPeople, winner, "연기");
				} else if (action == 3) {// 3번 선택시
					for(int i=0;i<index;i++) {// 배열 내의 예능인 인원수 파악
						if(datas[i] instanceof Entertainer) {
							numberOfPeople[cnt++]=datas[i];// 배열에 해당 객체 저장
						}
					}
					int winner=rand.nextInt(cnt);
					((Entertainer)numberOfPeople[winner]).entertainmentAwardCount++;
					victoryPrint(numberOfPeople, winner, "연예");
				}

				// 시상의 로직
				// 원하는 대상의 배열을 따로 생성하여 배열의 길이 만큼의 랜덤 숫자를 뽑아 뽑힌 번호의 인덱스의 객체가 시상자.
			}

		}
	}

}