프로그램 문제

학생 이름, 학과, 학번, 학점평균 입력하세요. >>

낙산암 2021. 11. 9. 21:07

import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

class Student {

private String name;
private String department;
private String number;
private double grade;

public String getName() {
    return name;
}



public void setName(String name) {
    this.name = name;
}



public String getDepartment() {
    return department;
}



public void setDepartment(String department) {
    this.department = department;
}



public String getNumber() {
    return number;
}



public void setNumber(String number) {
    this.number = number;
}



public double getGrade() {
    return grade;
}



public void setGrade(double grade) {
    this.grade = grade;
}


public Student(String name, String department, String number, double grade) {
    this.name = name;
    this.department = department;
    this.number = number;
    this.grade = grade;
}



public void findStudent(String name) {
    if(name.equals(this.name)) {
        System.out.println(this.name+", "+this.department+", "+this.number+", "+this.grade);
    }
}

}

public class StudentTest{
public static void main(String[] args) {
final int STU_NUM = 4;
ArrayList arrList = new ArrayList();

    Scanner sc = new Scanner(System.in);
    System.out.println("학생 이름, 학과, 학번, 학점평균 입력하세요.");

    for(int i=0; i < STU_NUM; i++) {

        System.out.print(">> ");
        String text = sc.nextLine();

        StringTokenizer st = new StringTokenizer(text, ",");
        String name = st.nextToken().trim();
        String department = st.nextToken().trim();
        String number = st.nextToken().trim();
        double grade = Double.valueOf(st.nextToken().trim());

        arrList.add(new Student(name, department, number, grade));

    }

    for(int i=0; i<arrList.size(); i++) {
        System.out.println("----------------------------------");
        Student s = arrList.get(i);
        System.out.println("이름: "+s.getName());
        System.out.println("학과: "+s.getDepartment());
        System.out.println("학번: "+s.getNumber());
        System.out.println("학점평균: "+s.getGrade());
    }

    System.out.println("----------------------------------");
    while(true) {
        System.out.print("학생 이름 >> ");
        String name = sc.next();

        if(name.equals("그만"))
            break;

        for(int i=0; i< arrList.size(); i++) {
            Student s = arrList.get(i);
            s.findStudent(name);
        }
    }

    System.out.println("그만");

}

}