1. 程式人生 > >java 登陸分層結構設計思想

java 登陸分層結構設計思想

java 登陸分層結構設計思想

實體類

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>

java 登陸分層結構設計思想