使用Java编写一个能增删改查以及保存和加载的学生信息管理系统,使用集合类来存储学生的信息,使用File类将信息保存到文件中,方便下一次调用。
直接上代码:
学生类:
package Student;
public class Student {
private String Name;
private int StNo;
private int StAge;
public void SetStudent(String name,int StNo,int StAge){
this.Name = name;
this.StNo = StNo;
this.StAge = StAge;
}
public void print(){
System.out.println("学号:"+this.StNo+" "+"姓名:"+this.Name+" "+"年龄:"+this.StAge);
}
public int GetNO()
{
return this.StNo;
}
public String GetName(){
return this.Name;
}
public int GetAge(){
return this.StAge;
}
}
学生类中声明了学生的属性和属性的接口函数,方便我们在集合类中去调用它。
学生管理类:
package Student;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class StList {
ArrayList<Student> list = new ArrayList<>();
public boolean IsNoUsed(int No){
for(int i=0;i<list.size();i++)
{
if(list.get(i).GetNO()==No)
{
return true;
}
}
return false;
}
public void Insert(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号姓名年龄:");
int No = scanner.nextInt();
if(IsNoUsed(No))
{
System.out.println("输入的学号已被使用!");
return;
}
String str = scanner.next();
int Age = scanner.nextInt();
list.add(new Student());
int count = list.size()-1;
list.get(count).SetStudent(str, No, Age);
}
public void Print(){
int count = list.size();
for(int i=0;i<count;i++)
{
list.get(i).print();
}
}
public void Delete(int n){
if(!IsNoUsed(n))
{
System.out.println("输入学号有误!");
return;
}
int count;
for(count = 0;count<list.size();count++)
{
if(n != list.get(count).GetNO())
{
count++;
}
else
{
break;
}
}
int i = count;
while(i < list.size()-1)
{
list.set(i, list.get(i+1));
i++;
}
list.remove(i);
}
public void Revise(int inde){
if(!IsNoUsed(inde))
{
System.out.println("输入的学号有误!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入姓名和年龄:");
String str = scanner.next();
int age = scanner.nextInt();
int i;
for(i=0;i<list.size();i++)
{
if(list.get(i).GetNO()==inde)
{
break;
}
}
list.get(i).SetStudent(str, inde, age);
}
public void FileSave(){
File file = new File("D:\\JAVA_练\\Student\\bin\\Student\\a.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
int count = list.size()-1;
String str = null;
str = list.size()+"\r\n";
bw.write(str);
for(int i=0;i<=count;i++){
str = list.get(i).GetNO()+" "+list.get(i).GetName()+" "+list.get(i).GetAge()+" \r\n";
System.out.println(str);
bw.write(str);
}
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void FileLoad(){
File file = new File("D:\\JAVA_练\\Student\\bin\\Student\\a.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
BufferedReader br = new BufferedReader(new FileReader(file));
try {
String str = br.readLine();
if(null == str || "0".equals(str))
{
System.out.println("没有数据,无法载入!");
return;
}
int count = Integer.parseInt(str);
int StuNo;
String StuName;
int StuAge;
String[] infor;
for(int i=0;i<count;i++)
{
str = br.readLine();
System.out.println(str);
infor=str.split("\\s+");
StuNo = Integer.parseInt(infor[0]);
StuName = infor[1];
StuAge = Integer.parseInt(infor[2]);
list.add(new Student());
list.get(i).SetStudent(StuName, StuNo, StuAge);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void Show(){
Scanner scanner = new Scanner(System.in);
int value;
while(true){
System.out.println("学生管理系统");
System.out.println("请输入相应的数字选择功能方式:");
System.out.println("1:增加\t2:删除\t3:修改\t4:查看\t5:载入\t6:保存\t7:退出");
value = scanner.nextInt();
switch (value) {
case 1:
this.Insert();
break;
case 2:
System.out.println("请输入要删除的学号:");
int index = scanner.nextInt();
this.Delete(index);
break;
case 3:
System.out.println("请输入要修改的学号!");
int inde = scanner.nextInt();
this.Revise(inde);
break;
case 4:
this.Print();
break;
case 5:
FileLoad();
break;
case 6:
FileSave();
break;
case 7:
System.out.println("程序已退出!");
return;
default:
System.out.println("您的输入有误!");
break;
}
}
}
public static void main(String[] arg){
StList list = new StList();
list.Show();
}
}
学生管理类便是对学生信息的一些处理,其中加载使用了正则表达式中对空格进行分割,便于将信息提取出来,在保存时,也是相应的使用空格来分割字符串。
就先到这里吧,我下一步将使用数据库来存储信息,毕竟文件存储变数太多,文件易丢失。