用Java写学生管理系统,保存本地文件
##源码
package StudentManger;
import java.io.*;
import java.util.*;
public class StudentManger {
Map<String,Student> map=new HashMap<String,Student>();
public void PrintCommand(){
System.out.println("1.添加学生");
System.out.println("2.删除学生");
System.out.println("3.修改学生");
System.out.println("4.显示所有学生");
System.out.println("0.退出系统");
}
public void Start(){
int command=4;
do {
PrintCommand();
Scanner scanner = new Scanner(System.in);
command = scanner.nextInt();
switch (command) {
case 1:
Add();
break;
case 2:
Remove();
break;
case 3:
Rewrite();
break;
case 4:
Show();
break;
case 0:
}
}while(command!=0);
}
public int Add(){
System.out.println("请输入需要添加的学生信息:(格式“ID - Name- class”)");
Scanner scanner=new Scanner(System.in);
String info=scanner.next();
String infos[]=info.split("-");
if(infos.length!=3||infos==null){
System.out.println("输入格式错误!!!"+info);
return 0;
}
Student student=new Student();
student.setId(infos[0]);
student.setName(infos[1]);
student.setClazz(infos[2]);
String id=student.getId();
if(!map.containsKey(id)){
map.put(id,student);
System.out.println("添加成功!!!");
System.out.println();
saveFile(map,"C:\\Users\\YDB\\Desktop\\test1.txt");
return 1;
}else{
System.out.println(infos[0]+"此ID以存在,添加失败!!!");
}
return 0;
}
public int Remove(){
Show();
System.out.println("请输入需要删除的学生ID:");
Scanner scanner=new Scanner(System.in);
String id=scanner.next();
if(map.containsKey(id)){
map.remove(id);
System.out.println("删除成功!!!");
System.out.println();
saveFile(map,"C:\\Users\\YDB\\Desktop\\test1.txt");
return 1;
}else{
System.out.println("该ID不存在,删除失败!!!");
}
return 0;
}
public int Rewrite(){
Show();
System.out.println("请输入需要修改的学生ID:");
Scanner scanner=new Scanner(System.in);
String id1=scanner.next();
if(map.containsKey(id1)){
System.out.println("请输入该学生的信息:(格式“ID - Name- class”)");
Scanner scanner1=new Scanner(System.in);
String info=scanner.next();
String infos[]=info.split("-");
if(infos.length!=3||infos==null){
System.out.println("输入格式错误!!!"+info);
return 0;
}
Student student=new Student();
student.setId(infos[0]);
student.setName(infos[1]);
student.setClazz(infos[2]);
String id=student.getId();
map.put(id,student);
System.out.println("修改成功!!!");
System.out.println();
saveFile(map,"C:\\Users\\YDB\\Desktop\\test1.txt");
return 1;
}else{
System.out.println("该ID不存在,修改失败!!!");
}
return 0;
}
public void Show(){
map=readFile("C:\\Users\\YDB\\Desktop\\test1.txt");
Set<String> set=map.keySet();
List<String> list=new ArrayList<String>();
list.addAll(set);
Collections.sort(list);
System.out.println("----------------------------------------");
for(String l:list){
System.out.println(map.get(l));//默认调用map.get(l).toString
}
System.out.println("----------------------------------------");
}
public static void saveFile(Map<String,Student> map,String path){
FileOutputStream fileOutputStream =null;
ObjectOutputStream objectOutputStream=null;
try {
fileOutputStream=new FileOutputStream(new File(path));
objectOutputStream=new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(map);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static Map<String,Student> readFile(String path){
Map<String,Student> map=null;
FileInputStream fileInputStream=null;
ObjectInputStream objectInputStream=null;
try{
fileInputStream=new FileInputStream(new File(path));
objectInputStream=new ObjectInputStream(fileInputStream);
map=(Map<String,Student>)objectInputStream.readObject();
}catch (IOException e){
e.printStackTrace();
}catch (ClassNotFoundException e){
e.printStackTrace();
}finally {
if(fileInputStream!=null){
try {
fileInputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
return map;
}
public static void main(String[] args) {
StudentManger manger=new StudentManger();
manger.Start();
}
}
class Student implements Serializable{
private String id;
private String name;
private String clazz;
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {//对toString 的重写,也可以不重写,调用map.get(l).getName()
return "ID:"+id+" Name:"+name+" class:"+clazz;
}
}