实体类:
学生与成绩,一个学生可以拥有多个成绩
学生实体类的属性:学号(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 ();
}
}