JAvaの通信録ソースコード


1️⃣联系人类
package com.kll.LinkMan;

public class LinkMan {
    //   :              
    private String name = null;
    private int age = 0;
    private String gander = null;
    private String address = null;
    private String phone = null;
    //    
    public LinkMan() {
        super();
        // TODO Auto-generated constructor stub
    }
    public LinkMan(String name, int age, String gander, String address, String phone) {
        super();
        this.name = name;
        this.age = age;
        this.gander = gander;
        this.address = address;
        this.phone = phone;
    }

    //  sete/get  
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getGander() {
        return gander;
    }
    public void setGander(String gander) {
        this.gander = gander;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    //   toString  
        @Override
        public String toString() {
            return "[name=" + name + ", age=" + age + ", gander=" + gander + ", address=" + address + ", phone="
                    + phone + "]";
        }
}

2.連絡先管理クラス
package com.kll.LinkMan;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.TreeMap;
import java.util.TreeSet;

import com.lanou3g.bean.Pinyin4jUtil;

//     
class CompareName implements Comparator{

    @Override
    public int compare(LinkMan o1, LinkMan o2) {
        int rel = o1.getName().compareTo(o2.getName());
        return rel == 0 ? 1 : rel;
    }
}

//     
class CompareAge implements Comparator{

    @Override
    public int compare(LinkMan o1, LinkMan o2) {
        int rel = o2.getAge() - o1.getAge();
        return rel == 0 ? 1 : rel;
    }

}

public class LinkManManager {
    private TreeMap> map;
    //    
    public LinkManManager() {
        //    
        this.map = new TreeMap<>();
    }

    /*
     *      
     * 1.      A-Z     26    
     * 2.                
     *         
     *          list       
     *        map 
     *         
     *       list           
     *        list  map 
     *   
     */
    public void addLinkMan(LinkMan man) {
        //             
        if (man == null || man.getName() == null) {//            
            System.out.println("        ,     !");
            return;
        }
            //     
            String key = Pinyin4jUtil.getFirstLetter(man.getName());
            //          map 
            if (!this.map.containsKey(key)) {
                //         list       list 
                ArrayList list = new ArrayList<>();
                list.add(man);
                // list     map 
                this.map.put(key, list);

            }else {
                //       list      list  
                ArrayList list = this.map.get(key);
                //                        map 
                list.add(man);
            }
    }


    //    map  
    public void printMap() {
        for (String key : this.map.keySet()) {
            ArrayList list = this.map.get(key);
            System.out.println(key + "  ");
            for (LinkMan linkMan : list) {
                System.out.println(linkMan);
            }
        }
    }

    //1.                   
    public void findLinkManByGroup(String group) {
        //    map     
        if (! (this.map.isEmpty() || group == null || group.isEmpty())) { //   
            //      
            String key = group.toUpperCase();
            //   map      key
            if (this.map.containsKey(key)) {
                //           list
                ArrayList list = this.map.get(key);
                //   treeSet     
                TreeSet set = new TreeSet<>(new CompareName());
                set.addAll(list);
                //   set
                printSet(set);
            }else {
                System.out.println("  " + key + "  ");
            }
        }else {
            //           
            System.out.println("     ");
            return;
        }

    }
    //   set  
    private void printSet(TreeSet set) {
        for (LinkMan linkMan : set) {
            System.out.println(linkMan);
        }

    }

