SHS 5 år sedan
förälder
incheckning
bbebfa9041

+ 41 - 0
src/main/java/info/edu/BaseMain.java

@@ -1,4 +1,45 @@
 package info.edu;
 
+import info.edu.model.User;
+
+import java.text.SimpleDateFormat;
+import java.util.*;
+
 public class BaseMain {
+
+    private static User loginUser= new User();
+
+    public void  setToken(Map map){
+        User user = new User();
+        user.setId(map.get("id").toString());
+        user.setPwd(map.get("pwd").toString());
+        user.setName(map.get("name").toString());
+        user.setAccount(map.get("account").toString());
+        user.setCreated(map.get("created").toString());
+        loginUser = user;
+    }
+
+    public User getUser(String key){
+        return loginUser;
+    }
+
+    public void clearUser(){
+        loginUser = new User();
+    }
+
+    public List<User> getAllUser(List<Map> maps) {
+        List<User> users = new ArrayList<>();
+        for (Map map : maps) {
+            User user = new User();
+            user.setId(map.get("id").toString());
+            user.setPwd(map.get("pwd").toString());
+            user.setName(map.get("name").toString());
+            user.setAccount(map.get("account").toString());
+            Date date = new Date(Long.valueOf(map.get("created").toString()));
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+            user.setCreated(sdf.format(date));
+            users.add(user);
+        }
+        return users;
+    }
 }

+ 15 - 4
src/main/java/info/edu/Main.java

@@ -1,13 +1,24 @@
 package info.edu;
 
+import info.edu.operation.UserOperation;
+
+import javax.swing.*;
+import java.awt.*;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
 /**
  * Hello world!
  *
  */
