《java语言的学生信息管理系统(集合类)(共8页).doc》由会员分享,可在线阅读,更多相关《java语言的学生信息管理系统(集合类)(共8页).doc(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上Java的学生信息管理源代码:必须配合学生类才可以运行,学生类在下方,我建议最好还是把学生类分成另一个类来运行比较好!package com.geminno.day12.homework;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.i
2、o.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.List;import java.util.Properties;import java.util.Scanner;public class StudentManagement /类private static int ids = 10000;/定义属性private static List datas;/定义集合public static voi
3、d showStudents()/方法for(Student stu : datas)/遍历数组datasSystem.out.println(stu.getId()+-+stu.getName()+-+stu.getGender()+-+stu.getMajor()+-+stu.getAge();/输出结果public static void main(String args) /主方法int input;/定义整型datas = new ArrayList();/实例化对象/初始化数据initData();Scanner s = new Scanner(System.in);/从键盘输入w
4、hile(true)/进入菜单循环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(| 请输入选择(1-6): |);System.out.println(*);System.out.
5、print(请选择您要的操作:);input = s.nextInt();/从键盘输入switch(input)/switch语句case 1:/第一种情况System.out.print(请输入姓名:);String name = s.next();/输入姓名System.out.print(请输入性别:);String gender = s.next();System.out.print(请输入年龄:);int age = s.nextInt();System.out.print(请输入专业:);String major = s.next();Student stud = new Stud
6、ent(String.valueOf(ids+),name,gender,age,major);/创建一个对象datas.add(stud);System.out.println(增加成功!);break;/跳出循环case 2:/情况2if(datas.size() 0)/如果datas长度大于0showStudents();/则显示学生elseSystem.out.println(无学生信息!);/不然就输出break;/跳出循环case 3:/情况3System.out.print(请输入您要修改的学生学号:);String id = s.next();Student stu = fin
7、dStudentById(id);/修改之前要先查找到该学生boolean flag = false;/定义个标志位if(stu = null)/如果查找的人为空System.out.println(对不起,没有您要修改的学生);/则输出elsewhile (true) /不然进入循环/修改对应的属性System.out.println(*);System.out.println(| 1. 修改姓名 |);System.out.println(| 2. 修改年龄 |);System.out.println(| 3. 修改性别 |);System.out.println(| 4. 修改专业 |)
8、;System.out.println(| 5. 返回 |);System.out.println(| 请输入选择(1-5): |);System.out.println(*);System.out.print(请选择您要的操作:);int updateInput = s.nextInt();/从键盘输入switch (updateInput) /switch语句case 1:/修改的情况1System.out.print(请输入姓名:);String uname = s.next();stu.setName(uname);/设置名字System.out.println(修改成功!);brea
9、k;/跳出循环case 2:/修改的情况2System.out.print(请输入年龄:);int uage = s.nextInt();stu.setAge(uage);/设置年龄System.out.println(修改成功!);break;/跳出循环case 3:/修改的情况3System.out.print(请输入性别:);String ugender = s.next();stu.setGender(ugender);/设置年龄System.out.println(修改成功!);break;/跳出循环case 4:/修改的情况4System.out.print(请输入专业:);Str
10、ing umajor = s.next();stu.setMajor(umajor);/设置专业System.out.println(修改成功!);break;/跳出循环case 5:/修改的情况5flag = true;/默认标志位为false就进入循环,如果为true,就返回break;/跳出循环default:System.out.println(您的输入有误);/否则就输入有误if(flag)break;break;case 4:/情况4System.out.print(请输入您要删除的学生学号:);String deleteId = s.next();Student deleteSt
11、ud = findStudentById(deleteId);/在删除之前要先找到该学生if(deleteStud = null)/如果该学生为空System.out.println(对不起,没有您要删除的学生!);/则输出elsedatas.remove(deleteStud);/否则就删除System.out.println(删除成功!);break;/跳出循环case 5:/情况5System.out.print(请输入您要查找的学生学号:);String searidId = s.next();Student searchStud = findStudentById(searidId)
12、;/要查找的学生,根据id进行查找if(searchStud = null)/如果查找的学生没有找到System.out.println(对不起,没有您要找的学生!);/则输出else/否则就显示出信息System.out.println(您要查找的学生信息如下:);System.out.println(searchStud.getId()+-+searchStud.getName()+-+searchStud.getGender()+-+searchStud.getMajor()+-+searchStud.getAge();break;case 6:/情况6/保存数据saveDate();/
13、保存System.exit(0);/退出break;default:System.out.println(您的输入有误);/否则输入有误private static void initData() /初始化数据try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(stud.txt);/读取)/都是整理输出的结果String str;while(str = br.readLine() != null)/str:Student id=10000, name=jack
14、, gender=男, age=20, major=计算机String studStr = str.substring(str.indexOf() + 1, str.lastIndexOf();/studStr:id=10000, name=jack, gender=男, age=20, major=计算机String studArray = studStr.split(,);/id=10000 name=jack gender=男 age=20 major=计算机Student obj = new Student();for(int i = 0;i studArray.length;i+)i
15、f(i = 0)String id = studArrayi.split(=);/id 10000obj.setId(id1);else if(i = 1)String name = studArrayi.split(=);/name jackobj.setName(name1);else if(i = 2)String gender = studArrayi.split(=);/ gender 男obj.setGender(gender1);else if(i = 3)String age = studArrayi.split(=);/age 20obj.setAge(Integer.par
16、seInt(age1);else if(i = 4)String major = studArrayi.split(=);/major 计算机obj.setMajor(major1);datas.add(obj);/解决id重复的问题Student lastStud = datas.get(datas.size() - 1);int lastId = Integer.parseInt(lastStud.getId();ids = +lastId; catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e)
17、 e.printStackTrace();private static void saveDate() try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(stud.txt);/写出)/datasfor(Student stud : datas)/遍历datasbw.write(stud.toString();/写出到stud.txt文件中bw.newLine();/换行catch (FileNotFoundException e) e.printSta
18、ckTrace(); catch (IOException e1) e1.printStackTrace();private static Student findStudentById(String id) for(Student stud : datas)/遍历datasif(stud.getId().equals(id)/如果找的id和本身的id相等则返回studreturn stud;return null;这是学生类:public class Student private String id;private String name;private String gender;pri
19、vate int age;private String major;Overridepublic String toString() return Student id= + id + , name= + name + , gender= + gender+ , age= + age + , major= + major + ;Overridepublic int hashCode() final int prime = 31;int result = 1;result = prime * result + (id = null) ? 0 : id.hashCode();return resu
20、lt;Overridepublic boolean equals(Object obj) if (this = obj)return true;if (obj = null)return false;if (getClass() != obj.getClass()return false;Student other = (Student) obj;if (id = null) if (other.id != null)return false; else if (!id.equals(other.id)return false;return true;public String getId()
21、 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 getGender() return gender;public void setGender(String gender) this.gender = gender;public int getAge() return age;public void setAge(int age) this.age = age;public String getMajor() return major;public void setMajor(String major) this.major = major;public Student(String id, String name, String gender, int age, String major) super();this.id = id;this.name = name;this.gender = gender;this.age = age;this.major = major;public Student() 专心-专注-专业
限制150内