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>
原文地址:http://blog.51cto.com/357712148/2103971