-public class Main extends BaseMain
+public class Main
 {
-    public static void main( String[] args )
-    {
-        System.out.println( "Hello World!" );
+
+    public static void main( String[] args ) {
+        //调用用户操作
+        final UserOperation userOperation = new UserOperation();
+        userOperation.home();
     }
+
 }
+

+ 7 - 6
src/main/java/info/edu/model/User.java

@@ -1,18 +1,19 @@
 package info.edu.model;
 
+
 public class User {
 
-    private Long id;
+    private String id;
     private String account;
     private String pwd;
     private String name;
-    private Long created;
+    private String created;
 
-    public Long getId() {
+    public String getId() {
         return id;
     }
 
-    public void setId(Long id) {
+    public void setId(String id) {
         this.id = id;
     }
 
@@ -40,11 +41,11 @@ public class User {
         this.name = name;
     }
 
-    public Long getCreated() {
+    public String getCreated() {
         return created;
     }
 
-    public void setCreated(Long created) {
+    public void setCreated(String created) {
         this.created = created;
     }
 }

+ 149 - 0
src/main/java/info/edu/operation/HomeOperation.java

@@ -0,0 +1,149 @@
+package info.edu.operation;
+
+import info.edu.BaseMain;
+import info.edu.model.User;
+import info.edu.service.UserService;
+import info.edu.utils.Regular;
+
+import javax.annotation.processing.SupportedSourceVersion;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.List;
+
+public class HomeOperation extends BaseMain {
+
+    private UserService userService = new UserService();
+
+
+    public void home(){
+        System.out.println("\033[33;4m" + "Welcome to use SuperManager Login Success" + "\033[0m"+"\n\n");
+        System.out.println("\033[34;4m" + "1,与我相关" + "\033[0m");
+        System.out.println("\033[35;4m" + "2,商品管理" + "\033[0m");
+        System.out.println("\033[37;4m" + "3,退出系统" + "\033[0m");
+
+        System.out.print("\033[38;4m" + "请输入要进行的操作:" + "\033[0m");
+        operation();
+    }
+
+    public static void main(String[] args){
+        HomeOperation homeOperation = new HomeOperation();
+        homeOperation.userlist();
+    }
+
+    public void userlist(){
+        System.out.print("\033[34;4m" + "用户Id" + "\033[0m"+"\t\t\t\t\t\t\t\t");
+        System.out.print("\033[35;4m" + "用户账号" + "\033[0m"+"\t\t\t\t\t");
+        System.out.print("\033[36;4m" + "用户名称" + "\033[0m"+"\t\t\t\t\t");
+        System.out.print("\033[37;4m" + "创建时间" + "\033[0m"+"\n");
+        List<User> users = userService.getAllUser();
+        if(users!=null){
+            for (User user : users) {
+                System.out.print("\033[34;4m" + user.getId() + "\033[0m"+"\t\t\t\t\t");
+                System.out.print("\033[35;4m" + user.getAccount() + "\033[0m"+"\t\t\t\t\t");
+                System.out.print("\033[36;4m" + user.getName()+ "\033[0m"+"\t\t\t\t\t");
+                System.out.print("\033[37;4m" + user.getCreated() + "\033[0m"+"\n");
+            }
+        }
+        System.out.println("\n"+"\033[34;4m" + "1,返回主页" + "\033[0m");
+        System.out.println("\033[35;4m" + "2,删除用户" + "\033[0m");
+        System.out.println("\033[38;4m" + "请输选择操作:" + "\033[0m");
+        operationUser();
+    }
+
+    public void operationUser(){
+        String operation = getInput();
+        switch (operation){
+            case "1":
+                home();
+                break;
+            case "2":
+                System.out.println("\033[38;4m" + "请输入要删除的Id:" + "\033[0m");
+                delUser();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "操作非法!请输选择正确的操作:" + "\033[0m");
+                operationUser();
+                break;
+        }
+    }
+
+    public void delUser() {
+        String id = getInput();
+        Integer del = userService.del(id);
+        switch (del){
+            case 0:
+                System.out.println("\033[38;4m" + "删除成功!请选择操作:" + "\033[0m");
+                delOpertion();
+                break;
+            case 1:
+                System.out.println("\033[38;4m" + "删除用户不存在!请输选择操作:" + "\033[0m");
+                delOpertion();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "操作非法!请输选择正确的操作:" + "\033[0m");
+                delUser();
+                break;
+        }
+    }
+
+    public void delOpertion(){
+        System.out.println("\033[35;4m" + "1,重新输入" + "\033[0m");
+        System.out.println("\033[35;4m" + "2,返回列表" + "\033[0m");
+        System.out.println("\033[35;4m" + "3,返回主页" + "\033[0m");
+        System.out.println("\033[38;4m" + "请输选择操作:" + "\033[0m");
+        operationDel();
+    }
+
+    public void operationDel(){
+        String operation = getInput();
+        switch (operation){
+            case "1":
+                delUser();
+                break;
+            case "2":
+                userlist();
+                break;
+            case "3":
+                home();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "操作非法!请输选择正确的操作:" + "\033[0m");
+                operationDel();
+                break;
+        }
+    }
+
+    public void operation(){
+        String operation = getInput();
+        switch (operation){
+            case "1":
+                userlist();
+                break;
+            case "2":
+                System.out.println("\033[38;4m" + "该功能暂未完善!请输选择其他操作:" + "\033[0m");
+                operation();
+                break;
+            case "3":
+                System.out.println("\033[33;4m" + "System Closed!" + "\033[0m"+"\n\n");
+                Thread.interrupted();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "操作非法!请输选择正确的操作:" + "\033[0m");
+                operation();
+                break;
+        }
+    }
+
+    public static String getInput(){
+        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+        try {
+            String perNumber = br.readLine();
+            return perNumber;
+        }catch (Exception e){
+            System.out.print("\033[38;4m" + "Input Error ! System Closed" + "\033[0m");
+            return null;
+        }
+    }
+
+
+}

+ 247 - 0
src/main/java/info/edu/operation/UserOperation.java

@@ -0,0 +1,247 @@
+package info.edu.operation;
+
+import info.edu.BaseMain;
+import info.edu.service.UserService;
+import info.edu.utils.Regular;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+public class UserOperation extends BaseMain {
+
+    private UserService userService = new UserService();
+
+    private HomeOperation homeOperation = new HomeOperation();
+
+    public void home(){
+        System.out.println("\033[33;4m" + "Welcome to use SuperManager" + "\033[0m"+"\n\n");
+        System.out.println("\033[34;4m" + "1,用户登陆" + "\033[0m");
+        System.out.println("\033[35;4m" + "2,用户注册" + "\033[0m");
+        System.out.println("\033[36;4m" + "3,密码找回" + "\033[0m");
+        System.out.println("\033[37;4m" + "4,退出系统" + "\033[0m");
+
+        System.out.print("\033[38;4m" + "请输入要进行的操作:" + "\033[0m");
+        operation();
+
+    }
+
+    public void operation(){
+        String operation = getInput();
+        System.out.println(operation);
+        switch (operation){
+            case "1":
+                userLogin();
+                break;
+            case "2":
+                //执行注册
+                userRegist();
+                break;
+            case "3":
+                updatePwd();
+                break;
+            case "4":
+                System.out.println("\033[33;4m" + "System Closed!" + "\033[0m"+"\n\n");
+                Thread.interrupted();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "操作非法!请输选择正确的操作:" + "\033[0m");
+                operation();
+                break;
+        }
+    }
+
+    public void updatePwd(){
+        System.out.println("\033[33;4m" + "SuperManager User Update Password" + "\033[0m"+"\n\n");
+        System.out.print("\033[34;4m" + "请输入账号:" + "\033[0m");
+        String account = getUserAccount();
+        isUser(account);
+    }
+
+    public void updatePwd(String account){
+        System.out.println("\033[38;4m" + "请输入要修改的密码:" + "\033[0m");
+        String pwd = getPasswd();
+        Integer update = userService.updatePwd(account,pwd);
+        switch (update){
+            case 0:
+                System.out.println("\033[38;4m" + "密码修改成功!你可以进行以下操作:" + "\033[0m");
+                userOperationSuccess();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "登陆错误!你可以进行以下操作:" + "\033[0m");
+                userOperationSuccess();
+                break;
+        }
+    }
+
+    public void isUser(String account){
+        Integer isUser = userService.getUser(account);
+        switch (isUser){
+            case 0:
+                updatePwd(account);
+                break;
+            case 1:
+                System.out.println("\033[38;4m" + "该账号不存在你可以进行如下操作:" + "\033[0m");
+                userOperationRgist();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "登陆错误!你可以进行以下操作:" + "\033[0m");
+                userOperationSuccess();
+                break;
+        }
+    }
+
+    public void userLogin(){
+        System.out.println("\033[33;4m" + "SuperManager User Login" + "\033[0m"+"\n\n");
+        System.out.print("\033[34;4m" + "请输入账号:" + "\033[0m");
+        String account = getUserAccount();
+        System.out.print("\033[34;4m" + "请输入密码:" + "\033[0m");
+        String pwd = getPasswd();
+        Integer retCode = userService.login(account,pwd);
+        switch (retCode){
+            case 0:
+                System.out.println("\033[38;4m" + "登陆成功:" + "\033[0m");
+                homeOperation.home();
+                break;
+            case 1:
+                System.out.println("\033[38;4m" + "该账号未注册" + "\033[0m");
+                userOperationRgist();
+                break;
+            case 2:
+                System.out.println("\033[38;4m" + "账号或密码错误" + "\033[0m");
+                userOperationSuccess();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "登陆错误!你可以进行以下操作:" + "\033[0m");
+                userOperationSuccess();
+                break;
+        }
+    }
+
+    public void userRegist(){
+        clearConsole();
+        System.out.println("\033[33;4m" + "SuperManager User Regist" + "\033[0m"+"\n\n");
+        System.out.print("\033[34;4m" + "请输入用户名 :" + "\033[0m");
+        String name = getInput();
+        System.out.print("\033[34;4m" + "请输入用户账号(账号要求纯数字):" + "\033[0m");
+        String account = getUserAccount();
+        System.out.print("\033[34;4m" + "请输入用户密码:" + "\033[0m");
+        String pwd = getPasswd();
+        Integer retCode = userService.save(account,pwd,name);
+        switch (retCode){
+            case 0:
+                System.out.println("\033[38;4m" + "该账号已经注册!你可以进行以下操作:" + "\033[0m");
+                userOperationSuccess();
+                break;
+            case 1:
+                System.out.println("\033[38;4m" + "账号注册成功!你可以进行以下操作:" + "\033[0m");
+                userOperationSuccess();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "账号注册失败!你可以进行以下操作:" + "\033[0m");
+                userOperationSuccess();
+                break;
+        }
+    }
+
+    public void userOperationRgist(){
+        System.out.println("\033[34;4m" + "1,返回首页" + "\033[0m");
+        System.out.println("\033[35;4m" + "2,注册账号" + "\033[0m");
+        operationRegist();
+    }
+
+    public void operationRegist() {
+        String operation = getInput();
+        System.out.println(operation);
+        switch (operation) {
+            case "1":
+                home();
+                break;
+            case "2":
+                userRegist();
+                break;
+            default:
+                System.out.println("\033[38;4m" + "操作非法!请输选择正确的操作:" + "\033[0m");
+                operationSuccess();
+                break;
+        }
+    }
+
+    public void userOperationSuccess(){
+        System.out.println("\033[34;4m" + "1,返回首页" + "\033[0m");
+        System.out.println("\033[35;4m" + "2,跳转登陆" + "\033[0m");
+        operationSuccess();
+    }
+    public void operationSuccess(){
+        String operation = getInput();
+        System.out.println(operation);
+        switch (operation){
+            case "1":
+                home();
+                break;
+            case "2":
+                userLogin();
+                break;
+            default:
+                System.out.print("\033[38;4m" + "操作非法!请输选择正确的操作:" + "\033[0m");
+                operationSuccess();
+                break;
+        }
+    }
+
+    public String getPasswd(){
+        String userPwd = getInput();
+        if(Regular.passwdCheck(userPwd)){
+            return userPwd;
+        }else {
+            System.out.print("\033[34;4m" + "非法输入(密码只能输入6-20个字母、数字、下划线)请重新输入:" + "\033[0m");
+            getPasswd();
+            return null;
+        }
+
+    }
+
+    public String getUserAccount(){
+        String userAccount = getInput();
+        if(Regular.isNumericzidai(userAccount)){
+            return userAccount;
+        }else {
+            System.out.print("\033[34;4m" + "非法输入(账号要求纯数字)请重新输入:" + "\033[0m");
+            getUserAccount();
+            return null;
+        }
+    }
+
+    public static String getInput(){
+        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+        try {
+            String perNumber = br.readLine();
+            return perNumber;
+        }catch (Exception e){
+            System.out.print("\033[38;4m" + "Input Error ! System Closed" + "\033[0m");
+            return null;
+        }
+    }
+
+
+    // 清空控制台
+    private static void clearConsole()
+    {
+        try
+        {
+            String os = System.getProperty("os.name");
+
+            if (os.contains("Windows"))
+            {
+                Runtime.getRuntime().exec("cls");
+            }
+            else
+            {
+                Runtime.getRuntime().exec("clear");
+            }
+        }
+        catch (Exception exception)
+        {
+            //  Handle exception.
+        }
+    }
+}

