考试系统(未完成的小程序)
系统组成:
1、用于封装get和set(name,idcard,grade,examid,location)方法的test_system包
2、用于封装add,delect和find方法的deo包
3、用于封装用户不存在的自定义异常类exception包
4、用于封装测试各类方法的test测试包
5、用于封装和用户打交道的UI界面包
6、用于封装(getdocument方法和把更改操作更新到xml文档中的方法)的utils工具包
7、用于充当数据库的xml文件
总结:用deo包封装了增、删、查方法,其中在增删查方法里用到了utils包里document方法(获取标签等)以及更新(更改xml文件)方法,另外还会出现一些异常,所以自定义了一个异常包。
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>//建一个xml文档,充当数据库 2 <student examid="4356" idcard="1234"> 3 <name>aa</name> 4 <locationg>沈阳</locationg> 5 <grade>89</grade> 6 </student> 7 8 <student examid="3434" idcard="45656"> 9 <name>dfdf</name> 10 <location>dgerg</location> 11 <grade>67.0</grade> 12 13 </student> 14 <student examid="7890" idcard="123"> 15 <name>heeh</name> 16 <location>345</location> 17 <grade>44.0</grade> 18 19 </student> 20 21 </exam>
1 package test_system; 2 //封装get和set方法 3 public class student { 4 5 private String idcard; 6 private String examid; 7 private String name; 8 private String location; 9 private double grade; 10 public String getIdcard() 11 { 12 return idcard; 13 } 14 public void setIdcard(String idcard) 15 { 16 this.idcard=idcard; 17 } 18 public String getExamid() 19 { 20 return examid; 21 } 22 public void setExamid(String examid) 23 { 24 this.examid=examid; 25 } 26 public String getLocation() 27 { 28 return location; 29 } 30 public void setLocation(String location) 31 { 32 this.location=location; 33 } 34 public String getName() 35 { 36 return name; 37 } 38 public void setName(String name) 39 { 40 this.name=name; 41 } 42 public double getGrade() 43 { 44 return grade; 45 } 46 public void setGrade( double grade) 47 { 48 this.grade=grade; 49 } 50 }
1 package test_system.deo; 2 3 //具体封装add方法,delect方法和find方法 4 5 import java.rmi.StubNotFoundException; 6 7 import org.w3c.dom.Document; 8 import org.w3c.dom.Element; 9 import org.w3c.dom.NodeList; 10 11 import test_system.student; 12 import test_system.exception.StudentNotException; 13 import test_system.utils.xmlutils; 14 15 public class studentdeo { 16 17 public void add(student s) 18 { 19 try { 20 Document document= xmlutils.getDocument(); 21 //创建出封装学生信息的标签 22 Element student_tag=document.createElement("student"); 23 student_tag.setAttribute("idcard", s.getIdcard()); 24 student_tag.setAttribute("examid", s.getExamid()); 25 //创建用于封装学生姓名、成绩和所在地的标签 26 Element name=document.createElement("name"); 27 Element location=document.createElement("location"); 28 Element grade=document.createElement("grade"); 29 name.setTextContent(s.getName()); 30 location.setTextContent(s.getLocation()); 31 grade.setTextContent(s.getGrade()+""); 32 student_tag.appendChild(name); 33 student_tag.appendChild(location); 34 student_tag.appendChild(grade); 35 //把封装了学生信息的标签挂到文档上 36 document.getElementsByTagName("exam").item(0).appendChild(student_tag); 37 //更新内存 38 xmlutils.writetoxml(document); 39 40 41 } catch (Exception e) { 42 throw new RuntimeException(e);//此处的异常,不能直接抛给上一级,因为没有意义,又不能打印,因为只能转型成运行异常! 43 } 44 } 45 public student find(String examid) 46 { 47 try { 48 Document document=xmlutils.getDocument(); 49 NodeList list=document.getElementsByTagName("student"); 50 for(int i=0;i<list.getLength();i++) 51 { 52 Element stdent_tag=(Element) list.item(i); 53 if(stdent_tag.getAttribute("examid").equals(examid)) 54 { 55 student s=new student(); 56 s.setExamid(examid); 57 s.setIdcard(stdent_tag.getAttribute("idcard")); 58 s.setName(stdent_tag.getElementsByTagName("name").item(0).getTextContent()); 59 s.setLocation(stdent_tag.getElementsByTagName("location").item(0).getTextContent()); 60 s.setGrade(Double.parseDouble(stdent_tag.getElementsByTagName("grade").item(0).getTextContent())); 61 return s; 62 63 } 64 return null; 65 } 66 67 68 } catch (Exception e) { 69 throw new RuntimeException(); 70 } 71 return null; 72 } 73 public void delect(String name) throws StudentNotException 74 { 75 try { 76 Document document=xmlutils.getDocument(); 77 NodeList list=document.getElementsByTagName("name"); 78 for(int i=0;i<=list.getLength();i++) 79 { 80 if(list.item(i).getTextContent().equals(name)) 81 { 82 list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode()); 83 xmlutils.writetoxml(document); 84 return; 85 } 86 } 87 88 throw new StudentNotException(name+"不存在!!!"); 89 90 91 }catch(StudentNotException e){ 92 throw e; 93 } 94 95 96 catch (Exception e) { 97 throw new RuntimeException(e); 98 } 99 100 } 101 102 103 104 }
1 package test_system.UI; 2 //和用户打交道的界面 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 7 import test_system.student; 8 import test_system.deo.studentdeo; 9 import test_system.exception.StudentNotException; 10 11 public class main { 12 13 public static void main(String[] args) { 14 15 try { 16 17 System.out.println("添加学生(a) 删除学生(b) 查找学生(c)"); 18 System.out.print("请输入操作类型:"); 19 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 20 String type=br.readLine(); 21 if("a".equals(type)) 22 { 23 System.out.print("请输入学生姓名:"); 24 String name=br.readLine(); 25 System.out.print("请输入学生准考证号:"); 26 String examid=br.readLine(); 27 System.out.print("请输入学生身份证号:"); 28 String idcard=br.readLine(); 29 System.out.print("请输入学生所在地:"); 30 String location=br.readLine(); 31 System.out.print("请输入学生成绩:"); 32 String grade=br.readLine(); 33 student s=new student(); 34 s.setExamid(examid); 35 s.setGrade(Double.parseDouble(grade)); 36 s.setIdcard(idcard); 37 s.setLocation(location); 38 s.setName(name); 39 studentdeo deo=new studentdeo(); 40 deo.add(s); 41 System.out.println("添加成功!!!"); 42 } 43 else if("b".equals(type)) 44 { 45 System.out.println("请输入要删除学生的姓名"); 46 String name=br.readLine(); 47 try { 48 49 studentdeo deo=new studentdeo(); 50 deo.delect(name); 51 System.out.println("删除成功!"); 52 } catch (StudentNotException e) { 53 System.out.println("你要删除的用户不存在!"); 54 } 55 56 57 } 58 else if("c".equals(type)) 59 { 60 System.out.println("请输入要查找学生的考号:"); 61 String examid=br.readLine(); 62 63 64 studentdeo deo=new studentdeo(); 65 student a=deo.find(examid); 66 System.out.print(a.getGrade());//这个地方该如何打印出学生的成绩 67 68 69 70 } 71 else 72 { 73 System.out.print("请输入正确的指令"); 74 } 75 } catch (IOException e) { 76 77 e.printStackTrace(); 78 System.out.print("运行出错!!"); 79 } 80 } 81 82 }
package test_system.utils; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; 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 { private static String filename="src/exam.xml"; public static Document getDocument() throws Exception { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); return builder.parse(filename); } //封装把修稿的内容更新到xml文档中的方法 public static void writetoxml(Document document) throws Exception { TransformerFactory factory=TransformerFactory.newInstance(); Transformer tf=factory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename))); } }
1 package test_system.exception; 2 3 //自定义的一个表示学生不存在的异常类 4 public class StudentNotException extends Exception { 5 6 public StudentNotException() { 7 // TODO Auto-generated constructor stub 8 } 9 10 public StudentNotException(String message) { 11 super(message); 12 // TODO Auto-generated constructor stub 13 } 14 15 public StudentNotException(Throwable cause) { 16 super(cause); 17 // TODO Auto-generated constructor stub 18 } 19 20 public StudentNotException(String message, Throwable cause) { 21 super(message, cause); 22 // TODO Auto-generated constructor stub 23 } 24 25 public StudentNotException(String message, Throwable cause, 26 boolean enableSuppression, boolean writableStackTrace) { 27 super(message, cause, enableSuppression, writableStackTrace); 28 // TODO Auto-generated constructor stub 29 } 30 31 }
1 package test_system.test; 2 3 import org.junit.Test; 4 5 import test_system.student; 6 import test_system.deo.studentdeo; 7 import test_system.exception.StudentNotException; 8 9 //测试add方法、delect方法和find方法的测试类 10 11 public class student_test { 12 13 @Test 14 public void studenttest() { 15 student a=new student(); 16 studentdeo deo=new studentdeo(); 17 a.setExamid("1212"); 18 a.setGrade(98); 19 a.setIdcard("2324355"); 20 a.setLocation("山东"); 21 a.setName("山玩意"); 22 deo.add(a); 23 24 } 25 @Test 26 public void studenttest1() { 27 28 studentdeo deo=new studentdeo(); 29 deo.find("1212"); 30 31 32 } 33 @Test 34 public void studenttest2() throws StudentNotException { 35 36 studentdeo deo=new studentdeo(); 37 deo.delect("aa"); 38 39 40 } 41 42 43 44 45 46 }