好久沒有寫博客了,今天趕早先來一篇,就寫寫最近複習到的一個東西:基於ArrayList實現學生信息管理系統。
說到ArrayList,大家立馬想到的就是其操作方法了吧,有哪些呢,remove(),add()……當然我們來這裏不是講方法的,而是對這個的應用。
寫到這裏,我不禁想發表一下感慨,似乎這個學生信息管理系統是初學者避不開的話題,又或者說在做課程設計的時候總會有人選這個,爲什麼呢?其實很簡單,因爲這個裏邊的增刪改查最容易和我們學過的知識聯繫起來。那其他的就聯繫不起來了嗎?當然不是,簡單舉兩個例子:1、訂票系統;2、酒店管理系統。這麼一提醒大家是不是就很容易想到怎麼應用呢?
好,咱們接下來就切入正題:
先分析一下需求:
1、對學生信息的管理只要包括四種:增刪改查,也就是說要有四個方法;
2、那麼我們增刪改查的對象是誰呢?可以直接是學生對象嗎?顯然不合適,所以我們就用一個ArrayList來盛學生對象
3、我們要寫在一個java文件中,方法加static更方便調用
4、界面的設計和交互的設計(如界面佈局美觀性、輸入相同學號該怎麼提醒用戶、沒有用戶查詢的信息該怎麼處理……)
分析完需求我們就各個擊破:
先看一下主方法:在這裏用到的有while(true)控制多次循環,System.exit(0)控制退出程序,case穿透和方法的調用,因爲都比較簡單,所以不再一一贅述了,接着往下看
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<Student>();
while (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("請輸入你的選擇:");
Scanner sc = new Scanner(System.in);
String choiceString = sc.nextLine();
switch (choiceString) {
case "1":
findAllStudent(array);
break;
case "2":
addStudent(array);
break;
case "3":
deleteStudent(array);
break;
case "4":
updateStudent(array);
break;
case "5":
default:
System.out.println("謝謝你的使用 ");
System.exit(0);
break;
}
}
}
然後看增:首先我們需要調用scanner來完成輸入,而後接受輸入並判斷輸入內容與已有對象是否衝突(while循環來判斷,大家可以看到我引入了一個flag標誌來作爲判斷依據),接着賦值給對象,最後加入到ArrayList中
private static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
String sid;
while (true) {
System.out.println("請輸入學號:");
sid = sc.nextLine();
boolean flag = false;
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
if (s.getSid().equals(sid)) {
flag = true;
}
}
if (flag == true) {
System.out.println("你輸入的學號已經被佔用,請重新輸入");
} else {
break;
}
}
System.out.println("請輸入姓名:");
String name = sc.nextLine();
System.out.println("請輸入年齡:");
String age = sc.nextLine();
System.out.println("請輸入地址:");
String address = sc.nextLine();
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加學生成功");
}
然後我們看一下刪:這個相對來說比較簡單,也是輸入、接收對象、判斷、然後操作,故這裏不再贅述
private static void deleteStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要刪除學生的學號:");
String sid = sc.nextLine();
int index = -1;
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
if (s.getSid().equals(sid)) {
index = x;
break;
}
}
if (index != -1) {
array.remove(index);
System.out.println("刪除學生成功");
} else {
System.out.println("不好意思,你要刪除的學號對應的學生信息不存在,請回去重新選擇你的操作");
return;
}
}
接下來把查和改放到一起:相信大家看完之前的增和刪,再看查和改都沒有問題,大家往下看
private static void findAllStudent(ArrayList<Student> array) {
if (array.size() == 0) {
System.out.println("不好意思,目前沒有學生信息可供查看,請回去重新選擇你的操作");
return;
}
System.out.println("學號\t\t姓名\t年齡\t居住地");
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
}
private static void updateStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要修改學生的學號:");
String sid = sc.nextLine();
int index = -1;
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
if (s.getSid().equals(sid)) {
index = x;
break;
}
}
if (index != -1) {
System.out.println("請輸入新姓名:");
String name = sc.nextLine();
System.out.println("請輸入新年齡:");
String age = sc.nextLine();
System.out.println("請輸入新地址:");
String address = sc.nextLine();
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.set(index, s);
System.out.println("修改學生成功");
} else {
System.out.println("不好意思,你要修改的學號對應的學生信息不存在,請回去重新選擇你的操作");
return;
}
}
最後在這裏做一下總結,大家會發現,
1、我們對學生信息的操作其實就是藉助ArraList的add(),remove()方法進行操作,同理,在我們呢用到其他地方也可以類似的進行推理
2、在程序中是有大量的代碼冗餘的,爲什麼呢?比如說增加操作,我們是不是要先查詢一下ArrayLis裏有沒有?再說更新和刪除,是不是都要查詢,既然是這樣,我們反覆的寫這部分代碼,顯然是不正確的,那我們就可以再寫一個查詢方法,讓其返回值爲Boolean,然後查到了就是ture否則爲false,然後再增刪改中調用這個Boolean方法就可以了,因爲這個比較簡單,所以不再附上代碼
3、但是這裏是有一個問題的,他只在當前工程運行下才保存我們所作的操作,如果我們重啓工程,那麼我們就沒有辦法看到這些信息,怎麼辦呢?可以用io流寫入文件中,也可以用jdbc寫入數據庫中,或者還有其他的操作……
下一篇博客我們來一起探討一下io流寫入文件中的操作