Java/Java 연습 문제

[Java 연습 문제] 학생 관리 시스템 *

Joo.v7 2024. 9. 1. 22:16

student pakage

Student.java

더보기
package student;

public abstract class Student {
    protected int studentNo;
    protected String name;
    protected String cellphone;

    public Student(int studentNo, String name, String cellphone) {
        this.studentNo = studentNo;
        this.name = name;
        this.cellphone = cellphone;
    }

    public int getStudentNo() {
        return this.studentNo;
    }

    public String getName() {
        return this.name;
    }

    public String getCellphone() {
        return this.cellphone;
    }

    @Override
    public String toString() {
        return this.studentNo + ", " + this.name + ", " + this.cellphone;
    }
}

 

CollegeBoy.java

더보기
package student;

public class CollegeBoy extends Student {
    private String doubleMajor;

    public CollegeBoy(int studentNo, String name, String cellphone) {
        super(studentNo, name, cellphone);
    }

    public CollegeBoy(int studentNo, String name, String cellphone, String doubleMajor) {
        super(studentNo, name, cellphone);
        this.doubleMajor = doubleMajor;
    }

    public String getDoubleMajor() {
        return this.doubleMajor;
    }

    @Override
    public String toString() {
        if (this.doubleMajor != null) {
            return this.studentNo + ", " + this.name + ", " + this.cellphone + ", " + this.doubleMajor;
        }
        else {
            return this.studentNo + ", " + this.name + ", " + this.cellphone;
        }
    }
}

 

Graduate.java

더보기
package student;

public class Graduate extends Student {
    private String major;

    public Graduate(int studentNo, String name, String cellphone, String major) {
        super(studentNo, name, cellphone);
        this.major = major;
    }

    public String getMajor() {
        return this.major;
    }

    @Override
    public String toString() {
        return this.studentNo + ", " + this.name + ", " + this.cellphone + ", " + this.major;
    }
}

 

StudentNameComparator.java

더보기
package student;

import java.util.Comparator;

public class StudentNameComparator implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return s1.getName().compareTo(s2.getName());
    }
}

 

StudentNoComparator.java

더보기
package student;

import java.util.Comparator;

public class StudentNoComparator implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return s1.getStudentNo() - s2.getStudentNo();
    }
}

 

department pakage

 

Department.java

더보기
package department;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import java.util.Comparator;
import student.Student;

public class Department<E extends Student> implements Iterable<E> {
    public int departmentNo;
    public String departmentName;
    public List<E> list;

    public Department(int departmentNo, String departmentName) {
        this.departmentNo = departmentNo;
        this.departmentName = departmentName;
        this.list = new ArrayList<>();
    }

    public int getDepartmentNo() {
        return this.departmentNo;
    }

    public String getDepartmentName() {
        return this.departmentName;
    }

    public void addStudent(E e) {
        this.list.add(e);
    }

    public Iterator<E> iterator() {
        return this.list.iterator();
    }

    public void sort() {
        Collections.sort(this.list, new student.StudentNoComparator());
    }

    public void sort(Comparator<Student> comparator) {
        Collections.sort(this.list, comparator);
    }
}

 

Test.java

더보기
import department.Department;
import student.CollegeBoy;
import student.StudentNameComparator;

public class Test {
    public static void main(String[] args) {
        Department<CollegeBoy> department = new Department<>(1, "Computer Engineering");
        department.addStudent(new CollegeBoy(3, "Celine", "010-1111-2222"));
        department.addStudent(new CollegeBoy(1, "James", "010-2222-4444"));
        department.addStudent(new CollegeBoy(6, "Adriana", "010-3333-4444"));
        department.addStudent(new CollegeBoy(4, "Robert", "010-4444-4444"));
        department.addStudent(new CollegeBoy(7, "William", "010-5555-4444"));

        for(CollegeBoy c: department) {
            System.out.println(c);
        }

        System.out.println();
        department.sort();

        for(CollegeBoy c: department) {
            System.out.println(c);
        }

        System.out.println();
        department.sort(new StudentNameComparator());

        for(CollegeBoy c: department) {
            System.out.println(c);
        }
    }
}

 

실행 결과

더보기
3, Celine, 010-1111-2222
1, James, 010-2222-4444
6, Adriana, 010-3333-4444
4, Robert, 010-4444-4444
7, William, 010-5555-4444

1, James, 010-2222-4444
3, Celine, 010-1111-2222
4, Robert, 010-4444-4444
6, Adriana, 010-3333-4444
7, William, 010-5555-4444

6, Adriana, 010-3333-4444
3, Celine, 010-1111-2222
1, James, 010-2222-4444
4, Robert, 010-4444-4444
7, William, 010-5555-4444

6, Adriana, 010-3333-4444
3, Celine, 010-1111-2222
1, James, 010-2222-4444
4, Robert, 010-4444-4444
7, William, 010-5555-4444

 

2024.08.31 - [Java] - [Java] 11. Generics

 

[Java] 11. Generics

Chapter 1: Generics 개요Generics 개요강력한 타입 검사타입 변환 감소알고리즘 일반화 Chapter 2: Generic 타입Generic 타입 선언Generic 타입 생성자Generic 객체 생성Generic 메소드 Chapter 3: Generic 메소드 선언타

lightningtech.tistory.com

 

출처: https://github.com/gikpreet/class-programming_with_java/tree/master

 

GitHub - gikpreet/class-programming_with_java

Contribute to gikpreet/class-programming_with_java development by creating an account on GitHub.

github.com