老师,有什么方法转换成班级类吗?不通过searchByName这个方法,直接转换的有吗?
import java.util.*; public class Stutent_TestDemo { public void Student_test(){ // Student st1=new Student(001,"张三",88.0f,99.7f); // Student st2=new Student(002,"李四",86.0f,90.7f); // Student st3=new Student(003,"王五",60.0f,78.7f); // List<Student> st=new ArrayList<Student>(); // Collections.addAll(st,st1,st2,st3); // for (Student stone:st){ // System.out.println(stone); // } } public void Banji_test(){ Student st1=new Student(001,"张三",88.0f,99.7f); Student st2=new Student(002,"李四",86.0f,90.7f); Student st3=new Student(002,"王五",60.0f,78.7f); BanJi bj=new BanJi("701班级","高一年纪"); bj.addStudent(st1); bj.addStudent(st2); //bj.addStudent(st3); bj.displayAllStudent(); System.out.println("++++++"); Student student=bj.searchStudentByNum(006); if(student==null){ System.out.println("学号不存在"); }else { System.out.println(student); } System.out.println("++++++"); bj.insertChineseScore(002,99.5f); // bj.insertMathScore(002,77f); bj.deleteStudent(001); bj.displayAllStudent(); } public void schooi_test(){ Student st1=new Student(001,"张三",100.0f,60.7f); Student st2=new Student(002,"李四",330.0f,100.7f); Student st3=new Student(003,"王五",200.0f,200.7f); BanJi bj1=new BanJi("701班级","高一年纪"); BanJi bj2=new BanJi("601班级","高二年纪"); bj1.addStudent(st1); bj1.addStudent(st2); bj2.addStudent(st3); School sh=new School(); sh.addBanji(bj1); sh.addBanji(bj2); //BanJi s=sh.searchByName("高一年纪"); //System.out.println(s); // System.out.println("============="); // sh.sortChineseByAverage(); // System.out.println("============="); // sh.sortMathByAverage(); sh.displayBanJiName(); } public void mainMenu() { System.out.println("**********************************"); System.out.println(" **主菜单**"); System.out.println(" 1--班级管理"); System.out.println(" 2--学校管理"); System.out.println(" 0--退出"); System.out.println("**********************************"); System.out.println("请输入操作选项:"); } public void banJiMenu() { System.out.println("******************************************************"); 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(" 6--删除学生信息"); System.out.println(" 7--显示所有学生信息"); System.out.println(" 9--返回上一级菜单"); System.out.println("******************************************************"); System.out.println("请输入操作选项:"); } public void schoolMenu() { System.out.println("******************************************************"); 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(" 6--显示所有班级名称"); System.out.println(" 9--返回上一级菜单"); System.out.println("******************************************************"); System.out.println("请输入操作选项:"); } public void TestDeme(){ BanJi zbj=new BanJi("S001","主班级"); School shl=new School(); Stutent_TestDemo st=new Stutent_TestDemo(); Scanner sc=new Scanner(System.in); int input1,input2,input3; while (true){ st.mainMenu(); input1=sc.nextInt(); if(input1==0){ System.out.println("成功退出"); break; } switch (input1){ case 1: while (true){ st.banJiMenu(); System.out.println("请输入对应的数字对播放列表进行管理:"); input2=sc.nextInt(); if(input2==9){ System.out.println("成功返回上一级菜单"); break; } switch (input2){ case 1: int input4; System.out.println("请输入需要添加学生的人数"); input4=sc.nextInt(); for (int i=0;i<input4;i++){ System.out.println("请输入学号"); int a1=sc.nextInt(); System.out.println("请输入学生姓名"); String a2=sc.next(); Float f01,f02; Student sj=new Student(a1,a2,f01=0.0f,f02=0.0f); zbj.addStudent(sj); } //zbj.addStudent(); continue; case 2: System.out.println("请输入需要添加的班级名称"); String bjname=sc.next(); BanJi bj1=shl.searchByName(bjname); if(bj1 == null){ System.out.println("请先创建班级,"+bjname+"该班级不存在"); continue; }else { System.out.println("请输入需要添加的数量"); int b1=sc.nextInt(); for (int i=0;i<b1;i++){ System.out.println("请先输入学号"); int b2=sc.nextInt(); Student zbj2=zbj.searchStudentByNum(b2); if(zbj2 ==null){ System.out.println("请输入学生姓名"); String b3=sc.next(); float b4,b5; Student sd=new Student(b2,b3,b4=0.0f,b5=0.0f); bj1.addStudent(sd); zbj.addStudent(sd); }else { bj1.addStudent(zbj2); } } //BanJi s=bj1; zbj.displayAllStudent(); } shl.searchByName(bjname); continue; case 3: System.out.println("请输入学号"); int stuNumt=sc.nextInt(); Student stuNumone=zbj.searchStudentByNum(stuNumt); if(stuNumone ==null){ System.out.println("学号不存在"); break; }else { System.out.println(stuNumone); } continue; case 4: System.out.println("请输入需要添加语文成绩学生的学号"); int s1=sc.nextInt(); Student s2=zbj.searchStudentByNum(s1); if(s2==null){ System.out.println("学号不存在"); break; }else { System.out.println("请输入需要语文分数"); float f1=sc.nextFloat(); zbj.insertChineseScore(s1,f1); } continue; case 5: System.out.println("请输入需要添加数学成绩学生的学号"); int s3=sc.nextInt(); Student s4=zbj.searchStudentByNum(s3); if(s4 == null){ System.out.println("输入的学号不存在"); break; }else { System.out.println("请输入需要添加的数学成绩"); float sf1=sc.nextFloat(); zbj.insertMathScore(s3,sf1); } continue; case 6: System.out.println("输入需要删除学生的学号"); int s5=sc.nextInt(); Student sd1=zbj.searchStudentByNum(s5); if(sd1==null){ System.out.println("你输入的删除的学号不存在"); break; }else { zbj.deleteStudent(s5); } continue; case 7: zbj.displayAllStudent(); continue; } } break; case 2: while (true){ st.schoolMenu(); System.out.println("请输入对应的操作"); input3=sc.nextInt(); if (input3==9){ System.out.println("成功返回上一页"); break; } switch (input3){ case 1: System.out.println("请输入班级id"); String a1=sc.next(); System.out.println("请输入班级名称"); String a2=sc.next(); BanJi a3=new BanJi(a1,a2); shl.addBanji(a3); break; case 2: System.out.println("请输入需要删除的班级名称"); String st1=sc.next(); BanJi bj1=shl.searchByName(st1); if(bj1==null){ System.out.println("输入的班级不存在"); break; }else if(bj1.equals("主班级")){ System.out.println("主班级无法删除"); break; }else { shl.deleteBanji(st1); } break; } } break; default: System.out.println("输入不正确,请重新输入"); break; } } } public static void main(String[] args) { Stutent_TestDemo st=new Stutent_TestDemo(); //st.Student_test(); //st.Banji_test(); //st.schooi_test(); st.TestDeme(); } } import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BanJi implements Comparable{ private String classId; private String className; private List<Student> studentslist; public BanJi(String classId,String className){ this.setClassId(classId); this.setClassName(className); studentslist=new ArrayList<Student>(); } /** * 将学生添加到班级 * @param stu */ public void addStudent(Student stu){ boolean fals=false; for(Student st:studentslist){ if(st.getStuNum()==stu.getStuNum()){ fals=true; break; } } if(fals){ System.out.println(stu.getStuName()+"学号重复"); }else { studentslist.add(stu); } } /** * * @param stuNum * @return 通过学号查询学生在班级中是否存在 */ public Student searchStudentByNum(int stuNum){ Student stuNum1=null; for (Student st:studentslist){ if(st.getStuNum()==stuNum){ stuNum1=st; break; } } return stuNum1; } /** * 输入班级学生的语文成绩 * @param stuNum * @param score */ public void insertChineseScore(int stuNum,float score){ Student st=searchStudentByNum(stuNum); for (Student s:studentslist){ if(s.getStuNum()==stuNum){ s.setChinese(score); System.out.println(s); break; } } } /** * 输入班级学生的数学成绩: * @param stuNum * @param score */ public void insertMathScore(int stuNum,float score){ for(Student st:studentslist){ if(st.getStuNum()==stuNum){ st.setMath(score); System.out.println(st); break; } } } /** * 删除学生信息 * @param stuNum */ public void deleteStudent(int stuNum){ Student st=searchStudentByNum(stuNum); studentslist.remove(st); System.out.println("学号"+stuNum+"删除成功"); } /** * 显示所有学生的信息(包括学号和姓名) */ public void displayAllStudent(){ for (Student list:studentslist){ System.out.println(list); } } //获取语文成绩平均分 public float yuwen(){ float ft=0.0f; for(Student stu:studentslist){ ft=ft+stu.getChinese(); break; } return ft/studentslist.size(); } //数学成绩平均分 public float shuxue(){ float ft=0.0f; for (Student stu:studentslist){ ft=ft+stu.getMath(); } return ft/studentslist.size(); } public String getClassId() { return classId; } public void setClassId(String classId) { this.classId = classId; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public List<Student> getStudentslist() { return studentslist; } public void setStudentslist(List<Student> studentslist) { this.studentslist = studentslist; } @Override public String toString() { return "班级{" + "班级Id='" + classId + '\'' + ", 班级名称='" + className + '\'' + ", 班级列表=" + studentslist + '}'; } @Override public int compareTo(Object o) { return 0; } } import java.util.*; import java.util.stream.Stream; public class School{ Map<String,BanJi>schoolMap; public School(){ schoolMap=new HashMap<String,BanJi>(); } /** * 添加班级 * @param banji */ public void addBanji(BanJi banji){ schoolMap.put(banji.getClassName(),banji); } /** * 删除班级 * @param banji */ public void deleteBanji(BanJi banji){ schoolMap.remove(banji.getClassName()); System.out.println("已经成功删除班级"+banji); } /** * 通过班级名称查询班级 * @param className * @return */ public BanJi searchByName(String className) { BanJi bj=null; Set<String> setbanji=schoolMap.keySet(); for(String se:setbanji){ if(se.equals(className)){ bj=schoolMap.get(se); break; } } return bj; } /** * 对各班语文成绩按平均分进行由大到小排序: */ public void sortChineseByAverage() { List<BanJi> listbj=new ArrayList<>(); Iterator<BanJi> itbj=schoolMap.values().iterator(); while (itbj.hasNext()){ listbj.add(itbj.next()); } System.out.println("未排序前"+listbj); Collections.sort(listbj,new YuWen()); for (BanJi li:listbj){ System.out.println("排序后"+li); System.out.println("该班语文平均分为:"+li.yuwen()); } } public void sortMathByAverage() { List<BanJi> listbj=new ArrayList<>(); Iterator<BanJi> itbj=schoolMap.values().iterator(); while (itbj.hasNext()){ listbj.add(itbj.next()); } Collections.sort(listbj,new ShuXue()); for (BanJi li:listbj){ System.out.println("排序后"+li); System.out.println("数学平均成绩"+li.shuxue()); } } /** * 显示所有班级名称 */ public void displayBanJiName() { Set<String> setlist=schoolMap.keySet(); for (String se:setlist){ System.out.println(se); } } public Map<String, BanJi> getSchoolMap() { return schoolMap; } public void setSchoolMap(Map<String, BanJi> schoolMap) { this.schoolMap = schoolMap; } } import java.util.Objects; public class Student { private int stuNum; private String stuName; private float math; private float chinese; public Student(){} /** * * @param stuNum 学号 * @param stuName 学生姓名 * @param math 数学成绩 * @param chinese 语文成绩 */ public Student(int stuNum,String stuName,float math,float chinese){ this.setStuNum(stuNum); this.setStuName(stuName); this.setMath(math); this.setChinese(chinese); } public int getStuNum() { return stuNum; } public void setStuNum(int stuNum) { this.stuNum = stuNum; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public float getMath() { return math; } public void setMath(float math) { this.math = math; } public float getChinese() { return chinese; } public void setChinese(float chinese) { this.chinese = chinese; } @Override public String toString() { return "学生信息{" + "学号" + stuNum + ",姓名='" + stuName + '\'' + ", 数学成绩=" + math + ", 语文成绩=" + chinese + '}'; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Student student = (Student) obj; return stuNum == student.stuNum && Float.compare(student.math, math) == 0 && Float.compare(student.chinese, chinese) == 0 && stuName.equals(student.stuName); } @Override public int hashCode() { return Objects.hash(stuNum, stuName, math, chinese); } } import java.util.Comparator; public class YuWen implements Comparator<BanJi> { @Override public int compare(BanJi o1, BanJi o2) { int f=0; float b1=o1.yuwen(); float b2=o2.yuwen(); f=(int) (b2-b1); return f; } } import java.util.Comparator; public class ShuXue implements Comparator<BanJi> { @Override public int compare(BanJi o1, BanJi o2) { int i=0; float b1=o1.shuxue(); float b2= o2.shuxue(); i=(int) (b2-b1); return i; } }
15
收起
正在回答
1回答
同学你好,很抱歉老师没能很好的理解同学的问题,同学是想将什么转换成班级类呢?建议再具体描述下哦。
祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星