结合数组和接口的知识实现学生成绩的管理

实体类:

学生与成绩,一个学生可以拥有多个成绩
学生实体类的属性:学号(id),姓名(name),班级(stuClass),成绩(scores),年龄(age)
成绩实体类的属性:编号(id),学科名称(cName),学科分数(number)

接口需求

拥有三个方法:
方法一可以根据学生的学号来查找对应学生的成绩
方法二可以根据学生的学号来统计学生所学科目的及格数量和所有学科总分
方法三可以显示学生和学生的成绩信息

设计接口

import com.bun.work1.entity.Student;

import java.util.Scanner;

/**
 * @Author: Bun
 * @Version: 1.0
 * @Date: 2022/10/5-10:22
 * @Since: jdk1.8
 * @Name:
 */
public interface StudentService {
    /**
     * 学生信息显示
     * @param students
     */
    void show(Student[] students);

    /**
     * 查找成绩信息
     * @param students
     * @param scanner
     */
    void find(Student[] students, Scanner scanner);

    /**
     * 统计学生成绩信息
     * @param students
     * @param scanner
     */
    void count(Student[] students, Scanner scanner);
}

实现类:重写接口中的所有抽象方法

package com.bun.work1.service.impl;

import com.bun.work1.entity.Score;
import com.bun.work1.entity.Student;
import com.bun.work1.service.StudentService;

import java.util.Scanner;

/**
 * @Author: Bun
 * @Version: 1.0
 * @Date: 2022/10/5-10:24
 * @Since: jdk1.8
 * @Name:
 */
public class StudentServiceImpl implements StudentService {

    @Override
    public void show(Student[] students) {
        System.out.println ( "显示学生信息" );
        for (Student student : students) {
            System.out.println ( student );
            //根据学生获取成绩信息
            Score[] scores = student.getScores ();
            for (Score score : scores) {
                System.out.println ( score );
            }
            System.out.println ( "----------------------" );
        }
    }

    @Override
    public void find(Student[] students, Scanner scanner) {
        System.out.println ( "搜索学生成绩信息" );
        System.out.println ( "请输入搜索的学生的学号:" );
        int inputId = scanner.nextInt ();

        Score[] scores = getScoresById ( students, inputId );
        if (scores == null) {
            System.out.println ( "[搜索失败,没有任何该学生的成绩信息!]" );
            return;
        }

        for (Score score : scores) {
            System.out.println ( score );
        }
    }

    private Score[] getScoresById(Student[] students, int inputId) {
        for (Student student : students) {
            if (inputId == student.getId ()) {
                return student.getScores ();
            }
        }
        return null;
    }

    @Override
    public void count(Student[] students, Scanner scanner) {
        System.out.println ( "统计学生成绩信息" );
        System.out.println ( "请输入统计的学生的学号:" );
        int inputId = scanner.nextInt ();

        Score[] scores = getScoresById ( students, inputId );
        if (scores == null) {
            System.out.println ( "[统计失败,没有任何该学生的成绩信息!]" );
            return;
        }

        //及格科目数量+总成绩
        int count = 0;
        float sum = 0;
        for (Score score : scores) {
            if (score.getNumber () >= 60) {
                count++;
            }
            sum += score.getNumber ();
        }
        System.out.println ( count + "  " + sum );
    }
}

测试类

package com.bun.work1.action;

import com.bun.work1.entity.Score;
import com.bun.work1.entity.Student;
import com.bun.work1.service.StudentService;
import com.bun.work1.service.impl.StudentServiceImpl;

import java.util.Scanner;

/**
 * @Author: Bun
 * @Version: 1.0
 * @Date: 2022/10/5-9:38
 * @Since: jdk1.8
 * @Name:
 */
public class StudentAction {
    //多个学生
    static Student[] students;
    //扫描仪
    static Scanner scanner = new Scanner ( System.in );

    static StudentService studentService = new StudentServiceImpl ();

    //静态初始化
    static {
        System.out.println ( "初始化---------------" );
        students = new Student[3];
        //学生一
        Score[] scores1 = new Score[2];
        scores1[0] = new Score ( "语文", 100 );
        scores1[1] = new Score ( "数学", 80 );
        students[0] = new Student ( "小明", 18, "一班", scores1 );
        //学生二
        Score[] scores2 = new Score[3];
        scores2[0] = new Score ( "语文", 80 );
        scores2[1] = new Score ( "数学", 70 );
        scores2[2] = new Score ( "外语", 40 );
        students[1] = new Student ( "小军", 19, "二班", scores2 );

        //学生三
        Score[] scores3 = new Score[4];
        scores3[0] = new Score ( "语文", 70 );
        scores3[1] = new Score ( "数学", 90 );
        scores3[2] = new Score ( "外语", 30 );
        scores3[3] = new Score ( "理综", 50 );
        students[2] = new Student ( "小强", 21, "三班", scores3 );
    }

    /**
     * 启动菜单
     */
    public static void startMenu() {
        boolean a = true;
        while (a) {
            System.out.println ( "-------------------------------" );
            System.out.println ( "1.搜索成绩信息" );
            System.out.println ( "2.统计成绩信息" );
            System.out.println ( "3.显示所有的学生信息" );
            System.out.println ( "请选择:" );
            int choose = scanner.nextInt ();

            switch (choose) {
                case 1:
                    a = false;
                    studentService.find ( students, scanner );
                    break;
                case 2:
                    a = false;
                    studentService.count ( students, scanner );
                    break;
                case 3:
                    a = false;
                    studentService.show ( students );
                    break;
            }
        }
    }
}

package com.bun.work1.test;

import com.bun.work1.action.StudentAction;

/**
 * @Author: Bun
 * @Version: 1.0
 * @Date: 2022/10/5-10:31
 * @Since: jdk1.8
 * @Name:
 */
public class Start {
    public static void main(String[] args) {
        StudentAction.startMenu ();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值