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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| import org.testng.annotations.Test;
import java.util.*;
public class StuGradeMethod {
Scanner scan = new Scanner(System.in);
public void GradeManageUI() { System.out.println("-----------学生成绩管理-----------"); System.out.println("| 1.列出所有学生成绩 |"); System.out.println("| 2.添加成绩 |"); System.out.println("| 3.修改成绩 |"); System.out.println("| 4.整体成绩信息 |"); System.out.println("| 5.返回上级 |"); System.out.println("---------------------------------"); System.out.print("请输入你的选择:"); }
public boolean choose(boolean OK) { String choose = scan.nextLine(); switch (choose) { case "1": showAllGrade(); break; case "2": addGrade(); break; case "3": updateGrade(); break; case "4": gradeInfo(); break; case "5": return false; default: System.out.println("没有该选项!"); } return OK; }
@Test public void showAllGrade() { String sql = "select stu.stu_id id,stu_name name, chinese,math,english,science,total_grade totalGrade from student stu join stu_grade sg on stu.stu_id = sg.stu_id"; List<Student> list = DBUtils.selectInfo(Student.class, sql); if (list == null) { System.out.println("无成绩信息"); return; } System.out.println("|学号\t|名字\t\t|语文\t|数学\t|英语\t|科学\t|总分\t|"); Iterator<Student> iterator = list.iterator(); while (iterator.hasNext()) { iterator.next().outputGraInfo(); } }
@Test public void addGrade() { System.out.print("请输入需要添加成绩的学生学号:"); String id = scan.nextLine(); if (DBUtils.haveRecord(id)) { System.out.print("请输入语文成绩:"); String chinese = scan.nextLine(); System.out.print("请输入数学成绩:"); String math = scan.nextLine(); System.out.print("请输入英语成绩:"); String english = scan.nextLine(); System.out.print("请输入科学成绩:"); String science = scan.nextLine();
String sql = "insert into stu_grade(stu_id, chinese, math, english, science) values(?,?,?,?,?)";
if (DBUtils.insertOrUpdate(sql, id, chinese, math, english, science) > 0) { System.out.println("录入成功"); } else { System.out.println("录入失败,请检查格式或者成绩已存在"); } } else { System.out.println("该学生不存在"); } }
@Test public void updateGrade() { System.out.print("请输入需要修改成绩的学生学号:"); String id = scan.nextLine(); if (DBUtils.haveRecord(id)) { System.out.print("请输入语文成绩:"); String chinese = scan.nextLine(); System.out.print("请输入数学成绩:"); String math = scan.nextLine(); System.out.print("请输入英语成绩:"); String english = scan.nextLine(); System.out.print("请输入科学成绩:"); String science = scan.nextLine();
String sql = "update stu_grade set chinese = ?,math = ?,english = ?,science = ? where stu_id = ?";
if (DBUtils.insertOrUpdate(sql, chinese, math, english, science, id) > 0) { System.out.println("修改成功"); } else { System.out.println("修改失败"); } } }
@Test public void gradeInfo() { String sql = "select avg(chinese) chinese,avg(math) math,avg(english) english,avg(science) science,avg(total_grade) totalGrade from stu_grade"; float[] gradeAvg = new float[5];
DBUtils.getAvgGrade(sql, gradeAvg);
String gradeInfoSelect = "select count(*) from stu_grade where ? <= ";
String[] subjects = {"chinese", "math", "english", "science"}; List<GradeInfo> infos = new ArrayList();
for (String subject : subjects) { GradeInfo gradeInfo = new GradeInfo(subject); if (subject.equals("science")) { gradeInfo.setExcellent(DBUtils.selectInfo(gradeInfoSelect, subject, 80)); gradeInfo.setPass(DBUtils.selectInfo(gradeInfoSelect, subject, 60)); gradeInfo.setNoPass(DBUtils.selectInfo(gradeInfoSelect, subject, 0) - gradeInfo.getPass()); } else { gradeInfo.setExcellent(DBUtils.selectInfo(gradeInfoSelect, subject, 120)); gradeInfo.setPass(DBUtils.selectInfo(gradeInfoSelect, subject, 90)); gradeInfo.setNoPass(DBUtils.selectInfo(gradeInfoSelect, subject, 0) - gradeInfo.getPass()); } infos.add(gradeInfo); }
int person = infos.get(0).getPass() + infos.get(0).getNoPass();
System.out.println("各科人数:"); System.out.println("总人数:" + person); System.out.println("|科目\t|优秀\t|及格\t|不及格\t|"); for (GradeInfo gradeinfo : infos) { gradeinfo.outputGradeInfo(); } System.out.println("\n"); System.out.println("各科平均分:"); System.out.print("|语文\t|数学\t|英语\t|科学\t|总分\t|\n|"); for (float i : gradeAvg) { System.out.print(i+"\t|"); } System.out.println();
} }
|