Java中集合ArrayList、Map的使用

**

完整类的创建*

package com.test.Model;

public class Person {//创建Person类 包括Getter、Setter方法和无参构造方法、全参构造方法
    private int pid;
    private String name;
    private String sex;
    private String age;
    private String stratTime;

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getStratTime() {
        return stratTime;
    }

    public void setStratTime(String stratTime) {
        this.stratTime = stratTime;
    }

    public Person() {
    }

    public Person(String name, String sex, String age, String stratTime) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.stratTime = stratTime;
    }

    public Person(int pid, String name, String sex, String age, String stratTime) {
        this.pid = pid;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.stratTime = stratTime;
    }
}
package com.test.Model;

public class Department {//构造Department类
    private int did;
    private String name;

    public Department() {
    }

    public Department(String name) {
        this.name = name;
    }

    public Department(int did, String name) {
        this.did = did;
        this.name = name;
    }

    public int getDid() {
        return did;
    }

    public void setDid(int did) {
        this.did = did;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Department{" +
                "did=" + did +
                ", name='" + name + '\'' +
                '}';
    }
}

创建Dao层接口

package com.test.dao;

import com.test.Model.Person;

import java.util.List;

public interface PersonDao {
    List<Person> doGetPerson(Person person);//使用List创建Person列表

    String doGetOnePerson(Person person);
}
package com.test.dao;

import com.test.Model.Department;

public interface DepartmentDao {
    //1.生成部门:为了后期扩展
    Department doAddDepartment(Department department);
}

Dao层方法实现

package com.test.dao.impl;

import com.test.Model.Person;
import com.test.dao.PersonDao;

import java.util.ArrayList;
import java.util.List;


public class PersonDaoImpl implements PersonDao {

    //当你创建一个部门,都需要招人
    List<Person> people = new ArrayList<Person>();
    //1.校招
    @Override
    public List<Person> doGetPerson(Person person) {
        try {
            people.add(person);
            return people;
        }catch(Exception e){
            return people;
        }
    }

    @Override
    public String doGetOnePerson(Person person) {
        return null;
    }
}
package com.test.dao.impl;

import com.test.Model.Department;
import com.test.dao.DepartmentDao;

public class DepartmentImpl implements DepartmentDao {
    @Override
    public Department doAddDepartment(Department department) {
        return department;
    }
}

添加Util层,实现操作方法

import java.util.Scanner;

public class PersonUtil {
    Scanner in = new Scanner(System.in);
    public Person getPerson(String str){
        System.out.println("请你输入要"+str+"的员工编号");
        int pid = in.nextInt();
        System.out.println("请您输入要"+str+"的员工姓名");
        String name = in.next();
        System.out.println("请您输入要"+str+"的员工性别");
        String sex = in.next();
        System.out.println("请您输入要"+str+"的员工年龄");
        String age = in.next();
        System.out.println("请您输入要"+str+"的员工入职时间");
        String startTime = in.next();

        Person person = new Person(pid,name,sex,age,startTime);
        return person;
    }
}
package com.test.Util;

import com.test.Model.Department;

import java.util.Scanner;

public class DepartmentUtil {
    Scanner in = new Scanner(System.in);
    public Department getDepartment(String str){
        System.out.println("请你输入要"+str+"的部门编号");
        int did = in.nextInt();
        System.out.println("请您输入要"+str+"的部门名称");
        String name = in.next();

        Department department = new Department(did,name);
        return department;
    }
}

增加Service层,使用map讲员工写入不同的部门

package com.test.service;

import com.test.Model.Department;
import com.test.Model.Person;

import java.util.List;
import java.util.Map;

public interface DepartmentPersonService {

    //把招聘过来的人放到对应的部门中去
    Map<Department, List<Person>> doPeopleSaveToDepartment(Department department, List<Person>people);

    //实现公司所有信息的打印
    void doPrintAll();
}

方法实现

package com.test.service.impl;

import com.test.Model.Department;
import com.test.Model.Person;
import com.test.service.DepartmentPersonService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DepartmentPersonServiceImpl implements DepartmentPersonService {

    //
    Map<Department,List<Person>> maps = new HashMap<Department,List<Person>>();
    @Override
    public Map<Department, List<Person>> doPeopleSaveToDepartment(Department department, List<Person> people) {//使用map将List的员工按Department分类
        try {
            maps.put(department,people);
            return maps;
        }catch (Exception e){
            return maps;
        }
    }

    @Override
    public void doPrintAll() {
        for (Department dept:maps.keySet()){//找到全部部门进行遍历
            System.out.println("当前部门名称:"+dept.getName());

            List<Person> people = maps.get(dept);
            System.out.println("员工编号\t员工姓名\t员工共性别\t员工年龄\t员工入职时间");

            for(int i=0;i<people.size();i++){
                Person person = people.get(i);
                System.out.println(person.getPid()+person.getName()+person.getSex()+person.getAge()+person.getStratTime());
            }
            System.out.println();
        }
    }
}

最终效果测试

package com.test.test;

import com.test.Model.Department;
import com.test.Model.Person;
import com.test.Util.DepartmentUtil;
import com.test.Util.PersonUtil;
import com.test.service.DepartmentPersonService;
import com.test.service.impl.DepartmentPersonServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;


public class TestDemo {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        DepartmentPersonService dps = new DepartmentPersonServiceImpl();
        Department d1 = new DepartmentUtil().getDepartment("新增");

        List<Person> people = new ArrayList<Person>();
        System.out.println("请输入需要"+d1.getName()+"添加的人数");
        int num = in.nextInt();
        for (int i=0;i<num;i++){
            Person p = new PersonUtil().getPerson("新增第"+(i+1)+"位");
            people.add(p);
        }

        Department d2 = new DepartmentUtil().getDepartment("新增");

        List<Person> people2 = new ArrayList<Person>();
        System.out.println("请输入需要"+d2.getName()+"添加的人数");
        int num2 = in.nextInt();
        for (int i=0;i<num2;i++){
            Person p = new PersonUtil().getPerson("新增第"+(i+1)+"位");
            people.add(p);
        }


        Map<Department,List<Person>> maps = dps.doPeopleSaveToDepartment(d1,people);
        dps.doPrintAll();
    }
}

请你输入要新增的部门编号
1
请您输入要新增的部门名称
工程部
请输入需要工程部添加的人数
1
请你输入要新增第1位的员工编号
1
请您输入要新增第1位的员工姓名
合欢
请您输入要新增第1位的员工性别

请您输入要新增第1位的员工年龄
28
请您输入要新增第1位的员工入职时间
20181212
请你输入要新增的部门编号
2
请您输入要新增的部门名称
财务部
请输入需要财务部添加的人数
2
请你输入要新增第1位的员工编号
1
请您输入要新增第1位的员工姓名
宁姚
请您输入要新增第1位的员工性别

请您输入要新增第1位的员工年龄
20
请您输入要新增第1位的员工入职时间
20181212
请你输入要新增第2位的员工编号
2
请您输入要新增第2位的员工姓名
左右
请您输入要新增第2位的员工性别

请您输入要新增第2位的员工年龄
30
请您输入要新增第2位的员工入职时间
20150302
当前部门名称:工程部
员工编号 员工姓名 员工共性别 员工年龄 员工入职时间
1合欢男2820181212
1宁姚女2020181212
2左右男3020150302

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值