posts - 45,comments - 0,views - 4815

 

 

 Student类:

复制代码
package com.exercise.bean;

import lombok.*;

import java.util.Arrays;

/**
 * @Author:Zxb
 * @Version:1.0
 * @Date:2022/11/22-9:50
 * @Since:jdk1.8
 * @Description:
 */
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor

public class Student {
    //学号,姓名,性别,班级,多门成绩信息
    private int stuId;
    private String name;
   private String stuSex;
   private String stuClass;
    private Score[] scores;

    @Override
    public String toString() {
        return "Student{" +
                "stuId=" + stuId +
                ", name='" + name + '\'' +
                ", stuSex='" + stuSex + '\'' +
                ", stuClass='" + stuClass + '\'' +
                '}';
    }
}
复制代码

Score类:

 

复制代码
package com.exercise.bean;

import lombok.*;

/**
 * @Author:Zxb
 * @Version:1.0
 * @Date:2022/11/22-9:57
 * @Since:jdk1.8
 * @Description:
 */
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Score {
    //成绩编号,科目,分数
    private int scoreId;
    private String subject;
    private int number;

}
复制代码

StudentAction类:显示菜单

复制代码
package com.exercise.action;

import com.exercise.bean.Score;
import com.exercise.bean.Student;
import com.exercise.service.StudentService;
import com.exercise.service.impl.StudentServiceImpl;

import java.util.Scanner;

/**
 * @Author:Zxb
 * @Version:1.0
 * @Date:2022/11/22-9:50
 * @Since:jdk1.8
 * @Description:
 */
public class StudentAction {
    //扫描仪
    private static Scanner scanner=new Scanner(System.in);
    //一组学生
    private static Student[] students =new Student[3];
    //业务对象
    private static StudentService studentService =new StudentServiceImpl();
      static{
          //第一个学生
          //成绩列表
          Score[] scores1=new Score[2];
          scores1[0] =new Score(1,"语文",80);
          scores1[1] =new Score(2,"数学",90);
          students[0]=new Student(101,"tom","男","java",scores1);

          //第二个学生
          //成绩列表
          Score[] scores2 = new Score[3];
          scores2[0] = new Score(3, "语文", 70);
          scores2[1] = new Score(4, "数学", 88);
          scores2[2] = new Score(5, "英语", 100);
          students[1] = new Student(102, "cat", "女", "JAVA1班", scores2);

          //第三个学生
          //成绩列表
          Score[] scores3 = new Score[3];
          scores3[0] = new Score(6, "语文", 79);
          scores3[1] = new Score(7, "数学", 89);
          scores3[2] = new Score(8, "英语", 100);
          students[2] = new Student(103, "lucy", "女", "JAVA1班", scores3);
      }

    /**
     * 启动菜单
     */
    public static void startMenu(){
        boolean b=true;
        while(b){
            System.out.println("********欢迎使用成绩管理系统*********");
            System.out.println("\t\t1、查找成绩信息");
            System.out.println("\t\t2、统计成绩信息");
            System.out.println("\t\t3、显示学生信息");
            System.out.println("\t\t0、退出系统");
            System.out.println("请选择:");
            int choose=scanner.nextInt();
            switch (choose){

                case 1:
                    studentService.findScore(students,scanner);
                    break;
                case 2:
                    studentService.countScore(students,scanner);
                    break;
                case 3:
                    studentService.showStudent(students);
                    break;
                case 0:
                    System.out.println("谢谢使用,系统已退出!");
                    b=false;
                    break;
            }
        }

    }
}
复制代码

StudentService:接口

复制代码
package com.exercise.service;

import com.exercise.bean.Student;

import java.util.Scanner;

/**
 * @Author:Zxb
 * @Version:1.0
 * @Date:2022/11/22-9:51
 * @Since:jdk1.8
 * @Description:
 */
public interface StudentService {
    /**
     * 查找成绩信息
     * @param students
     * @param scanner
     */
    void findScore(Student[] students, Scanner scanner);

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

    /**
     * 显示学生信息
     * @param students
     */
    void showStudent(Student[] students);
}
复制代码

StudentServiceImpl:实现

复制代码
package com.exercise.service.impl;

import com.exercise.bean.Score;
import com.exercise.bean.Student;
import com.exercise.service.StudentService;

import java.util.Scanner;

/**
 * @Author:Zxb
 * @Version:1.0
 * @Date:2022/11/22-10:08
 * @Since:jdk1.8
 * @Description:
 */
public class StudentServiceImpl implements StudentService {
    @Override
    public void findScore(Student[] students, Scanner scanner) {
        System.out.println("请输入要查询的学生学号:");
        int inputId = scanner.nextInt();
       //根据编号查找对应的成绩信息
        Score[] scores = getScoreById(students, inputId);
        //先判断
        if (scores == null) {
            System.out.println("查找失败,该学生没有参加考试!");
            return;
        }
        //成绩信息
        for (Score score : scores) {
            System.out.println(score);
        }
    }

    @Override
    public void countScore(Student[] students, Scanner scanner) {
        System.out.println("请输入要统计的学生编号:");
        int inputId = scanner.nextInt();
        //根据编号查找对应的成绩信息
        Score[] scores = getScoreById(students, inputId);
        //先判断
        if (scores == null) {
            System.out.println("统计失败,该学生没有参加考试");
            return;
        }
        //及格门数以及总成绩
        int count = 0, sum = 0;
        for (Score score : scores) {
            if (score.getNumber() >= 60) {
                count++;
            }
            //累加和
            sum += score.getNumber();
        }
        System.out.println("及格门数:" + count + "\t总成绩为:" + sum);
    }

    @Override
    public void showStudent(Student[] students) {
        for (Student student : students) {
            System.out.println(student);
            //学生对应的成绩
            Score[] scores = student.getScores();
            for (Score score : scores) {
                System.out.println(score);
            }
            System.out.println("-----------");
        }

    }

    /**
     * 根据学生编号查找成绩信息
     *
     * @param students
     * @param id
     * @return
     */
    private Score[] getScoreById(Student[] students, int id) {
        for (Student student : students) {
            if (id == student.getStuId()) {
                return student.getScores();
            }
        }
        return null;
    }
}
复制代码

StudentTest:

复制代码
package com.exercise.test;

import com.exercise.action.StudentAction;
import com.exercise.bean.Student;

/**
 * @Author:Zxb
 * @Version:1.0
 * @Date:2022/11/22-9:51
 * @Since:jdk1.8
 * @Description:
 */
public class StudentTest {
    /**
     * 启动
     * @param args
     */
    public static void main(String[] args) {
        StudentAction.startMenu();
    }
}
复制代码

 

posted on   小贤看世界  阅读(116)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示