com.mywork.controll
StudentManager.java
package com.mywork.control;
import java.util.Scanner;
import com.mywork.data.Student;
public class StudentManager {
Student[] arr;
int idx;
int studentNum;
Scanner scanner = new Scanner(System.in);
public StudentManager(int numOfStudents){
arr = new Student[numOfStudents];
for(int i = 0; i < arr.length; i++) {
arr[i] = new Student();
}
}
public Student input() {
System.out.print("학생 성명 >> ");
String name = scanner.nextLine();
System.out.print("학생 학과 >> ");
String dept = scanner.nextLine();
System.out.print("중간 점수 >> ");
String score1 = scanner.nextLine();
System.out.print("기말 점수 >> ");
String score2 = scanner.nextLine();
return new Student(name, dept, score1, score2);
}
public void addNewStudent(Student student) {
arr[idx++] = student;
}
public void outputAllStudents() {
for(int i = 0; i < idx; i++) {
System.out.println((i + 1) + "번째 학생 정보 ----------");
arr[i].output();
}
}
public void outputAverage() {
double total = 0;
for(int i = 0; i < idx; i++) {
total += arr[i].getAverage();
}
System.out.println("학생" + idx + "명의 총 평균은 " + (total / idx) + "입니다.");
}
public Student findStudent(){
System.out.print("찾을 학생 이름 입력 >> ");
String name = scanner.nextLine();
for(int i = 0; i < idx; i++) {
if(name.equals(arr[i].name)) {
return arr[i];
}
}
return null;
}
public void output(Student student) {
if(student != null) {
student.output();
}else{
System.out.println("찾는 학생이 존재하지 않습니다.");
}
}
}
com.mywork.data
Student.java
package com.mywork.data;
public class Student {
public String name;
public String dept;
public String score1;
public String score2;
public double average;
public boolean isPass;
public Student(){}
public Student(String name, String dept, String score1, String score2){
this.name = name;
this.dept = dept;
this.score1 = score1;
this.score2 = score2;
this.average = getAverage();
this.isPass = getPass();
}
public double getAverage() {
double a = Double.parseDouble(score1);
double b = Double.parseDouble(score2);
return (a + b) / 2;
}
public boolean getPass() {
return getAverage() >= 80 ? true : false;
}
public void output() {
System.out.println("이름 : " + name);
System.out.println("학과 : " + dept);
System.out.println("평균 : " + average);
System.out.println("합격 유무 : " + (isPass ? "합격" : "불합격"));
}
}
com.mywork.run
StudentMain.java
package com.mywork.run;
import com.mywork.control.StudentManager;
public class StudentMain {
public static void main(String[] args) {
StudentManager manager = new StudentManager(3);
manager.addNewStudent(manager.input());
manager.addNewStudent(manager.input());
manager.addNewStudent(manager.input());
manager.outputAllStudents();
manager.outputAverage();
manager.output(manager.findStudent());
}
}