如何将两个对象字段作为同一类中的单独方法进行比较

时间:2015-09-28 04:35:33

标签: java servlets

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

if( action.equals("LOGIN")) {

...
}

 else if (action.equals("EDIT"))
    {                                                               /* get updated information for updating user */

            int userId = Integer.parseInt(request.getParameter("userId"));
            userObj = service.getUserById(userId);
            UserDetails user = getUserInfoFromRequest(request);
            forward= LIST_USER;

            if(!(userObj.getFirstName()).equals(user.getFirstName())) {
                 inbox.setMessage("Change of First Name from " +userObj.getFirstName() +" to " +user.getFirstName() +" for user " +userObj.getFirstName());
                 service.addToInbox(user.getUserId(), inbox.getMessage());
            }
            if(!(userObj.getLastName()).equals(user.getLastName())) {
                inbox.setMessage("Change of Last Name from " +userObj.getLastName() +" to " +user.getLastName() +" for user " +userObj.getFirstName());
                service.addToInbox(user.getUserId(), inbox.getMessage());
            }
            if(!(userObj.getEmail()).equals(user.getEmail())) {
                inbox.setMessage("Change of Email from " +userObj.getEmail() +" to " +user.getEmail() +" for user " +userObj.getFirstName());
                service.addToInbox(user.getUserId(), inbox.getMessage());
            }
            if(!(userObj.getUserName()).equals(user.getUserName())) {
                inbox.setMessage("Change of Username from " +userObj.getUserName() +" to " +user.getUserName() +" for user " +userObj.getFirstName());
                service.addToInbox(user.getUserId(), inbox.getMessage());
            }
            if(!(userObj.getPassword()).equals(user.getPassword())) {
                inbox.setMessage("Change of Password from " +userObj.getPassword() +" to " +user.getPassword() +" for user " +userObj.getFirstName());
                service.addToInbox(user.getUserId(), inbox.getMessage());
            }
            if((userObj.getDeptId()) != (user.getDeptId())) {
                inbox.setMessage("Change of Department from " +userObj.getDeptName() +" for user " +userObj.getFirstName());
                service.addToInbox(user.getUserId(), inbox.getMessage());
            }

            service.updateUser(user);
            String userName = request.getParameter("username");
            user = service.getUserByUserName(userName); 
            if (user!=null) {
                users =  new ArrayList<>();
                users.add(user);
            }
            request.setAttribute("users", users);
            RequestDispatcher view = request.getRequestDispatcher(forward);
            view.forward(request, response);
        }       

2 个答案:

答案 0 :(得分:0)

public class UserDetails {

    String password;
    String userName;

    public boolean compareTo(UserDetails o) {
        if (!this.password.equals(o.password)) return false;
        if (!this.userName.equals(o.userName)) return false;

        return true;
    }
}

答案 1 :(得分:0)

通过覆盖方法

来做到这一点

私有类比较扩展UserController {

    private static final long serialVersionUID = 1L;

     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         Service service = new Service(); 
            UserDetails userObj = new UserDetails();
            String action = request.getParameter("action");             
            Inbox inbox = new Inbox();

         if (action.equals("EDIT")) {   
             System.out.println("Edit in extended class");
                int userId = Integer.parseInt(request.getParameter("userId"));
                userObj = service.getUserById(userId);
                UserDetails user = getUserInfoFromRequest(request);

                if(!(userObj.getFirstName()).equals(user.getFirstName())) {
                     inbox.setMessage("Change of First Name from " +userObj.getFirstName() +" to " +user.getFirstName() +" for user " +userObj.getFirstName());
                     service.addToInbox(user.getUserId(), inbox.getMessage());
                }
                if(!(userObj.getLastName()).equals(user.getLastName())) {
                    inbox.setMessage("Change of Last Name from " +userObj.getLastName() +" to " +user.getLastName() +" for user " +userObj.getFirstName());
                    service.addToInbox(user.getUserId(), inbox.getMessage());
                }
                if(!(userObj.getEmail()).equals(user.getEmail())) {
                    inbox.setMessage("Change of Email from " +userObj.getEmail() +" to " +user.getEmail() +" for user " +userObj.getFirstName());
                    service.addToInbox(user.getUserId(), inbox.getMessage());
                }
                if(!(userObj.getUserName()).equals(user.getUserName())) {
                    inbox.setMessage("Change of Username from " +userObj.getUserName() +" to " +user.getUserName() +" for user " +userObj.getFirstName());
                    service.addToInbox(user.getUserId(), inbox.getMessage());
                }
                if(!(userObj.getPassword()).equals(user.getPassword())) {
                    inbox.setMessage("Change of Password from " +userObj.getPassword() +" to " +user.getPassword() +" for user " +userObj.getFirstName());
                    service.addToInbox(user.getUserId(), inbox.getMessage());
                }
                if((userObj.getDeptId()) != (user.getDeptId())) {
                    inbox.setMessage("Change of Department from " +userObj.getDeptName() +" for user " +userObj.getFirstName());
                    service.addToInbox(user.getUserId(), inbox.getMessage());
                } 

         }
   }
}