考试系统(未完成的小程序)
生活随笔
收集整理的這篇文章主要介紹了
考试系统(未完成的小程序)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??
系統組成:
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 }?
轉載于:https://www.cnblogs.com/jjlovemm/p/4264694.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的考试系统(未完成的小程序)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript改变样式(cssFl
- 下一篇: 工厂模式一之简单工厂