要求:
实现以下功能:
①添加一个班级;
②往班级中添加学生;
③查询班级,学生;
④删除班级,学生;
分析:
对象:
①班级:班级编号,班级名称,学生数组;
②学生:学号,姓名,性别,年龄;
在Myeclipse中完成:新建4个类
①Student
package StudentManagement; public class Student { private String sno; private String sname; private String ssex; private int sage; public Student() { super(); } public Student(String sno, String sname, String ssex, int sage) { super(); this.sno = sno; this.sname = sname; this.ssex = ssex; this.sage = sage; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public int getSage() { return sage; } public void setSage(int sage) { this.sage = sage; } }
②Class
package StudentManagement; public class Class { private String cno; private String cname; private Student[] stus; private int size; private int count=0; public Class() { this.size=10; stus=new Student[this.size]; } public Class(String cno, String cname, int size) { super(); this.cno = cno; this.cname = cname; this.size = size; stus=new Student[this.size]; } public String getCno() { return cno; } public void setCno(String cno) { this.cno = cno; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Student[] getStus() { return stus; } public void setStus(Student[] stus) { this.stus = stus; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public void addStudent(Student stu){ if(count<stus.length){ stus[count]=stu; count++; System.out.println("添加学生成功!"); }else{ System.out.println("班级人数已满!"); } } }
③ManageService
package StudentManagement; import java.util.Scanner; public class ManageService { private Class[] classes; private int size; private int count = 0; public ManageService() { this.size = 10; classes = new Class[this.size]; } public ManageService(int size) { this.size = size; classes = new Class[this.size]; } public void addClazz(Class c){ if(count<classes.length){ classes[count] = c; count++; System.out.println("班级添加成功!"); }else{ System.out.println("班级数量达到上限,不能添加班级!"); } } public void printAllClass(){ System.out.println("班级编号\t班级名称"); for(int i=0;i<classes.length;i++){ Class c = classes[i]; if(c!=null){ System.out.println(c.getCno()+"\t"+c.getCname()); } } } public Class getClassByNo(String cno){ Class clazz = null; for(int i=0;i<classes.length;i++){ Class c = classes[i]; if(c!=null){ if(c.getCno().equals(cno)){ clazz = c; } }else{ break; } } return clazz; } public Student[] getStusByClass(String cno){ Student[] stus = null; for(int i=0;i<classes.length;i++){ Class c = classes[i]; if(c!=null){ if(c.getCno().equals(cno)){ stus = c.getStus(); } }else{ break; } } return stus; } public Student getStuByNo(String sno){ Student stu = null; for(int i=0;i<classes.length;i++){ Class c = classes[i]; if(c!=null){ Student[] stus = c.getStus(); for(int j=0;j<stus.length;j++){ Student s = stus[j]; if(s!=null){ if(s.getSno().equals(sno)){ stu = s; } }else{ break; } } }else{ break; } } return stu; } public void deleteStuByNo(String sno){ for(int i=0;i<classes.length;i++){ Class c = classes[i]; if(c!=null){ Student[] stus = c.getStus(); for(int j=0;j<stus.length;j++){ Student s = stus[j]; if(s!=null){ if(s.getSno().equals(sno)){ stus[j] = null; } }else{ break; } } }else{ break; } } } public void deleteClassByNo(String cno){ for(int i=0;i<classes.length;i++){ Class c = classes[i]; if(c!=null){ if(c.getCno().equals(cno)){ classes[i]=null; } }else{ break; } } } public void run(){ System.out.println("欢迎来到苏大学生管理系统!"); Scanner sc = new Scanner(System.in); while(true){ System.out.println("1.添加班级,2.添加学生,3.查看班级,4.查看学生,5.删除学生,6.删除班级,7.退出"); String comd = sc.next(); if(comd.equals("1")){ System.out.println("请输入班级编号:"); String cno = sc.next(); Class c = getClassByNo(cno); if(c==null){ System.out.println("请依次输入班级名称,人数:"); String cname = sc.next(); int size = sc.nextInt(); c = new Class(cno, cname, size); addClazz(c); }else{ System.out.println("班级已经存在!"); } }else if(comd.equals("2")){ System.out.println("请输入班级编号:"); String cno = sc.next(); Class c = getClassByNo(cno); if(c!=null){ System.out.println("请依次输入学号,姓名,性别,年龄:"); String sno = sc.next(); String sname = sc.next(); String ssex = sc.next(); int sage=sc.nextInt(); Student stu = new Student(sno, sname, ssex, sage); c.addStudent(stu); }else{ System.out.println("班级不存在!"); } }else if(comd.equals("3")){ printAllClass(); }else if(comd.equals("4")){ System.out.println("1.根据班级查看,2.查看学生个人详情"); String type = sc.next(); if(type.equals("1")){ System.out.println("请输入班级编号:"); String cno = sc.next(); Student[] stus = getStusByClass(cno); if(stus!=null){ System.out.println("学号\t姓名\t性别\t年龄"); for(int j=0;j<stus.length;j++){ Student s = stus[j]; if(s!=null){ System.out.println(s.getSno()+"\t"+s.getSname()+"\t"+s.getSsex()+"\t"+s.getSage()); }else{ break; } } }else{ System.out.println("班级不存在,或该班级暂无学生!"); } }else if(type.equals("2")){ System.out.println("请输学号:"); String sno = sc.next(); Student stu = getStuByNo(sno); if(stu!=null){ System.out.println("学号\t姓名\t性别\t年龄"); System.out.println(stu.getSno()+"\t"+stu.getSname()+"\t"+stu.getSno()+"\t"+stu.getSage()); }else{ System.out.println("学生"+sno+"不存在!"); } } }else if(comd.equals("5")){ System.out.println("请输学号:"); String sno = sc.next(); Student stu = getStuByNo(sno); if(stu!=null){ deleteStuByNo(sno); System.out.println("删除成功!"); }else{ System.out.println("学生"+sno+"不存在!"); } }else if(comd.equals("6")){ System.out.println("请输班级编号:"); String cno = sc.next(); Class c = getClassByNo(cno); if(c!=null){ deleteClassByNo(cno); System.out.println("删除成功!"); }else{ System.out.println("班级"+cno+"不存在!"); } }else if(comd.equals("7")){ System.out.println("系统退出!"); System.exit(0); }else{ System.out.println("命令不合法!"); } } } }
④App
package StudentManagement; public class App { public static void main(String[] args) { ManageService ss=new ManageService(5); ss.run(); } }
时间: 2024-10-09 23:10:54