dengdaiyemanren / onetopiconeday

关注感兴趣的话题,实践
6 stars 1 forks source link

互选程序 #16

Open dengdaiyemanren opened 4 years ago

dengdaiyemanren commented 4 years ago
package match;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Match {

    Map man = new HashMap<String,ArrayList>();
    Map wman = new HashMap<String,ArrayList>();

    Map resultM = new HashMap<String,ArrayList>();

    public void matchM() throws IOException
    {
        String filePathA = this.getClass().getResource("/dirinfo/").getPath() + "testman.txt";

        File file = new File(filePathA);

        BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));

        String lineStr;
        boolean nextFlag = false;
        String currentNo = null;

        while ((lineStr = in.readLine()) != null)
        {
            if(lineStr.contains("我的编号"))
            {
                String chooseMno = new String(lineStr.substring(lineStr.indexOf("我的编号")+5)).trim();
                System.out.println("男方编号:"+chooseMno);
                nextFlag = false;
                currentNo = chooseMno;
                continue;
            }

            if(nextFlag == false && lineStr.contains("我选择的是"))
            {
                String chooseMstr = new String(lineStr.substring(lineStr.indexOf("我选择的是")+6)).trim();

                System.out.println("男方选择"+chooseMstr);

                List choose = new ArrayList();

                String [] result = chooseMstr.split(",");

                choose= Arrays.asList(result);

                man.put(currentNo, choose);

                nextFlag = true;

            }

        }

    }

    public void matchW() throws IOException
    {
        String filePathA = this.getClass().getResource("/dirinfo/").getPath() + "testwoman.txt";

        File file = new File(filePathA);    
        BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));

        String lineStr;
        boolean nextFlag = false;
        String currentNo = null;

        while ((lineStr = in.readLine()) != null)
        {
            if(lineStr.contains("我的编号"))
            {

                String chooseMno = new String(lineStr.substring(lineStr.indexOf("我的编号")+5)).trim();
                System.out.println("女方编号:"+chooseMno);
                nextFlag = false;
                currentNo = chooseMno;
                continue;
            }

            if(nextFlag == false && lineStr.contains("我选择的是"))
            {
                String chooseMstr = new String(lineStr.substring(lineStr.indexOf("我选择的是")+6)).trim();

                System.out.println("女方选择:"+chooseMstr);

                List choose = new ArrayList();

                String [] result = chooseMstr.split(",");
                choose= Arrays.asList(result);

                wman.put(currentNo, choose);

                nextFlag = true;

            }

        }

    }

    public void matchALL()
    {

        System.out.println("开始匹配");

        //匹配中的数据

        Set<String> manKey= man.keySet();

        //获取男选择的编号,同时查看男的编号是否在女的选择里面

        for(String key :manKey)
        {
            System.out.println("男:"+key+"选择的编号是:"+man.get(key));

            List<String> chooseWlist =  (List) man.get(key);

            for(String one:chooseWlist)
            {
                List<String> chooseMlist =  (List<String>) wman.get(one);

                if(null!=chooseMlist && chooseMlist.contains(key))
                {
                    System.out.println("互相OK:男"+key+"女:"+one);

                    if(resultM.get(one) == null)
                    {
                        resultM.put(key, new ArrayList().add(one));
                    }
                    else
                    {
                        ((ArrayList) resultM.get(key)).add(one);
                    }
                }
            }

        }

        System.out.println("匹配结束");

    }

    public static void main(String[] args) throws IOException {

        Match match  = new Match();

        match.matchM();
        match.matchW();
        match.matchALL();

    }

}