부모 클래스인 Person 클래스를 통해 Student 클래스와 Employee 클래스를 생성하고 메뉴를 통해 Student 또는 Employee의 정보를 저장하고 출력하는 프로그램을 만들어보자.
<aside> 📢 코멘트 올려주신 실습문제 확인했습니다. 우선 컨트롤러 쪽 피드백 드리면 , 문제에서 없는 3가지 필드를 사용 하셨더라구요. 물론 , 돌아가기만 하면 문제는 없지만 문제에서 정확히 요구하는 답은 아니었습니다 . 자세한 답은 실습문제 풀이시간에 알려드릴게요~~. 메뉴 쪽은 프로그램의 흐름 잘 이해하시고 작성해주신 것으로 보이네요. 문제푸시느라수고하셨습니다!
</aside>
package com.kh.hw.person.model.vo;
public class Person {
private String name;
private int age;
private double height;
private double weight;
public Person() {
}
public Person(String name, int age, double height, double weight) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setHeight(double height) {
this.height = height;
}
public double getHeight() {
return this.height;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getWeight() {
return this.weight;
}
@Override
public String toString() {
return name+", "+age+", "+height+", "+weight;
// return "Person [name="+name+", age="+age+", height="+height+", weight="+weight+"]";
}
}
package com.kh.hw.person.model.vo;
public class Student extends Person{
private int grade;
private String major;
public Student() {
}
public Student(String name, int age, double height, double weight, int grade, String major) {
super(name, age, height, weight);
this.grade = grade;
this.major = major;
}
public void setGrade(int grade) {
this.grade = grade;
}
public int getGrad() {
return this.grade;
}
public void setMajor(String major) {
this.major = major;
}
public String getMajor() {
return this.major;
}
@Override
public String toString() {
return super.toString()+", "+grade+", "+major;
// return "Student ["+super.toString()+", grade="+grade+", major="+major+"]";
}
}
package com.kh.hw.person.model.vo;
public class Employee extends Person{
private int salary;
private String dept;
public Employee() {
}
public Employee(String name, int age, double height, double weight, int salary, String dept) {
super(name, age, height, weight);
this.salary = salary;
this.dept = dept;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getSalary() {
return this.salary;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getDept() {
return this.dept;
}
@Override
public String toString() {
return super.toString();
// return "Employee ["+super.toString()+", salary="+salary+", dept"+dept+"]";
}
}
package com.kh.hw.person.controller;
import java.util.Arrays;
import com.kh.hw.person.model.vo.Employee;
import com.kh.hw.person.model.vo.Student;
public class PersonController {
private Student [] s = new Student[3];
private Employee [] e = new Employee[10];
int count1 = 0;
int count2 = 0;
// 질문
public int[] PersonCounter() {
int [] pCounter = new int [2];
pCounter[0] = count1;
pCounter[1] = count2;
return pCounter;
}
private int sCount = 0;
public void insertStudent(String name, int age, double height, double weight, int grade, String major) {
Student stu = new Student();
stu.setName(name);
stu.setAge(age);
stu.setHeight(height);
stu.setWeight(weight);
stu.setGrade(grade);
stu.setMajor(major);
s[sCount] = stu;
sCount++;
count1++;
}
public Student[] printStudent() {
Student [] s2 = new Student[s.length];
int S = 0;
for(int i=0; i<s.length; i++) {
s2[S] = s[i];
S++;
}
Student [] copy = Arrays.copyOf(s2, S);
return copy;
}
private int eCount = 0;
public void insertEmployee(String name, int age, double height, double weight, int salary, String dept) {
Employee emp = new Employee();
emp.setName(name);
emp.setAge(age);
emp.setHeight(height);
emp.setWeight(weight);
emp.setSalary(salary);
emp.setDept(dept);
e[eCount] = emp;
eCount++;
count2++;
}
public Employee[] printEmployee() {
Employee [] e2 = new Employee[e.length];
int E = 0;
for(int i=0; i<s.length; i++) {
e2[E] = e[i];
E++;
}
Employee [] copy = Arrays.copyOf(e2, E);
return copy;
}
}