學生信息管理小系統(以XML為存儲方式)
阿新 • • 發佈:2019-01-26
分享 target win ava sta 體驗 one generate art
為了更好地應用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用戶體驗
- 底層處理邏輯應更加的精簡
再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow
學生信息管理小系統(以XML為存儲方式)