JAVA课程学习九:类训练-学生管理实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/iceyung/article/details/78439919

练习知识1:掌握类的创建,成员变量与方法的编写

练习知识2:掌握类的实例化与成员、方法的调用

练习知识3:掌握面向对象思想,理解类的封装与关联关系

设计要求:
建立一个学生类(姓名,学号,3门课成绩(英语,数学,语文),总分),类的方法:输入,输出,学生数据,根据总分排序,打印学生名次)

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你要录入的学生人数:");
        int studentNum = in.nextInt();

        System.out.println("===========================================================");

        List<Student> list = new ArrayList<Student>();

        for(int i=0;i<studentNum;i++) {
            System.out.println("输入学生的学号:");
            String studentID = in.next();

            System.out.println("输入学生的姓名:");
            String studentName = in.next();

            System.out.println("输入数学科目的成绩:");
            int math = in.nextInt();

            System.out.println("输入英语科目的成绩:");
            int English = in.nextInt();

            System.out.println("输入语文科目的成绩:");
            int chinese = in.nextInt();

            Course course = new Course(math,English,chinese);
            Student student = new Student(studentID,studentName,course);
            list.add(student);
            System.out.println("===========================================================");
        }

        //排序
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getCourse().getSum()>o2.getCourse().getSum())
                    return -1;
                else
                    return 1;
            }

        });

        //遍历输出
        for(Student student : list) {
            System.out.println("学号:"+student.getStudentID()+"\t"+"姓名:"+student.getStudentName()+"\t"
                    + "总分:"+student.getCourse().getSum());
        }

    }

}

/**
 * @描述 学生类
 * @author iceyu
 *
 */
class Student{
    private String studentID;
    private String studentName;
    private Course course;

    public Student() {}
    public Student(String studentID,String studentName,Course course) {
        this.course = course;
        this.studentID = studentID;
        this.studentName = studentName;
    }

    public String getStudentID() {
        return studentID;
    }
    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public Course getCourse() {
        return course;
    }
    public void setCourse(Course course) {
        this.course = course;
    }

}

/**
 * @描述 课程类
 * @author iceyu
 *
 */
class Course{
    private int math;
    private int English;
    private int chinese;
    private int sum;
    public Course() {}
    public Course(int math,int English,int chinese) {
        this.math = math;
        this.chinese = chinese;
        this.English = English;
    }

    public int getSum() {
        return this.chinese + this.English + this.math;
    }
    public void setSum(int sum) {
        this.sum = sum;
    }
    public int getMath() {
        return math;
    }
    public void setMath(int math) {
        this.math = math;
    }
    public int getEnglish() {
        return English;
    }
    public void setEnglish(int english) {
        English = english;
    }
    public int getChinese() {
        return chinese;
    }
    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

}

最终结果:

请输入你要录入的学生人数:
3
===========================================================
输入学生的学号:
001
输入学生的姓名:
小明
输入数学科目的成绩:
67
输入英语科目的成绩:
78
输入语文科目的成绩:
55
===========================================================
输入学生的学号:
002
输入学生的姓名:
小红
输入数学科目的成绩:
89
输入英语科目的成绩:
99
输入语文科目的成绩:
90
===========================================================
输入学生的学号:
003
输入学生的姓名:
小白
输入数学科目的成绩:
78
输入英语科目的成绩:
67
输入语文科目的成绩:
55
===========================================================
最终排名:
学号:002  姓名:小红   总分:278
学号:001  姓名:小明   总分:200
学号:003  姓名:小白   总分:200

覆盖tostring方法输出:

package 学生管理;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你要录入的学生人数:");
        int studentNum = in.nextInt();

        System.out.println("===========================================================");

        List<Student> list = new ArrayList<Student>();

        for(int i=0;i<studentNum;i++) {
            System.out.println("输入学生的学号:");
            String studentID = in.next();

            System.out.println("输入学生的姓名:");
            String studentName = in.next();

            System.out.println("输入数学科目的成绩:");
            int math = in.nextInt();

            System.out.println("输入英语科目的成绩:");
            int English = in.nextInt();

            System.out.println("输入语文科目的成绩:");
            int chinese = in.nextInt();

            Course course = new Course(math,English,chinese);
            Student student = new Student(studentID,studentName,course);
            list.add(student);
            System.out.println("===========================================================");
        }

        //排序
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getCourse().getSum()>o2.getCourse().getSum())
                    return -1;
                else
                    return 1;
            }

        });

        System.out.println("最终排名:");
        //遍历输出
        for(Student student : list) {
//          System.out.println("学号:"+student.getStudentID()+"\t"+"姓名:"+student.getStudentName()+"\t"
//                  + "总分:"+student.getCourse().getSum());
            System.out.println(student.toString());
        }

    }

}

/**
 * @描述 学生类
 * @author iceyu
 *
 */
class Student{
    private String studentID;
    private String studentName;
    private Course course;

    public Student() {}
    public Student(String studentID,String studentName,Course course) {
        this.course = course;
        this.studentID = studentID;
        this.studentName = studentName;
    }

    public String getStudentID() {
        return studentID;
    }
    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public Course getCourse() {
        return course;
    }
    public void setCourse(Course course) {
        this.course = course;
    }

    //重写类的ToString方法
    @Override
    public String toString() {
        String result = "学号:"+this.getStudentID()+"\t"+"姓名:"+this.getStudentName()+"\t"
                + "总分:"+this.getCourse().getSum();
        return result;
    }


}

/**
 * @描述 课程类
 * @author iceyu
 *
 */
class Course{
    private int math;
    private int English;
    private int chinese;
    private int sum;
    public Course() {}
    public Course(int math,int English,int chinese) {
        this.math = math;
        this.chinese = chinese;
        this.English = English;
    }

    public int getSum() {
        return this.chinese + this.English + this.math;
    }
    public void setSum(int sum) {
        this.sum = sum;
    }
    public int getMath() {
        return math;
    }
    public void setMath(int math) {
        this.math = math;
    }
    public int getEnglish() {
        return English;
    }
    public void setEnglish(int english) {
        English = english;
    }
    public int getChinese() {
        return chinese;
    }
    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

}

猜你喜欢

转载自blog.csdn.net/iceyung/article/details/78439919