## **Java期末大作业-实现学生管理系统**
运用java实现学生管理系统的增删改查,以及查询和浏览相同班级的人数,最后实现用文件的方式来保存,比较综合,适合拿来去练习。
主要运用了集合的方式-哈希来实现,比较方便,哈希是一个比较重要的知识点,它可以比较快捷的查询对应学号的学生以及一些基本功能。
大体结构:
创建一个学生类来存放属性,创建一个接口来存放学生类的抽象方法,创建另一个类来继承学生类并实现接口的各种方法,最后Test类进行测试。
Test类
import java.io.IOException;
public class Test {
public static void main(String[] args) {
Studentmethod s1 = new Studentmethod();
java.util.Scanner s = new java.util.Scanner(System.in);
int a1=2;
while (a1 != 4) {
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("1.学生增加 2.查找学生 3.浏览全部 4.退出 5.删除学生 6.修改学生信息");
System.out.println("请输入你的选择:");
int a = s.nextInt();
switch (a) {
case 1:
s1.Add();//添加
break;
case 2:
s1.search();//查找
break;
case 3:
s1.show();//浏览
break;
case 4:
break;//退出
case 5:
s1.deleteData();//删除
break;
case 6:
s1.update();//修改
break;
case 7:s1.searchClass();//查找相同班级的人数,和展示
break;
}
}
}
}
学生类
import java.io.*;
import java.util.*;
public class Student {
String sno;//学号
String name;//姓名
int age;//年龄
String Class1;//班级
public Student() {
}
public Student(String sno, String name, int age, String Class1) {
this.sno = sno;
this.name = name;
this.age = age;
this.Class1 = Class1;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSno() {
return sno;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setClass(int Class1) {
Class1 = Class1;
}
public String getClass1() {
return Class1;
}
public String toString() {
return "学号: " + getSno() + " 姓名: " + getName() + " 年龄: " + getAge() + " 班级: " + getClass1();
}
}
学生接口类
//学生方法接口
public interface StudentIV {
public abstract void Add();
public abstract void show();
public abstract void search();
public abstract void deleteData();
public abstract void update();
public abstract void searchClass();
}
实现接口类
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Studentmethod extends Student implements StudentIV {
java.util.Scanner s = new java.util.Scanner(System.in);
Map<String, Student> map = new HashMap<>();
//添加学生
public void Add(){
boolean sr = true;
while (sr) {
System.out.println("请输入学号:");
String s1 = s.next();
if (map.containsKey(s1)) {
System.out.println("该学号已经存在,请重新输入");
Add();
break;
}
System.out.println("请输入姓名:");
String s2 = s.next();
System.out.println("请输入年龄:");
int s3 = s.nextInt();
System.out.println("请输入班级: ");
String s4 = s.next();
Student stu = new Student(s1, s2, s3, s4);
map.put(s1, stu);//学号为key,学生对象是value
//保存到文件中
File destPath = new File("c:\\dell\\temp.txt");
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(destPath));
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write("学生学号\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write("学生姓名\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write("学生年龄\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write("学生班级\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.newLine();//换行
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
// 获取集合中的每一个Student对象
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
for (String sno : set) {
Student a = map.get(sno);//获取学生对象
try {
bw.write(a.getSno() + "\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write(a.getName() + "\t");
} catch (IOException e) {
e.printStackTrace();
}
// bw.write(a.getSno() + "\t");
try {
bw.write(a.getAge() + "\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write(a.getClass1() + "\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
//释放资源
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("是否继续输入true/false");
sr = s.nextBoolean();
if (sr == false) {
System.out.println("录入完成");
break;
}
}
}
//浏览全部
public void show(){
if (map.size() == 0) {
System.out.println("没有学生信息");
}
File destPath = new File("c:\\dell\\temp.txt");
// 获取集合中的每一个Student对象
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(destPath));
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write("学生学号\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write("学生姓名\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write("学生年龄\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write("学生班级\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
for (String sno : set) {
//6: 把Student信息存储到文本文件中
//数据源Student s
//目的地Student.txt
Student a = map.get(sno);
try {
bw.write(a.getSno() + "\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write(a.getName() + "\t");
} catch (IOException e) {
e.printStackTrace();
}
// bw.write(a.getSno() + "\t");
try {
bw.write(a.getAge() + "\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write(a.getClass1() + "\t");
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
//释放资源
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
for (String s1 : set) {
System.out.println(map.get(s1));
}
}
//根据学号查询学生
public void search() {
boolean a = true;
while (a) {
if (map.size() == 0) {
System.out.println("没有学生信息");
}
System.out.println("请输入你要查询的学生学号:");
String sno = s.next();
if (map.containsKey(sno)) {
System.out.println(map.get(sno));
} else {
System.out.println("查询不到此学号");
}
System.out.println("是否继续查询 true/false");
a = s.nextBoolean();
if (a == false) {
System.out.println("查询结束");
break;
}
}
}
//根据学号删除
public void deleteData() {
boolean t = true;
while (t) {
if (map.size() == 0) {
System.out.println("没有学生信息,无法删除");
}
System.out.println("请输入你要删除学生的学号");
sno = s.next();
if (map.containsKey(sno)) {
map.remove(sno);
System.out.println("删除成功");
} else {
System.out.println("学号不存在");
}
System.out.println("是否继续删除 true/false");
t = s.nextBoolean();
if (t == false) {
System.out.println("删除结束");
break;
}
}
}
//按照学号修改
public void update() {
System.out.println("请输入你要修改的学生学号:");
String sno = s.next();
if (map.containsKey(sno)) {
System.out.println("请重新输入姓名:");
String name = s.next();
System.out.println("请重新输入年龄:");
int age = s.nextInt();
Student stu = map.get(sno);
stu.setName(name);
stu.setAge(age);
System.out.println("信息修改成功");
}
}
//统计班级相同的人数
public void searchClass() {
if(map.size()==0){
System.out.println("没有学生信息");
}
System.out.println("请输入你要查询的班级:");
String Class = s.next();
//获取所有value
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
int count=0;
while(it.hasNext()){
String sno=it.next();
Student t=map.get(sno);
if(t.getClass1().equals(Class)){
count++;
System.out.println(t);
}
}
System.out.println("相同班级的人数:"+count);
}
}