學生資訊管理小系統(以XML為儲存方式)
阿新 • • 發佈:2018-11-13
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
為了更好地應用XML,就寫了這個小專案。
下面是我的專案的目錄結構
專案思路
- dao是Date Access Object 資料訪問層,主要是負責操作資料
- domain是實體層,類似於bean層,放置專案用到的實體Student
- utils層是有關於XML操作的部分(一般實際開發中是資料庫操作部分)
- view層是檢視層(實際開發中是GUI層,與使用者直接打交道)
- Student.xml在這裡相當於我們的一個小小的資料庫
dao層設計
按照習慣的命名規則,我命名為StudentDao.java,具體實現的功能有新增學生資訊,查詢學生資訊,刪除學生資訊。這裡僅僅是直接對資料操作的模組,而把底層的操作XML文件的放到了utils中。這也在一定程度上實現了分層的思想,雖然這並不明顯,也並不必需!
package dao;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import utils.XMLUtils;import domain.Student;public class StudentDao { /** * 新增學生資訊模組 * @param student */ public void add(Student student) { try { Document document = XMLUtils.getDocument(); Element student_node = document.createElement("student"); student_node.setAttribute("examid", student.getExamid()); student_node.setAttribute("idcard", student.getIdcard()); Element name = document.createElement("name"); name.setTextContent(student.getName()); Element location = document.createElement("location"); location.setTextContent(student.getLocation()); Element grade = document.createElement("grade"); // 這裡是一個型別轉換的隱藏之處。不太明顯但是卻十分的重要 grade.setTextContent(student.getGrade() + ""); // 將新生成的三個子節點插入到student標籤內 student_node.appendChild(name); student_node.appendChild(location); student_node.appendChild(grade); // 對總的xml文件中新增一個學生資訊 document.getElementsByTagName("exam").item(0) .appendChild(student_node); //將記憶體中的操作物件寫回到xml檔案,真正實現對檔案的操作 XMLUtils.write2Xml(document); } catch (Exception e) { // TODO Auto-generated catch block throw new RuntimeException(e); } } public void delete(String name) { try { Document document = XMLUtils.getDocument(); NodeList name_node_list = document.getElementsByTagName("name"); for (int i = 0; i < name_node_list.getLength(); i++) { if (name_node_list.item(i).getTextContent().equals(name)) { Element person_node = (Element) name_node_list.item(i) .getParentNode(); Element exam_node = (Element) person_node.getParentNode(); exam_node.removeChild(person_node); //不要忘記將操作過的資料寫回,否則原資訊是不會發生變化的 XMLUtils.write2Xml(document); System.out.println("恭喜,學生資訊刪除成功!"); } } } catch (Exception e) { System.out.println("對不起,刪除操作未成功完成!請重試!"); throw new RuntimeException(e); } } /** * 給定學生的考號查詢該同學的詳細的資訊(不用姓名的原因是姓名具有不唯一性) * @param examid * @return */ public Student find(String examid) { Student student=null; try { Document document = XMLUtils.getDocument(); NodeList examid_node_list = document.getElementsByTagName("student"); //查詢准考證號與查詢值相一致的學生節點 for(int i=0; i<examid_node_list.getLength();i++){ Element examid_element = (Element) examid_node_list.item(i); if(examid_element.getAttribute("examid").equals(examid.toString().trim())){ //採用非遞迴的方式獲取student的詳細資訊 student = getStudentInfo(examid_element); return student; }else{ continue; } } } catch (Exception e) { e.printStackTrace(); System.out.println("對不起,未能正確的找到您要查詢的學生的姓名!請確認後重新嘗試!"); } return student; } /** * 給定一個節點,採用非遞迴的方式遍歷該學生節點的詳細的資訊 * 缺點:不能很好地複用程式碼,程式碼維護性較差 */ public Student getStudentInfo(Element node){ Student student = new Student(); if(node!=null){ String examid = node.getAttribute("examid"); String idcard = node.getAttribute("idcard"); NodeList node_list = node.getChildNodes(); //由於collection 的不確定性,是隨機取出的資料,導致bean中的資料不太對應 Node node_name = node_list.item(1); String name = node_name.getTextContent(); Node node_location = node_list.item(2); String location = node_location.getTextContent(); Node node_grade = node_list.item(0); String grade = node_grade.getTextContent()+"0.0"; //將獲取的資訊儲存到bean中,並作為返回值返回! student.setExamid(examid); student.setGrade(Double.parseDouble(grade)); student.setIdcard(idcard); student.setLocation(location); student.setName(name); return student; } System.out.println("this find operation is false!"); return null; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
domain–實體層(bean)
package domain;public class Student {//注意將Student具有的屬性設定為私有的成員,然後設定getter,setter來訪問 private String idcard; private String examid; private String name; private String location; private double grade; public String getIdcard() { return idcard; } public void setIdcard(String idcard) { this.idcard = idcard; } public String getExamid() { return examid; } public void setExamid(String examid) { this.examid = examid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public double getGrade() { return grade; } public void setGrade(double grade) { this.grade = grade; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
Utils層充當工具,關聯底層操作
package utils;import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;public class XMLUtils { //all the utils methods are static public static Document getDocument() throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/Student.xml")); return document; } public static void write2Xml(Document document) throws Exception{ TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer=factory.newTransformer(); //wrapper the two arguments DOMSource xmlSource = new DOMSource(document); StreamResult targetResult = new StreamResult(new File("src/Student.xml")); transformer.transform(xmlSource, targetResult);}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
小感悟:一般來說工具類的方法會做成靜態的私有的,這樣可以免去建立物件,又能很好的使用到它!
view層(與使用者直接互動的模組)
這裡僅僅是簡單的一些有好的互動,並沒有什麼複雜的地方
package view;import java.io.BufferedReader;import java.io.InputStreamReader;import dao.StudentDao;import domain.Student;public class Main { public static void main(String []args) throws Exception{ System.out.println("新增學生(a),查詢學生(f),刪除學生(d)"); System.out.println("請輸入操作型別:"); BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); String type = reader.readLine(); if(type.equalsIgnoreCase("a")){ //add student //name examid idcard location grade try{ System.out.println("請輸入學生姓名: "); String name = reader.readLine(); System.out.println("請輸入學生准考證號: "); String examid = reader.readLine(); System.out.println("請輸入學生身份證號: "); String idcard = reader.readLine(); System.out.println("請輸入學生所在地: "); String location = reader.readLine(); System.out.println("請輸入學生成績: "); String grade = reader.readLine(); Student student = new Student(); student.setExamid(examid); student.setIdcard(idcard); student.setLocation(location); student.setName(name); student.setGrade(Double.parseDouble(grade)); StudentDao studentDao = new StudentDao(); studentDao.add(student); System.out.println("恭喜:學生資訊新增成功!"); }catch(Exception e){ e.printStackTrace(); System.out.println("對不起,學生資訊資料新增失敗!"); } }else if(type.equalsIgnoreCase("f")){ //find student System.out.println("請輸入學生的准考證號"); StudentDao studentDao = new StudentDao(); String examid = reader.readLine(); Student student = studentDao.find(examid); PrintStudentInfo(student); }else if(type.equalsIgnoreCase("d")){ //delete student System.out.println("請輸入學生的姓名"); StudentDao studentDao = new StudentDao(); String name = reader.readLine(); studentDao.delete(name); }else{ System.out.println("Not Support!"); } } private static void PrintStudentInfo(Student student) { // TODO Auto-generated method stub if(student!=null){ System.out.println("學生考號: \t"+student.getExamid()); System.out.println("學生姓名: \t"+student.getName()); System.out.println("學生身份證號碼 : \t"+student.getIdcard()); System.out.println("學生所在地: \t"+student.getLocation()); System.out.println("學生成績: \t"+student.getGrade()); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
處理素材和結果
Student.xml檔案示意如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam> <student examid="222" idcard="111"> <name>Spring</name> <location>DaLian</location> <grade>89</grade> </student> <student examid="444" idcard="333"> <name>Summer</name> <location>ShangHai</location> <grade>97</grade> </student> <student examid="201492115" idcard="410728"> <name>郭璞</name> <location>大連</location> <grade>89.0</grade> </student> <student examid="123" idcard="1234567"> <name>未命名</name> <location>不知道</location> <grade>0.0</grade> </student></exam>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
操作結果:
新增學生操作
新增學生資訊結果
刪除學生操作
刪除學生結果
查詢學生詳細資訊
總結:
優點:
- 使用了分層思想(雖然有些地方並不是很明顯)
- 模組化的操作時的程式碼邏輯更加的清晰
- 較好的實現了對xml檔案的CRUD操作
結合了具體的專案,運用到了相關的知識點
缺點:程式碼處理上仍舊有很大的重複性
- 設計模式的運用不是很明顯
介面有點糟糕
有待改進之處:
使用GUI,rich使用者體驗
- 底層處理邏輯應更加的精簡