m0_57987142
Custos106
采纳率75%
2021-05-14 01:59 阅读 137
已采纳

初学者,用Java解决这个问题,但是自己写除了查询之外总是会有问题,哪位能帮帮忙?最好是能给个代码

50

某公司员工拥有若干个职员,职员信息有(工号、姓名、性别、出生年月、职称、出生地、爱好)

1.请设计如下的类

(1)社员(Staff):需要包含上述属性信息;本身支持按照员工工号排序;属性【爱好】要求是不重复的集合结构。

(2)公司(Company):需要包含所有的社员信息;需要支持根据工号和名字查找员工信息的方法。

2.请实现以下功能

(1)创建公司实例,并且添加10个以上的员工信息。

(2)请分别按照工号、姓名、性别、出生年月来排序员工信息,并输出。

(3)输入一个爱好,并判断本公司内是否有这个爱好的员工;若有则输出有这个爱好的员工信息。

(4)先获取公司人员的所有出生地信息,然后按出生地归类

如上海:2人 - 张三李四

     江西:1人 - 王五

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    technologist_32 CSDN专家-Time 2021-05-14 09:18
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class Main {
        public static class Staff{
            public Integer staffId;
            public String name;
            public Integer gender;
            public Date birthday;
            public String position;
            public String birthplace;
            public List<String> hobbies;
    
            public Integer getStaffId() {
                return staffId;
            }
    
            public void setStaffId(Integer staffId) {
                this.staffId = staffId;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public Integer getGender() {
                return gender;
            }
    
            public void setGender(Integer gender) {
                this.gender = gender;
            }
    
            public Date getBirthday() {
                return birthday;
            }
    
            public void setBirthday(Date birthday) {
                this.birthday = birthday;
            }
    
            public String getPosition() {
                return position;
            }
    
            public void setPosition(String position) {
                this.position = position;
            }
    
            public String getBirthplace() {
                return birthplace;
            }
    
            public void setBirthplace(String birthplace) {
                this.birthplace = birthplace;
            }
    
            public List<String> getHobbies() {
                return hobbies;
            }
    
            public void setHobbies(List<String> hobbies) {
                this.hobbies = hobbies;
            }
    
            @Override
            public String toString() {
                return "Staff{" +
                        "staffId=" + staffId +
                        ", name='" + name + '\'' +
                        ", gender=" + gender +
                        ", birthday=" + birthday +
                        ", position='" + position + '\'' +
                        ", birthplace='" + birthplace + '\'' +
                        ", hobbies=" + hobbies +
                        '}';
            }
        }
        public static class Company{
            public List<Staff> staffs;
            // 获取
            public Staff getStaff(Integer id){
                return staffs.stream().filter(staff -> staff.staffId.equals(id)).findFirst().get();
            }
            public Staff getStaff(String name){
                return staffs.stream().filter(staff -> staff.name.equals(name)).findFirst().get();
            }
            public List<Staff> getHobbies(String hobbies){
                return staffs.stream().filter(staff -> staff.hobbies.contains(hobbies)).collect(Collectors.toList());
            }
            public void Place(){
                Map<String, List<Staff>> singleMap = staffs.stream().collect(Collectors.groupingBy(Staff::getBirthplace));
                for(String key:singleMap.keySet()){
                    System.out.println("城市:"+key+" "+"人数:"+singleMap.get(key).size()+Company.getNames(singleMap.get(key)));
                }
            }
            public static String getNames(List<Staff> staffs){
                List<String> names = new ArrayList<String>();
                for(int i=0;i<staffs.size();i++){
                    names.add(staffs.get(i).name);
                }
                return names.toString();
            }
        }
        public static void main(String args[]){
            Company company = new Company();
            List<Staff> staffs = new ArrayList<Staff>();
            company.staffs =staffs;
            for(int i=0;i<11;i++){
                Staff item = new Staff();
                item.staffId = i;
                item.name = "张三"+i;
                item.birthday = new Date(1980,01,01);
                item.gender = 1;
                if(i%2==0){
                    item.birthplace="江苏";
                    List<String> hobbies = new ArrayList<String>();
                    hobbies.add("打网球");
                    item.hobbies = hobbies;
                }
                else{
                    item.birthplace="广东";
                    List<String> hobbies = new ArrayList<String>();
                    hobbies.add("游泳");
                    item.hobbies = hobbies;
                }
    
                company.staffs.add(item);
            }
            for (int i =0;i<11;i++){
                System.out.println(staffs.get(i).toString());
            }
            System.out.println("请输入爱好:");
            Scanner scanner = new Scanner(System.in);
            String place = scanner.nextLine();
            List<Staff> list2 = company.getHobbies(place);
            for (int i =0;i<list2.size();i++){
                System.out.println(list2.get(i).toString());
            }
            company.Place();
    
    
        }
    }
    

     结果:

    "C:\Program Files\Java\jdk1.8.0_45\bin\java.exe" "-javaagent:F:\IntelliJ IDEA 2021.1\lib\idea_rt.jar=11512:F:\IntelliJ IDEA 2021.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_45\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\rt.jar;G:\BaiduNetdiskDownload\app\out\production\app" Main
    Staff{staffId=0, name='张三0', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='江苏', hobbies=[打网球]}
    Staff{staffId=1, name='张三1', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    Staff{staffId=2, name='张三2', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='江苏', hobbies=[打网球]}
    Staff{staffId=3, name='张三3', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    Staff{staffId=4, name='张三4', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='江苏', hobbies=[打网球]}
    Staff{staffId=5, name='张三5', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    Staff{staffId=6, name='张三6', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='江苏', hobbies=[打网球]}
    Staff{staffId=7, name='张三7', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    Staff{staffId=8, name='张三8', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='江苏', hobbies=[打网球]}
    Staff{staffId=9, name='张三9', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    Staff{staffId=10, name='张三10', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='江苏', hobbies=[打网球]}
    请输入爱好:
    游泳
    Staff{staffId=1, name='张三1', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    Staff{staffId=3, name='张三3', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    Staff{staffId=5, name='张三5', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    Staff{staffId=7, name='张三7', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    Staff{staffId=9, name='张三9', gender=1, birthday=Sun Feb 01 00:00:00 CST 3880, position='null', birthplace='广东', hobbies=[游泳]}
    城市:广东 人数:5[张三1, 张三3, 张三5, 张三7, 张三9]
    城市:江苏 人数:6[张三0, 张三2, 张三4, 张三6, 张三8, 张三10]
    
    Process finished with exit code 0
    
    点赞 评论 复制链接分享
  • software7503 CSDN专家-赖老师(软件之家) 2021-05-14 06:39

    你这个采纳率为0,好像从来都不采纳哦。

    点赞 评论 复制链接分享
  • technologist_36 CSDN专家-sinjack 2021-05-14 08:39

    我给你写。

    点赞 评论 复制链接分享
  • QA_Assistant 有问必答小助手 2021-05-14 18:17

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论 复制链接分享

相关推荐