    //2.             13811554605    map
    public void findLinkManByPhone(String phone) {
        //         
        if (! (phone == null || phone.isEmpty())) {
            //     
            //    linkman         
            LinkMan linkMan = null;
            //    map
            for (String key : this.map.keySet()) {
                //    list
                ArrayList list = map.get(key);
                //    list
                for (LinkMan linkMan2 : list) {
                    //            
                    if (linkMan2.getPhone().equals(phone)) {
                        //   
                        linkMan = linkMan2;
                    }
                }
            }
            //   linkman    
            if (linkMan == null) {
                //    
                System.out.println(phone + "   ");
            }else {
                System.out.println(linkMan);
            }
        }else {
            //    
            System.out.println("    ,  !");
            return;
        }
    }
    //3.                          
    public void findLinkMansByGander(String gander) {
        //       
        if (! (gander.equals(" ") || gander.equals(" ")) || gander == null) {
            //       
            System.out.println("      !");
            return;
        }else {
            //       
            //   list        
            ArrayList list = new ArrayList<>();
            //      
            for (String key : this.map.keySet()) {
                //   key   value
                ArrayList list2 = this.map.get(key);
                //    list2
                for (LinkMan man : list2) {
                    //      
                    if (man.getGander().equals(gander)) {
                        //             
                        // list       
                        list.add(man);
                    }
                }
            }
            //                     
            if (list.isEmpty()) {
                //              
                System.out.println("     :" + gander + "  ");
            }else {
                //          
                //   treeSet     
                TreeSet set = new TreeSet<>(new CompareAge());
                set.addAll(list);
                //   set
                printSet(set);
            } 
        }
    }
    //4.           
    /*
     *      
     *      
     *      
     *             
     *                 ? 
     *      1.      
     *      2.      key 
     */
    public void delectLinkManByNAme(String name) {
        //           
        if (! (name == null || name.isEmpty())) {
            //        
            //        
            String key = Pinyin4jUtil.getFirstLetter(name);

            //    map            
            if (this.map.containsKey(key)) {
                //    
                //           value   
                ArrayList list = this.map.get(key);
                //      
                //      

                ListIterator iterator = list.listIterator();
                //   
                while (iterator.hasNext()) {
                    LinkMan man =  iterator.next();
                    //         
                    if (man.getName().equals(name)) {
                        //             
                        iterator.remove();
                        System.out.println(man);
                    }
                }
                if (list.isEmpty()) {
                    //          
                    this.map.remove(key);
                }

            }else {
                //     
                System.out.println(name + "   " + key + "  ");
            }

        }else {//        
                System.out.println("         ");
            }
    }
    // 5.               B   B    

    public void delectLinkManByGroup(String group) {
        //    group    
        if (! (group == null || group.isEmpty())) {
            //         
            //    
            String key = group.toUpperCase();
            //            map 
            if (this.map.containsKey(key)) {
                //   map     
                //System.out.println(this.map.get(key));
                this.map.remove(key);

                return;
            }else {
                //      
                System.out.println(key + "     ");
                return;
            }
        }else {
            //    
            System.out.println("      !");
        }
    }

    // 6.     
    /*
     *   
     *         
     *                      
     *                           
     *    
     */
    public void changeLinkMan(String oldName, String newName) {

        //        
        String oldKey = Pinyin4jUtil.getFirstLetter(oldName);
        String newKey = Pinyin4jUtil.getFirstLetter(newName);
        //          
        LinkMan oldMan = null;
        //              
        if (!this.map.containsKey(oldKey)) {
            //     ,  
            System.out.println("Sorry," + oldKey + "      " + oldName + "     ");
            return;
        }
        //      
        ArrayList oldList = this.map.get(oldKey);
        //      
        for (LinkMan linkMan : oldList) {
            if (linkMan.getName().equals(oldName)) {
                //          
                oldMan = linkMan;
            }
        }
        //          
        if (oldMan == null) {
            //    
            System.out.println("  " + oldName + "    ");
            return;
        }else {//     
            //           
            if (oldKey.equals(newKey)) {
            //             
                oldMan.setName(newName);
            }else {
                //        
                LinkMan newMan = new LinkMan(newName, oldMan.getAge(),
                        oldMan.getGander(), oldMan.getAddress(),
                        oldMan.getPhone());
                //     
                delectLinkManByNAme(oldName);
                //     
                addLinkMan(newMan);
            }
        }
    }
}
//                  map list   key -char value ArrayList
//   :              
//      :
//1.                    
//     B       B          B      
//2.             13811554605    map
//3.                          
//4.           
//5.               B   B    
//6.     

3️𔣤試験類
package com.kll.LinkMan;

public class Test {
    public static void main(String[] args) {
        //       
        LinkManManager manager = new LinkManManager();
        manager.addLinkMan(new LinkMan("  ", 22, " ", "  ", "12345678901"));
        manager.addLinkMan(new LinkMan("  ", 22, " ", "  ", "12302067890"));
        manager.addLinkMan(new LinkMan("  ", 22, " ", "  ", "16945678901"));
        manager.addLinkMan(new LinkMan("  ", 22, " ", "  ", "16945678901"));
        manager.addLinkMan(new LinkMan("  ", 22,  " ", "  ", "16945678901"));
        manager.addLinkMan(new LinkMan());
        //   map    
        manager.printMap();
        //                  
        manager.findLinkManByGroup(null);
        //                
        //manager.findLinkManByPhone("16945678901");
        System.out.println(new LinkMan());

        System.out.println("++++");
        //                    
        manager.findLinkMansByGander(" ");
        //               
        manager.delectLinkManByNAme("  ");
        manager.printMap();
        System.out.println("------");
        //        
        manager.delectLinkManByGroup(null);
        manager.printMap();
        System.out.println("*******");
        //         
        manager.changeLinkMan("  ", " ");
        //manager.printMap();
    }

}