本文实例为大家分享了Java实现学生教师管理系统的具体代码,供大家参考,具体内容如下
需求:
我们可以通过管理系统对学生和教师进行管理
对象学生和教师进行增删改查等的功能
1、Student和Teacher的父类
2、Student类
3、Teacher类
4、Utils工具类
5、测试类
Student和Teacher的父类
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 |
public class Person { private String id; // 编号 private String name; // 姓名 private String IDcard; // 身份证 private String sex; // 性别 private String birthday; // 生日 private int age; //年龄 private String site; //地址 private String phone; // 电话
public Person() { }
public Person(String id, String name, String IDcard, String sex, String birthday, String site, String phone) throws ParseException { this .id = id; this .name = name; this .IDcard = IDcard; this .sex = sex; this .birthday = birthday; this .age = Utils.birthdayToAge(birthday); this .site = site; this .phone = phone; }
public String getId() { return id; }
public void setId(String id) { this .id = id; }
public String getName() { return name; }
public void setName(String name) { this .name = name; }
public String getIDcard() { return IDcard; }
public void setIDcard(String IDcard) { this .IDcard = IDcard; }
public String getSex() { return sex; }
public void setSex(String sex) { this .sex = sex; }
public String getBirthday() { return birthday; }
public void setBirthday(String birthday) throws ParseException { this .birthday = birthday; this .age = Utils.birthdayToAge(birthday); }
public int getAge() { return age; }
public String getSite() { return site; }
public void setSite(String site) { this .site = site; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this .phone = phone; } } |
Student类
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 |
import java.text.ParseException;
public class Student extends Person { private int grade; // 成绩 private String group; // 班级
// 学生号,姓名,身份证,性别,生日,地址,手机号,成绩,班级 public Student(String id, String name, String IDcard, String sex, String birthday, String site, String phone, int grade, String group) throws ParseException { super (id, name, IDcard, sex, birthday, site, phone); this .grade = grade; this .group = group; }
public Student() { }
public int getGrade() { return grade; }
public void setGrade( int grade) { this .grade = grade; }
public String getGroup() { return group; }
public void setGroup(String group) { this .group = group; } } |
Teacher类
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 |
import java.text.ParseException;
public class Teacher extends Person{ private String career; // 专业 private String salary; // 工资 // 教师号,姓名,身份证,性别,生日,地址,手机号,专业,工资 public Teacher(String id, String name, String IDcard, String sex, String birthday, String site, String phone, String career, String salary) throws ParseException { super (id, name, IDcard, sex, birthday, site, phone); this .career = career; this .salary = salary; }
public Teacher() { }
public String getCareer() { return career; }
public void setCareer(String career) { this .career = career; }
public String getSalary() { return salary; }
public void setSalary(String salary) { this .salary = salary; } } |
根据生日计算年龄的一个静态工具类
Utils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;
public class Utils { public static int birthdayToAge(String birthday) throws ParseException {
SimpleDateFormat sdf= new SimpleDateFormat( "yyyy-MM-dd" ); Date date = sdf.parse(birthday); Date date1 = new Date(); long time = date.getTime(); long time1 = date1.getTime(); long age = (time1 - time) / 1000 / 60 / 60 / 24 / 365 ; return ( int )age; } } |
测试类
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
import java.text.ParseException; import java.util.ArrayList; import java.util.Scanner;
public class StudentAndTeacherSystem { public static Scanner sc = new Scanner(System.in); public static ArrayList<Student> students = new ArrayList(); public static ArrayList<Teacher> teachers = new ArrayList(); public static void main(String[] args) throws ParseException { System.out.println( "********************欢迎来到学生教师管理系统********************" ); while ( true ) { System.out.println( "【1】 学生管理系统 【2】 教师管理系统 【3】 退出" );
String num = sc.next(); switch (num) { case "1" : // 学生管理系统 studentSystem(); break ; case "2" : // 教师管理系统 teacherSystem(); break ; case "3" : System.out.println( "感谢您的使用!" ); System.exit( 0 ); default : System.out.println( "您输入的编号" + num + "有误!" ); break ; } } } // 学生 public static void studentSystem() throws ParseException {
System.out.println( "**********************【欢迎来到学生管理系统】**********************" ); while ( true ){ System.out.println( "*******【1】添加学生***************************【2】 修改学生*********" ); System.out.println( "*******【3】删除学生***************************【4】 查看所有学生******" ); System.out.println( "*******【5】查找指定学生************************【6】 统计所有学生信息**" ); System.out.println( "***************************【7】 返回上一级***************************" ); System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~【请输入您要选择的编号】~~~~~~~~~~~~~~~~~~~~~~~~" ); String num = sc.next(); switch (num){ case "1" : // 添加 addStudent(); break ; case "2" : // 修改 updateStudent(); break ; case "3" : // 删除 deleteStudent(); break ; case "4" : // 查看 selectStudent(); break ; case "5" : // 指定查看 assignStudent(); break ; case "6" : // 统计 statisticsStudent(); break ; case "7" : System.out.println( "返回上一级" ); return ; default : System.out.println( "您输入的编号" + num + "有误!" ); break ; }
}
} public static void addStudent() throws ParseException { // System.out.println("添加成功!"); System.out.println( "请输入您的学生号:" ); while ( true ) { String id = sc.next(); int index = getIndex(students, id); if (index == - 1 ) { System.out.println( "请输入您的姓名:" ); String name = sc.next(); System.out.println( "请输入您的身份证:" ); String IDcard = sc.next(); System.out.println( "请输入您的性别:" ); String sex = sc.next(); System.out.println( "请输入您的生日(格式:2000-10-10)" ); String birthday = sc.next(); System.out.println( "请输入您的家庭地址:" ); String site = sc.next(); System.out.println( "请输入您的手机号:" ); String phone = sc.next(); System.out.println( "请输入您的成绩:" ); int grade = sc.nextInt(); System.out.println( "请输入您的班级:" ); String group = sc.next(); Student stu = new Student(id, name, IDcard, sex, birthday, site, phone, grade, group); students.add(stu); System.out.println( "添加成功!" ); return ; } else { System.out.println( "您输入的学生号已存在,请重新输入!" ); } }
} // 修改 public static void updateStudent() throws ParseException { // System.out.println("修改成功!"); System.out.println( "请输入您要修改的学生号:" ); String id = sc.next(); int index = getIndex(students,id); if (index == - 1 ){ System.out.println( "您输入的学生号不存在!!" ); return ; } System.out.println( "请输入您的姓名:" ); String name = sc.next(); System.out.println( "请输入您的身份证:" ); String IDcard = sc.next(); System.out.println( "请输入您的性别:" ); String sex = sc.next(); System.out.println( "请输入您的生日:" ); String birthday = sc.next(); System.out.println( "请输入您的地址:" ); String site = sc.next(); System.out.println( "请输入您的手机号:" ); String phone = sc.next(); System.out.println( "请输入您的成绩:" ); int grade = sc.nextInt(); System.out.println( "请输入您的班级:" ); String group = sc.next(); Student stu = new Student(id,name, IDcard,sex,birthday,site,phone,grade,group); students.set(index,stu); System.out.println( "修改成功!" );
} // 删除 public static void deleteStudent() { // System.out.println("删除成功!"); System.out.println( "请输入您要删除的学生号:" ); String id = sc.next(); int index = getIndex(students,id); if (index == - 1 ){ System.out.println( "您输入的学生号不存在!" ); return ; } else { students.remove(index); System.out.println( "删除成功!" ); return ; }
} // 查看 public static void selectStudent() { // System.out.println("查看成功!"); int num = students.size(); if (num == 0 ){ System.out.println( "暂无信息,请添加以后在来查看!" ); return ; } // 编号,姓名,身份证,性别,生日,地址,手机号,成绩,班级 System.out.println( "学生号\t姓名\t身份证\t性别\t生日\t年龄\t地址\t手机号\t成绩\t班级" ); for ( int i = 0 ; i < students.size(); i++) { Student stu = students.get(i); System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getIDcard() + "\t" + stu.getSex() + "\t" + stu.getBirthday() + "\t" + stu.getAge() + "\t" + stu.getSite() + "\t" + stu.getPhone() + "\t" + stu.getGrade() + "\t" + stu.getGroup()); } } // 指定查看 public static void assignStudent() { // System.out.println("指定查看成功!"); System.out.println( "请输入您要查找的学生号:" ); String id = sc.next(); int index = getIndex(students,id); if (index == - 1 ){ System.out.println( "您输入的学生号不存在!" ); } else { // // 编号,姓名,身份证,性别,生日,地址,手机号,成绩,班级 for ( int i = 0 ; i < students.size(); i++) { Student stu = students.get(i); System.out.println( "学生号:" + stu.getId()); System.out.println( "姓 名:" + stu.getName()); System.out.println( "生 日:" + stu.getBirthday()); System.out.println( "年 龄:" + stu.getAge()); System.out.println( "身份证:" + stu.getIDcard()); System.out.println( "性 别:" + stu.getSex()); System.out.println( "地 址:" + stu.getSite()); System.out.println( "手机号:" + stu.getPhone()); System.out.println( "成 绩:" + stu.getGrade()); System.out.println( "班 级:" + stu.getGroup()); }
} } // 统计 public static void statisticsStudent() { // System.out.println("统计成功!"); int countSex = 0 ; int countGrade = 0 ; for ( int i = 0 ; i < students.size(); i++) { Student stu = students.get(i); if (stu.getSex().equals( "男" )){ countSex++; } if (stu.getGrade() > 60 ){ countGrade++; } } System.out.println( "学校一共有:" + students.size() + "个人" ); System.out.println( "男生有:" + countSex + "人" ); System.out.println( "女生有:" + (students.size() - countSex) + "人" ); System.out.println( "及格有:" + countGrade + "人" ); System.out.println( "不及格有:" + (students.size() - countGrade) + "人" ); } // 判断学生是否存在 public static int getIndex(ArrayList<Student> students,String id){ int index = - 1 ; for ( int i = 0 ; i < students.size(); i++) { Student stu = students.get(i); String sid = stu.getId(); if (sid.equals(id)){ index = i; } } return index; } //=================================================================================== // 教师 public static void teacherSystem() throws ParseException { System.out.println( "********************【欢迎来到教师管理系统】**************************" ); while ( true ){ System.out.println( "*******【1】添加教师***************************【2】 修改教师*********" ); System.out.println( "*******【3】删除教师***************************【4】 查看所有教师******" ); System.out.println( "*******【5】查找指定教师************************【6】 统计所有教师信息**" ); System.out.println( "***************************【7】 返回上一级***************************" ); System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~【请输入您要选择的编号】~~~~~~~~~~~~~~~~~~~~~~~~" ); String num = sc.next(); // 编号,姓名,身份证,性别,生日,地址,手机号,成绩,班级 switch (num){ case "1" : // 添加 addTeacher(); break ; case "2" : // 修改 updateTeacher(); break ; case "3" : // 删除 deleteTeacher(); break ; case "4" : // 查看 selectTeacher(); break ; case "5" : // 指定查看 assignTeacher(); break ; case "6" : // 统计 statisticsTeacher(); break ; case "7" : System.out.println( "返回上一级" ); return ; default : System.out.println( "您输入的编号" + num + "有误!" ); break ; }
} } // 添加 public static void addTeacher() throws ParseException { //System.out.println("添加成功!"); System.out.println( "请输入您的教师号:" ); while ( true ) { String id = sc.next(); int index = getindex(teachers, id); if (index == - 1 ) { System.out.println( "请输入您的姓名:" ); String name = sc.next(); System.out.println( "请输入您的身份证:" ); String IDcard = sc.next(); System.out.println( "请输入您的性别:" ); String sex = sc.next(); System.out.println( "请输入您的生日(格式:2000-10-10)" ); String birthday = sc.next(); System.out.println( "请输入您的家庭地址:" ); String site = sc.next(); System.out.println( "请输入您的手机号:" ); String phone = sc.next(); System.out.println( "请输入您的专业:" ); String career = sc.next(); System.out.println( "请输入您的工资:" ); String salary = sc.next(); Teacher teacher = new Teacher(id, name, IDcard, sex, birthday, site, phone, career, salary); teachers.add(teacher); System.out.println( "添加成功!" ); return ; } else { System.out.println( "您输入的教师号已存在,请重新输入!" ); } }
} // 修改 public static void updateTeacher() throws ParseException { //System.out.println("修改成功!"); System.out.println( "请输入您要修改的教师号:" ); String id = sc.next(); int index = getindex(teachers,id); if (index == - 1 ){ System.out.println( "您输入的教师号不存在!!" ); return ; } System.out.println( "请输入您的姓名:" ); String name = sc.next(); System.out.println( "请输入您的身份证:" ); String IDcard = sc.next(); System.out.println( "请输入您的性别:" ); String sex = sc.next(); System.out.println( "请输入您的生日:" ); String birthday = sc.next(); System.out.println( "请输入您的地址:" ); String site = sc.next(); System.out.println( "请输入您的手机号:" ); String phone = sc.next(); System.out.println( "请输入您的专业:" ); String career = sc.next(); System.out.println( "请输入您的工资:" ); String salary = sc.next(); Teacher teacher = new Teacher(id,name, IDcard,sex,birthday,site,phone,career,salary); teachers.set(index,teacher); System.out.println( "修改成功!" );
} // 删除 public static void deleteTeacher() { //System.out.println("删除成功!"); System.out.println( "请输入您要删除的教师号:" ); String id = sc.next(); int index = getindex(teachers,id); if (index == - 1 ){ System.out.println( "您输入的教师号不存在!" ); return ; } else { teachers.remove(index); System.out.println( "删除成功!" ); return ; }
} // 查看 public static void selectTeacher() { //System.out.println("查看成功!"); int num = teachers.size(); if (num == 0 ){ System.out.println( "暂无信息,请添加以后在来查看!" ); return ; } // 编号,姓名,身份证,性别,生日,地址,手机号,成绩,班级 System.out.println( "教师号\t姓名\t身份证\t性别\t生日\t年龄\t地址\t手机号\t专业\t工资" ); for ( int i = 0 ; i < teachers.size(); i++) { Teacher teacher = teachers.get(i); System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getIDcard() + "\t" + teacher.getSex() + "\t" + teacher.getBirthday() + "\t" + teacher.getAge() + "\t" + teacher.getSite() + "\t" + teacher.getPhone() + "\t" + teacher.getCareer() + "\t" + teacher.getSalary()); }
} // 指定查看 public static void assignTeacher() { //System.out.println("指定查看成功!"); System.out.println( "请输入您要查找的教师号:" ); String id = sc.next(); int index = getindex(teachers,id); if (index == - 1 ){ System.out.println( "您输入的教师号不存在!" ); } else { // // 编号,姓名,身份证,性别,生日,地址,手机号,成绩,班级 for ( int i = 0 ; i < teachers.size(); i++) { Teacher teacher = teachers.get(i); System.out.println( "教师号:" + teacher.getId()); System.out.println( "姓 名:" + teacher.getName()); System.out.println( "生 日:" + teacher.getBirthday()); System.out.println( "年 龄:" + teacher.getAge()); System.out.println( "身份证:" + teacher.getIDcard()); System.out.println( "性 别:" + teacher.getSex()); System.out.println( "地 址:" + teacher.getSite()); System.out.println( "手机号:" + teacher.getPhone()); System.out.println( "专 业:" + teacher.getCareer()); System.out.println( "工 资:" + teacher.getCareer()); }
}
} // 统计 public static void statisticsTeacher() { // System.out.println("统计成功!"); int countSex = 0 ; for ( int i = 0 ; i < teachers.size(); i++) { Teacher teacher = teachers.get(i); if (teacher.getSex().equals( "男" )){ countSex++; } } System.out.println( "学校一共有:" + teachers.size() + "个教师" ); System.out.println( "男教师有:" + countSex + "人" ); System.out.println( "女教师有:" + (teachers.size() - countSex) + "人" ); } // 判断教师是否存在 public static int getindex(ArrayList<Teacher> teachers,String id){ int index = - 1 ; for ( int i = 0 ; i < teachers.size(); i++) { Teacher teacher = teachers.get(i); String sid = teacher.getId(); if (sid.equals(id)){ index = i; } } return index; }
} |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/qq_44804064/article/details/115068953
查看更多关于Java实现简单的学生教师管理系统的详细内容...