+ 104 - 12
src/main/java/info/edu/service/UserService.java

@@ -1,27 +1,119 @@
 package info.edu.service;
 
+import info.edu.BaseMain;
 import info.edu.model.Item;
 import info.edu.model.User;
 import info.edu.utils.IdGen;
+import info.edu.utils.MD5Utils;
 import info.edu.utils.XmlUtils;
+import sun.security.provider.MD5;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 public class UserService extends XmlUtils<User> {
 
-    public static void main(String[] args) throws Exception{
-        XmlUtils xmlService = new XmlUtils();
-        List<Item> users = new ArrayList<>();
-        Item user = new Item();
-        user.setId(IdGen.getId());
-//        user.setAccount("admin");
-        user.setName("ceshiss");
-//        user.setPwd("123456");
-//        user.setCreated(System.currentTimeMillis());
-        users.add(user);
-//        xmlService.writeInnerXml("user.xml",users);
-        System.out.println(xmlService.readInnerXmlByAccount("user.xml","admin"));
+    private BaseMain baseMain = new BaseMain();
 
+    public int save(String account,String pwd,String name){
+        try{
+            User user = new User();
+            user.setId(IdGen.getId());
+            user.setAccount(account);
+            user.setName(name);
+            user.setPwd(MD5Utils.pwdRule(pwd));
+            user.setCreated(System.currentTimeMillis()+"");
+            if (this.readInnerXmlByAccount(XMLFILES.USER.getPath(),account) != null){
+                return  0;
+            }
+            this.writeInnerXml(XMLFILES.USER.getPath(),user);
+            return 1;
+        }catch (Exception e){
+            System.out.println("System Error!");
+            return -1;
+        }
+
+    }
+
+
+    public int login(String account,String pwd){
+        try{
+            Map user = this.readInnerXmlByAccount(XMLFILES.USER.getPath(),account);
+            if (user == null){
+                return  1;
+            }
+            if(MD5Utils.pwdValidate(pwd,user.get("pwd").toString())){
+                //写入BaseMain 方面操作端operation 全局调用
+               baseMain.setToken(user);
+                return 0;
+            }else {
+                return 2;
+            }
+        }catch (Exception e){
+            System.out.println("System Error!");
+            return -1;
+        }
+
+    }
+
+    public int getUser(String account){
+        try{
+            Map user = this.readInnerXmlByAccount(XMLFILES.USER.getPath(),account);
+            if (user == null){
+                return  1;
+            }else {
+                return 0;
+            }
+        }catch (Exception e){
+            System.out.println("System Error!");
+            return -1;
+        }
+    }
+
+    public int updatePwd(String account,String pwd){
+        try{
+            Map user = this.readInnerXmlByAccount(XMLFILES.USER.getPath(),account);
+            User user2 = new User();
+            user2.setPwd(MD5Utils.pwdRule(pwd));
+            this.UpdateXml(XMLFILES.USER.getPath(),user.get("id").toString(),user2);
+            return 0;
+        }catch (Exception e){
+            System.out.println("System Error!");
+            return -1;
+        }
+    }
+
+    public void update(String id ,String account,String pwd,String name) throws Exception{
+        User user = new User();
+        user.setId(id);
+        user.setAccount(account);
+        user.setName(name);
+        user.setPwd(MD5Utils.pwdRule(pwd));
+        user.setCreated(System.currentTimeMillis()+"");
+        if (this.readInnerXmlById(XMLFILES.USER.getPath(),id.toString()) == null){
+            System.out.println("制定修改账号不存在!");
+            return;
+        }
+        this.writeInnerXml(XMLFILES.USER.getPath(),user);
+        System.out.println("用户信息修改成功!");
+    }
+
+    public List<User> getAllUser() {
+        try{
+            return baseMain.getAllUser(this.readInnerXml(XMLFILES.USER.getPath()));
+        }catch (Exception e){
+            System.out.println("System Error!");
+            return null;
+        }
+    }
+
+
+    public int del(String id) {
+        try {
+            return this.DeleteXml(id,XMLFILES.USER.getPath());
+        }catch (Exception e){
+            return -1;
+        }
     }
 }

+ 2 - 2
src/main/java/info/edu/utils/IdGen.java

@@ -76,8 +76,8 @@ public class IdGen
         return System.currentTimeMillis();
     }
 
