zoukankan      html  css  js  c++  java
  • JAVA初学练手项目,学生管理系统

    github地址:https://github.com/qscqesze/StudentManager

    简单描述一下:

    UI层面用于接受用户的处理信息,然后移交给StudentDao去处理数据。

    其中StudentDao通过修改Xml充当的数据库,来反馈数据。

    StudentDomain是用来定义数据类型,Studentutils是处理数据时候封装的工具类型。

    写了个额外的异常,Exception。

    我也是参考着教程写的:《30天轻松掌握JavaWeb视频》 方立勋,网易云课堂里面有这个课程。

    然后这个代码就结束了。

    具体代码如下:

    exam.xml //数据库文件

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
    	<student examid="222" idcard="111">
    		<name>张三</name>
    		<location>沈阳</location>
    		<grade>89</grade>
    	</student>
    	
    	<student examid="444" idcard="333">
    		<name>李四</name>
    		<location>大连</location>
    		<grade>97</grade>
    	</student>
    </exam>
    

    StudentDaoTest.java //测试文件

    package junit.test;
    
    import org.junit.Test;
    
    import en.itcast.dao.StudentDao;
    import en.itcast.domain.Student;
    import en.itcast.exception.StudentNotExistException;
    
    public class StudentDaoTest {
    
    	@Test
    	public void testAdd(){
    		StudentDao dao = new StudentDao();
    		Student s = new Student();
    		s.setExamid("121");
    		s.setGrade(89);
    		s.setIdcard("121");
    		s.setLocation("北京");
    		s.setName("aa");
    		dao.add(s);
    		
    	}
    	
    	@Test
    	public void testFind(){
    		StudentDao dao = new StudentDao();
    		dao.find("222");
    	}
    
    	@Test
    	public void testdelete() throws StudentNotExistException{
    		StudentDao dao = new StudentDao();
    		dao.delete("aa");
    	}
    }
    

    XmlUtils.java //工具类文件

    package en.itcast.utils;
    
    import java.io.FileOutputStream;
    
    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 {
    	// 工具类约定俗成为静态类
    
    	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);
    	}
    
    	// 编译异常就是垃圾
    	public static void write2Xml(Document document) throws Exception {
    		TransformerFactory factory = TransformerFactory.newInstance();
    		Transformer tf = factory.newTransformer();
    		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream(filename)));
    		
    	}
    }
    

    Main.java //主程序,ui界面的

    package en.itcast.UI;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import en.itcast.dao.StudentDao;
    import en.itcast.domain.Student;
    import en.itcast.exception.StudentNotExistException;
    
    public class Main {
    
    	public static void main(String[] args) {
    		System.out.println("添加用户(a) 删除用户(b) 查找用户(c)");
    		System.out.print("请输入操作类型:");
    
    		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    		try {
    			String type = bf.readLine();
    
    			if ("a".equals(type)) {
    				System.out.print("请输入学生姓名:");
    				String name = bf.readLine();
    
    				System.out.print("请输入学生准考证号:");
    				String examid = bf.readLine();
    
    				System.out.print("请输入学生所在地:");
    				String location = bf.readLine();
    
    				System.out.print("请输入学生身份证:");
    				String idcard = bf.readLine();
    
    				System.out.print("请输入学生成绩:");
    				String grade = bf.readLine();
    
    				Student s = new Student();
    				s.setExamid(examid);
    				s.setGrade(Double.parseDouble(grade));
    				s.setIdcard(idcard);
    				s.setLocation(location);
    				s.setName(name);
    
    				StudentDao dao = new StudentDao();
    				dao.add(s);
    
    				System.out.println("添加成功");
    			} else if ("b".equals(type)) {
    				System.out.print("请输入要删除的学生姓名:");
    				String name = bf.readLine();
    				try {
    					StudentDao dao = new StudentDao();
    					dao.delete(name);
    					System.out.println("删除成功.");
    				} catch (StudentNotExistException e) {
    					System.out.println("你删除的用户不存在.");
    				}
    
    			} else if ("c".equals(type)) {
    				System.out.print("请输入你要查询的准考证id:");
    				String examid = bf.readLine();
    				StudentDao dao = new StudentDao();
    				Student find_student = dao.find(examid);
    				if(find_student.equals(null)){
    					System.out.println("对不起,找不到该学生.");
    				}else{
    					System.out.println("该学生的姓名是:"+find_student.getName());
    				}
    			} else {
    				System.out.println("不支持你的操作");
    
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    			;
    			System.out.println("sorry");
    		}
    
    	}
    
    }
    

    StudentNotExistException.java //自定义异常,处理删除的时候不存在用

    package en.itcast.exception;
    
    public class StudentNotExistException extends Exception {
    
    	public StudentNotExistException() {
    		// TODO Auto-generated constructor stub
    	}
    
    	public StudentNotExistException(String message) {
    		super(message);
    		// TODO Auto-generated constructor stub
    	}
    
    	public StudentNotExistException(Throwable cause) {
    		super(cause);
    		// TODO Auto-generated constructor stub
    	}
    
    	public StudentNotExistException(String message, Throwable cause) {
    		super(message, cause);
    		// TODO Auto-generated constructor stub
    	}
    
    	public StudentNotExistException(String message, Throwable cause, boolean enableSuppression,
    			boolean writableStackTrace) {
    		super(message, cause, enableSuppression, writableStackTrace);
    		// TODO Auto-generated constructor stub
    	}
    	
    }
    

    Student.java //封装Student的数据类型

    package en.itcast.domain;
    
    public class Student {
    	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;
    	}
    	
    }
    

    StudentDao.java //程序处理

    package en.itcast.dao;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    
    import en.itcast.domain.Student;
    import en.itcast.exception.StudentNotExistException;
    import en.itcast.utils.XmlUtils;
    
    public class StudentDao {
    	public void add(Student s) {
    		try {
    			Document document = XmlUtils.getDocument();
    			// 创建出封装学生信息的标签
    			Element student_tag = document.createElement("student");
    			student_tag.setAttribute("idcard", s.getIdcard());
    			student_tag.setAttribute("examid", s.getExamid());
    
    			// 创建用于封装学生其他信息的标签
    			Element name = document.createElement("name");
    			Element location = document.createElement("location");
    			Element grade = document.createElement("grade");
    
    			name.setTextContent(s.getName());
    			location.setTextContent(s.getLocation());
    			grade.setTextContent(s.getGrade() + "");
    
    			student_tag.appendChild(name);
    			student_tag.appendChild(location);
    			student_tag.appendChild(grade);
    			// 把封装的信息挂在文档上
    			document.getElementsByTagName("exam").item(0).appendChild(student_tag);
    
    			XmlUtils.write2Xml(document);
    		} catch (Exception e) {
    			throw new RuntimeException(e);
    		}
    	}
    
    	public Student find(String examid) {
    		try {
    			Document document = XmlUtils.getDocument();
    			NodeList list = document.getElementsByTagName("student");
    			for (int i = 0; i < list.getLength(); i++) {
    				Element student_tag = (Element) list.item(i);
    				if (student_tag.getAttribute("examid").equals(examid)) {
    					// 找到匹配的学生了,new一个student对象
    					Student s = new Student();
    					s.setExamid(examid);
    					s.setIdcard(student_tag.getAttribute("idcard"));
    					s.setName(student_tag.getElementsByTagName("name").item(0).getTextContent());
    					s.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent());
    					s.setGrade(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent()));
    					return s;
    				}
    			}
    			return null;
    		} catch (Exception e) {
    			throw new RuntimeException(e);
    		}
    	}
    
    	public void delete(String name) throws StudentNotExistException {
    		try {
    			Document document = XmlUtils.getDocument();
    			NodeList list = document.getElementsByTagName("name");
    			for (int i = 0; i < list.getLength(); i++) {
    				if (list.item(i).getTextContent().equals(name)) {
    					list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode());
    					XmlUtils.write2Xml(document);
    					return;
    				}
    			}
    
    			throw new StudentNotExistException(name + "不存在");
    
    		} catch (StudentNotExistException e) {
    			throw e;
    		} catch (Exception e) {
    			throw new RuntimeException(e);
    		}
    
    	}
    }
  • 相关阅读:
    受约束的十人参赛问题
    受约束的10人参赛问题
    [转载]A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞
    使用SSH集成框架开发项目步骤
    Java语言高职组最后一题题
    受约束的十人参赛问题
    setsockopt的TCP层实现剖析
    周鸿祎:做产品体验先把自己切换到二傻子模式
    sysctl使用方法的变化
    产品设计核心要做到极致
  • 原文地址:https://www.cnblogs.com/qscqesze/p/6357491.html
Copyright © 2011-2022 走看看