shadowsong_moon
2021-03-28 03:58
采纳率: 0%
浏览 308
已结题

Java设计一个类(已有一个User类),实现接口,查询是否存在某个用户,怎么写?

这两个(设计一个类,实现接口,查询是否存在某个用户),(设计一个类,实现接口,修改某个用户密码中的字母全部为小写)具体(代码)要怎么写啊

不太懂这题的两个(设计类实现接口)怎么写。是写个接口,写个类实现接口吗?还是User类实现接口?

可以的话帮我做完这道题,不尽感激!

Java程序设计题

User 有属性:用户名、密码、地址;

有一个接口如下: interface OperationService{ public String action(LinkedList client,String key); }

1.设计一个 User 类(按简单 Java 类的要求设计)

2.设计一个类,实现接口,查询是否存在某个用户 。

3.设计一个类,实现接口,修改某个用户密码中的字母全部为小写。

4.设计主类,执行程序,输出查询、修改后的结果。

3月28日21点前截止

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

5条回答 默认 最新

  • ProfSnail 2021-03-28 12:35
    最佳回答

    整上 

    // OperationService.java文件
    import java.util.LinkedList;
    
    public interface OperationService {
        public String action(LinkedList client, String key);
    }
    
    // User.java文件
    public class User{
        private String username;
        private String password;
        private String address;
        public User(){
            this.username = "";
            this.password = "";
            this.address = "";
        }
        public User(String username, String password, String address){
            this.username = username;
            this.password = password;
            this.address = address;
        }
        public String getUsername(){
            return this.username;
        }
        public String getPassword(){
            return this.password;
        }
        public String getAddress(){
            return this.address;
        }
        public void setPassword(String password){
            this.password = password;
        }
    }
    
    // SearchUser.java文件
    import java.util.LinkedList;
    
    public class searchUser {
        public searchUser(){
            ;
        }
        public String action(LinkedList client, String key){
            if(client!=null && client.size()>0 && key!=null){
                for(Object eachUser: client){
                    User thisUser = (User) eachUser;
                    if(thisUser.getUsername().equals(key)){
                        return thisUser.getUsername()+":"+thisUser.getAddress();
                    }
                }
                return "The user "+key+" is not in the list";
            }
            return "Find failed";
        }
    }
    
    // setPasswordLowercase.java文件
    import java.util.LinkedList;
    
    public class setPasswordLowercase {
        public setPasswordLowercase(){
            ;
        }
        public String action(LinkedList client, String key){
            if(client!=null && client.size()>0 && key!=null){
                for(int i = 0; i < client.size(); i++){
                    User eachUser = (User) client.get(i);
                    if(eachUser.getUsername().equals(key)){
                        System.out.println("Before set password, " + key+ "'s password is :" + eachUser.getPassword());
                        eachUser.setPassword(eachUser.getPassword().toLowerCase());
                        client.set(i, eachUser);
                        System.out.println("After set password, " + key+ "'s password is :" + eachUser.getPassword());
                        return "Set " + key + "'s password to its lowercased format. ";
                    }
                }
                return "Set failed. Not find " + key;
            }
            return "Set failed, for the input arguments invalid.";
        }
    }
    
    // mainClass.java文件
    import java.util.LinkedList;
    
    public class mainClass {
        public static void main(String[] args){
            LinkedList<User> client = new LinkedList<User>();
            client.add(new User("Xiaoming", "xiaomingPassWord", "xiaomingDeAddress"));
            client.add(new User("Zhangsan", "zhangSanPassWord", "zhangSanDeAddress"));
            client.add(new User("Lisi", "LisiPassWord", "LisiDeAddress"));
            client.add(new User("WangWu", "WangwuPassWord", "wangWuDeAddress"));
            System.out.println(new searchUser().action(client,"Xiaoming"));
            System.out.println(new searchUser().action(client,"xiaoming"));
            System.out.println(new searchUser().action(client,"Wangwu"));
            System.out.println(new searchUser().action(client,"ProfSnail"));
            System.out.println(new setPasswordLowercase().action(client,"Wangwu"));
            System.out.println(new setPasswordLowercase().action(client,"Wangwu"));
            System.out.println(new setPasswordLowercase().action(client,"Xiaoming"));
            System.out.println(new setPasswordLowercase().action(client,"ProfSnail"));
        }
    }
    

    在我这里已经测试运行通过了。题主可以私信找我要一个打包好的程序包。

     

    评论
    解决 1 无用
    打赏 举报
查看更多回答(4条)

相关推荐 更多相似问题