Java programming exercise: simulates User Logon using a layered structure based on XML files

Source: Internet
Author: User

The following XML file "user. xml" is used to store user logon information.

<?xml version="1.0" encoding="UTF-8"?><root><user id="u01" username="well" password="1234"/><user id="u02" username="lily" password="4321"/></root>

The hierarchical design concept is as follows:

Presentation Layer: LoginUI: input (): User LoginUI: output (): String

Business Layer: LoginBusiness: check (User): boolean

 

According to the above requirements, write the program:

(1) Compile the User class

package web.java.xml.exercise1;public class User {private String id;private String username;private String password;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;}}

 

(2) write the presentation layer LoginUI class, which is used to receive user input on the console and encapsulate it as a javaBean

package web.java.xml.exercise1;import java.util.Scanner;public class LoginUI {public User input(){User user = new User();Scanner scanner = new Scanner(System.in);System.out.print("username:");String username = scanner.nextLine();System.out.print("password:");String password = scanner.nextLine();user.setUsername(username);user.setPassword(password);return user;}public void output(boolean flag){if(flag){System.out.println("Login success!");} else {System.out.println("Login failed!");}}}

(3) write the LoginBusiness class at the business layer for User Login

package web.java.xml.exercise1;import java.io.File;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class LoginBusiness {public static void main(String[] args) throws Exception {LoginUI loginUI = new LoginUI();LoginBusiness loginBusiness = new LoginBusiness();User user = loginUI.input();boolean flag = loginBusiness.check(user);loginUI.output(flag);}public boolean check(User user) throws Exception {boolean flag = false;SAXReader saxReader = new SAXReader();File file = new File("src/web/java/xml/exercise1/users.xml");Document doc = saxReader.read(file);String xpath = "//user[@username='"+user.getUsername()+"' and @password='"+user.getPassword()+"']";Element ele = (Element) doc.selectSingleNode(xpath);if(ele!=null){flag = true;}return flag;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.