实体类
public class User { private String id; private String username; private String password; public User(){} public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
表现层:
//输入输出public class LoginUI { public User input() { User user = new User(); Scanner scanner = new Scanner(System.in); System.out.print("用户名:"); String username = scanner.nextLine(); System.out.print("密码:"); String password = scanner.nextLine(); user.setUsername(username); user.setPassword(password); return user; } public void output(boolean flag) { if(flag) { System.out.println("登陆成功"); }else { System.out.println("登陆失败"); } }}
业务层:
import java.io.File;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;import org.junit.Test;public class LoginBusiness { @Test public void logingtet() throws Exception { LoginUI loginUI = new LoginUI(); LoginBusiness lognBusiness = new LoginBusiness(); User user = loginUI.input(); boolean flag = lognBusiness.check(user); loginUI.output(flag); } //判断 public boolean check(User user) throws Exception { boolean flag = false; SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File("src/day2/domx/users.xml")); String xpath = "//user[@username='"+user.getUsername()+"' and @password='"+user.getPassword()+"']"; Element element = (Element) document.selectSingleNode(xpath); if(element !=null) { flag = true; } return flag; }}
文件
user.xml
<?xml version="1.0" encoding="UTF-8"?>
-<root>
<user password="123456" username="liwen" id="id001"/>
<user password="23456" username="python" id="id002"/>
</root>
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