Java之學生資訊管理系統(File類、集合類)
使用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();
}
}
學生管理類便是對學生資訊的一些處理,其中載入使用了正則表示式中對空格進行分割,便於將資訊提取出來,在儲存時,也是相應的使用空格來分割字串。
就先到這裡吧,我下一步將使用資料庫來儲存資訊,畢竟檔案儲存變數太多,檔案易丟失。