-    public static long getId() {
-        return getInstance().Id();
+    public static String getId() {
+        return getInstance().Id()+"";
     }
 
     private static class IdGenHolder

+ 42 - 0
src/main/java/info/edu/utils/MD5Utils.java

@@ -0,0 +1,42 @@
+package info.edu.utils;
+
+import java.security.MessageDigest;
+import java.util.Random;
+
+public class MD5Utils
+{
+    
+    public static final String pwdRule(final String pwd) {
+        final String rule = pwd;
+        return md5(rule);
+    }
+
+
+
+    public static final String md5(final String s) {
+        final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+        try {
+            final byte[] btInput = s.getBytes();
+            final MessageDigest mdInst = MessageDigest.getInstance("MD5");
+            mdInst.update(btInput);
+            final byte[] md = mdInst.digest();
+            final int j = md.length;
+            final char[] str = new char[j * 2];
+            int k = 0;
+            for (final byte byte0 : md) {
+                str[k++] = hexDigits[byte0 >>> 4 & 0xF];
+                str[k++] = hexDigits[byte0 & 0xF];
+            }
+            return new String(str).toLowerCase();
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    
+    public static final boolean pwdValidate(final String pwd, final String dpwd) {
+        return pwdRule(pwd).equals(dpwd);
+    }
+}

+ 30 - 0
src/main/java/info/edu/utils/Regular.java

@@ -0,0 +1,30 @@
+package info.edu.utils;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class Regular {
+
+    /*
+    * 检查是否为数字  // 正则表达式
+    * java regular
+    * */
+    public static boolean isNumericzidai(String str) {
+        Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
+        Matcher isNum = pattern.matcher(str);
+        if (!isNum.matches()) {
+            return false;
+        }
+        return true;
+    }
+
+    //校验密码:只能输入6-20个字母、数字、下划线
+    public static boolean passwdCheck(String str) {
+        Pattern pattern = Pattern.compile("^(\\w){6,20}$");
+        Matcher check = pattern.matcher(str);
+        if (!check.matches()) {
+            return false;
+        }
+        return true;
+    }
+}

+ 65 - 120
src/main/java/info/edu/utils/XmlUtils.java

@@ -24,7 +24,7 @@ public class XmlUtils<T> {
     /*
      * 查询全部xml数据
      * */
-    public List<T> readInnerXml(String path) throws Exception{
+    public List<Map> readInnerXml(String path) throws Exception{
         File dir = new File(defaultPath+path);
         if (!dir.exists()) {
             dir.createNewFile();
@@ -36,7 +36,7 @@ public class XmlUtils<T> {
         String dirPath = dir+"";
         Document dom = UtilsForXML.getDocument(dirPath);
         Element root = dom.getRootElement();
-        List<T> tables = new ArrayList<>();
+        List<Map> tables = new ArrayList<>();
         List list = root.elements();
         //处理root节点数据
         for (int i = 0; i < list.size(); i++) {
@@ -47,7 +47,7 @@ public class XmlUtils<T> {
                 Element type = (Element) iterator.next();
                 res.put(type.getName(),type.getStringValue());
             }
-            tables.add((T)res);
+            tables.add(res);
         }
         return tables;
     }
@@ -55,26 +55,27 @@ public class XmlUtils<T> {
     /*
      * 根据item的属性id查询xml
      * */
-    public T readInnerXmlById(String path,Long id) throws Exception{
+    public T readInnerXmlById(String path,String id) throws Exception{
         File dir = new File(defaultPath+path);
         String dirPath = dir+"";
         Document dom = UtilsForXML.getDocument(dirPath);
         Element root = dom.getRootElement();
-        Element beQuery = (Element)root.selectSingleNode("//item[@id="+id.toString()+"]");
+        Element beQuery = (Element)root.selectSingleNode("//item[@account="+id+"]");
         Map<String,Object> res = new HashMap<>();
         if(beQuery!=null){
             for (Iterator iterator = beQuery.elementIterator(); iterator.hasNext();) {
                 Element type = (Element) iterator.next();
                 res.put(type.getName(),type.getStringValue());
             }
+            return (T)res;
         }
-        return (T)res;
+        return null;
     }
 
     /*
-     * 根据item的属性id查询xml
+     * 根据item的属性account查询xml
      * */
-    public T readInnerXmlByAccount(String path,String account) throws Exception{
+    public Map readInnerXmlByAccount(String path,String account) throws Exception{
         File dir = new File(defaultPath+path);
         String dirPath = dir+"";
         Document dom = UtilsForXML.getDocument(dirPath);
@@ -86,156 +87,100 @@ public class XmlUtils<T> {
                 Element type = (Element) iterator.next();
                 res.put(type.getName(),type.getStringValue());
             }
+            return res;
         }
-        return (T)res;
+        return null;
     }
 
     /*
      * 增加xml数据
      * */
-    public int writeInnerXml(String path,List<T> lists) throws Exception{
+    public int writeInnerXml(String path,T list) throws Exception{
         File dir = new File(defaultPath+path);
         if (!dir.exists()) {
             dir.createNewFile();
+            Document dom = DocumentHelper.createDocument();
+            Element root = dom.addElement("table");
+            String dirpath = dir+"";
+            UtilsForXML.writeToXML(dom, dirpath);
         }
         int id = 1;
         String dirPath = dir+"";
         Document dom = UtilsForXML.getDocument(dirPath);
         Element root = dom.getRootElement();
-        for (T list : lists) {
-            Field[] fields = list.getClass().getDeclaredFields();
-            Element newPerson = root.addElement("item");
-            for(int i=0; i<fields.length; i++){
-                Field f = fields[i];
-                f.setAccessible(true);
-                if(f.getName() == "id"){
-                    newPerson.addAttribute("id", f.get(list).toString());
-                }
-                if(f.getName() == "account"){
-                    newPerson.addAttribute("account", f.get(list).toString());
-                }
-                Element element = newPerson.addElement(f.getName());
-                element.setText(f.get(list).toString());
+        Field[] fields = list.getClass().getDeclaredFields();
+        Element newPerson = root.addElement("item");
+        for(int i=0; i<fields.length; i++){
+            Field f = fields[i];
+            f.setAccessible(true);
+            if(f.getName() == "id" ){
+                newPerson.addAttribute("id", f.get(list).toString());
+            }
+            if(f.getName() == "account"){
+                newPerson.addAttribute("account", f.get(list).toString());
             }
+            Element element = newPerson.addElement(f.getName());
+            element.setText(f.get(list).toString());
         }
         UtilsForXML.writeToXML(dom, dirPath);
         return id;
     }
 
     /*
-     * 根据person属性id修改xml数据
+     * 根据item属性id修改xml数据
      * */
-    public static int UpdateClinetLoginUser(int id,String appDir,String publisherName,String serverUrl,String userName,String passWord) throws Exception{
-        File dir = new File(appDir+"\\persons.xml");
+    public int UpdateXml(String path,String id ,T t) throws Exception{
+        File dir = new File(defaultPath+path);
         String dirPath = dir+"";
         Document dom = UtilsForXML.getDocument(dirPath);
         Element root = dom.getRootElement();
-        Element beQuery = (Element)root.selectSingleNode("//person[@id="+id+"]");
-        beQuery.element("publishername").setText(publisherName);
-        beQuery.element("serverurl").setText(serverUrl);
-        beQuery.element("username").setText(userName);
-        beQuery.element("password").setText(passWord);
-        UtilsForXML.writeToXML(dom, dirPath);
-        return id;
+        Element beQuery = (Element)root.selectSingleNode("//item[@id="+id+"]");
+        Field[] fields = t.getClass().getDeclaredFields();
+        for(int i=0; i<fields.length; i++){
+            Field f = fields[i];
+            f.setAccessible(true);
+            try {
+                beQuery.element(f.getName()).setText(f.get(t).toString());
+            }catch (Exception s){
+
+            }
+        }
+        UtilsForXML.writeToXML(dom,dirPath);
+        return 1;
     }
+
     /*
-     * 根据person属性id删除xml数据
+     * 根据item属性id删除xml数据
      * */
-    public static int DeleteClinetLoginUser(int id,String appDir) throws Exception{
-        File dir = new File(appDir+"\\persons.xml");
+    public int DeleteXml(String id,String path) throws Exception{
+        File dir = new File(defaultPath+path);
         String dirPath = dir+"";
         Document dom = UtilsForXML.getDocument(dirPath);
         Element root = dom.getRootElement();
-        Element beQuery = (Element)root.selectSingleNode("//person[@id="+id+"]");
-        beQuery.getParent().remove(beQuery);
-        UtilsForXML.writeToXML(dom, dirPath);
-        return id;
-    }
-
-    public void checkXml(String path){
-        File file = new File(defaultPath+path);
-        if(file.exists()){
-            System.out.println("file success found");
+        Element beQuery = (Element)root.selectSingleNode("//item[@id="+id+"]");
+        if(beQuery != null) {
+            beQuery.getParent().remove(beQuery);
+            UtilsForXML.writeToXML(dom, dirPath);
+            return 0;
         }else {
-            System.out.println("fils not found ");
-            createdXml(file);
+            return 1;
         }
     }
 
-    public void createdXml(File file){
-        try {
-            Document doc = DocumentHelper.createDocument();
-            FileWriter os = new FileWriter(file);
-            doc.write(os);
-            os.flush();
-            os.close();
-        }catch (Exception e){
-            System.out.println("文件创建失败");
-        }
 
-    }
+    public enum XMLFILES{
+        USER("user.xml","用户信息存文件"),
+        ITEM("item.xml","商品文件");
 
-    public void test(List<T> lists) throws Exception{
+        private String path;
+        private String remark;
+        XMLFILES(String path,String remark){
+            this.path = path;
+            this.remark = remark;
+        }
 
-        for (T list : lists) {
-            Field[] fields = list.getClass().getDeclaredFields();
-            for(int i=0; i<fields.length; i++){
-                Field f = fields[i];
-                f.setAccessible(true);
-                System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(list));
-            }
+        public String getPath(){
+            return this.path;
         }
     }
-
-
-//    public void  writeInnerXml(String path, List<T> lists) throws Exception{
-//
-//        List<T> oldRes = new ArrayList<>();
-//        // 提取原有数据
-//        oldRes = readInnerXml(path);
-//        if (oldRes == null){
-//            System.out.println("not relast");
-//        }
-//        Document doc = DocumentHelper.createDocument();
-//        Element ele = doc.s("table");
-//
-//        for (T list : lists) {
-//            Field[] fields = list.getClass().getDeclaredFields();
-//            for(int i=0; i<fields.length; i++){
-//                Field f = fields[i];
-//                f.setAccessible(true);
-//                ele.setA("id", "1");
-//                Element element = ele.addElement(f.getName());
-//                element.setText(f.get(list).toString());
-//            }
-//        }
-//        try {
-//            XMLWriter writer = new XMLWriter(new FileWriter(new File(path)));
-//            writer.write(doc);
-//            writer.close();
-//        } catch (Exception e) {
-//            // TODO: handle exception
-//        }
-//    }
-
-
-//    public List<T> readInnerXml(String path) {
-//        try {
-//            File file = new File(path);
-//            SAXReader reader = new SAXReader();
-//            Document doc = reader.read(file);
-//            Element root = doc.getRootElement();
-//            List<T> lists = new ArrayList<>();
-////            root.
-//            for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
-//                Map<String,Object> res = new HashMap<>();
-//                Element type = (Element) iterator.next();
-//                res.put(type.getName(),type.getStringValue());
-//                lists.add((T)res);
-//            }
-//            return lists;
-//        } catch (Exception e) {
-//            return null;
-//        }
-//    }
 }

+ 0 - 48
src/xml/item.xml

@@ -1,48 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<table> 
-  <item id="1"> 
-    <id>1</id>  
-    <name>ceshi</name> 
-  </item>  
-  <item id="1"> 
-    <id>1</id>  
-    <name>ceshi</name> 
-  </item>  
-  <item id="1"> 
-    <id>1</id>  
-    <name>ceshiss</name> 
-  </item>  
-  <item id="1"> 
-    <id>1</id>  
-    <name>ceshiss</name> 
-  </item>  
-  <item id="1"> 
-    <id>1207227379462701056</id>  
-    <name>ceshiss</name> 
-  </item>  
-  <item id="1"> 
-    <id>1207228367980462080</id>  
-    <name>ceshiss</name> 
-  </item>  
-  <item id="1207228527720529920"> 
-    <id>1207228527720529920</id>  
-    <name>ceshiss</name> 
-  </item>  
-  <item id="1207228568648548352"> 
-    <id>1207228568648548352</id>  
-    <name>ceshiss</name> 
-  </item>  
-  <item id="1207228583332806656"> 
-    <id>1207228583332806656</id>  
-    <name>ceshiss</name> 
-  </item>  
-  <item id="1207234846737301504"> 
-    <id>1207234846737301504</id>  
-    <name>ceshiss</name> 
-  </item>  
-  <item id="1207234908804612096">
-    <id>1207234908804612096</id>
-    <name>ceshiss</name>
-  </item>
-</table>

+ 22 - 8
src/xml/user.xml

@@ -1,11 +1,25 @@
 <?xml version="1.0" encoding="UTF-8"?>
 
-<table>
-  <item id="1207233501422682112" account="admin">
-    <id>1207233501422682112</id>
-    <account>admin</account>
-    <pwd>123456</pwd>
-    <name>ceshiss</name>
-    <created>1576661870710</created>
-  </item>
+<table> 
+  <item id="1207335208093745152" account="123456"> 
+    <id>1207335208093745152</id>  
+    <account>123456</account>  
+    <pwd>fcea920f7412b5da7be0cf42b8c93759</pwd>  
+    <name>admin</name>  
+    <created>1576686119487</created> 
+  </item>  
+  <item id="1207335544376262656" account="1234567"> 
+    <id>1207335544376262656</id>  
+    <account>1234567</account>  
+    <pwd>02c75fb22c75b23dc963c7eb91a062cc</pwd>  
+    <name>1234567</name>  
+    <created>1576686199646</created> 
+  </item>  
+  <item id="1207373499929198592" account="1233112"> 
+    <id>1207373499929198592</id>  
+    <account>1233112</account>  
+    <pwd>e10adc3949ba59abbe56e057f20f883e</pwd>  
+    <name>张三</name>  
+    <created>1576695248988</created> 
+  </item> 
 </table>