728x90
반응형

class

public class Person {
	
	private String name;
	private int age;
	private char gender;
	
	//기본생성자
	public Person() { }
	
	//파라미터 생성자
	public Person(String name, int age, char gender) {
		this.name = name;
		this.age = age;
		this.gender = gender;
		
	}
	
	//setter
	public void setName(String name) {this.name = name;}
	public void setAge(int age) {this.age = age;}
	public void setGender(char gender) {this.gender = gender;}
	
	//getter
	public String getName() {return this.name;}
	public int getAge() {return this.age;}
	public char getGender() {return this.gender;}
	
	public String getPersonInfo() {
		return String.format("[name=%s,  age=%d, gender=%c]",name, age, gender);
	}

}

 

control class

public class PersonManager {
	
	private Person[] personArr;
	private int personIndex; //private int personIndex = 0;이랑 똑같음(기본값 세팅)
	
	private Scanner sc = new Scanner(System.in);
	final int MAX_PERSON = 5;  //몇명이 되든 숫자만 바꾸면되기에 유지보수 편리
	
	public PersonManager() {
		personArr = new Person[MAX_PERSON];
	}
	
	public void initManage() {
		insertData();
		printData();	
	}
	
	public void insertData() {
		
		for(int i=0; i<MAX_PERSON; i++) {
			System.out.printf("-----------%d-----------%n",i+1);
			System.out.print("이름 입력 : ");
			String name = sc.next();
			System.out.print("나이 입력 : ");
			int age = sc.nextInt();
			System.out.print("성별(m/f) 입력 : ");
			char gender = sc.next().toLowerCase().charAt(0);
			Person p =new Person(name, age, gender);
			
			//배열에 대입
			personArr[personIndex++] = new Person(name, age, gender);
			
			System.out.println("더 입력하시겠습니까?(y/n) : ");
			char yn = sc.next().toLowerCase().charAt(0);
			if(yn=='n')
				break;
		}
		System.out.println("=========입력완료=========");
	}
	public void printData() {
		System.out.println("현재 등록된 인원은 ["+personIndex+"]명 입니다.");
		for(int i=0; i<personIndex; i++) {
				System.out.println(i+" : "+personArr[i].getPersonInfo());
		}
	}

 

main class

public class PersonMain {

	public static void main(String[] args) {
		PersonManager pm = new PersonManager();
		pm.initManage();
	}
}    

 

 

 

 

반응형
복사했습니다!