Java复习41(PTA)

jmu-Java-m06 根据姓名以及电话号码查找联系人

分数 20

全屏浏览

切换布局

作者 郑如滨

单位 集美大学

该程序包含:

Person类:

属性:int id, String name, String phoneNumber,String address。

方法:

无参构造方法,有参构造方法(id, name, phoneNumber,addres),

各个参数的getter/setter方法,

toString()方法。

Utils类:

用于生成通讯录(具体代码下面已给出)

Main类的main方法:

使用Utils类创建一个通讯录personList,使用Map将personList中的person对象按name分类存储。即,key为person的name值,value为对应的person对象的List列表(说明:就是将相同name值的person对象放在一起List列表中)。

接下来就可以在该Map中根据姓名与电话号码查询联系人。查询方法如下:

  1. 输入n
  2. 输入n行的name以及phoneNumber值,以空格分开。然后根据name查找map,如果找到且在列表中的存在某个person对象的电话号码也相同,则输该person对象。如果找不到(即找不到name与电话号码均匹配的Person对象)则输出提示信息。
  3. 最后一行输出map的数量,即map.size()(该行输出在输出样例中未显示)

Utils类实现如下:

class Utils{

    private static final Random rd = new Random(3);
    private static final int number = 200000;
    private static List<String> getPersonName(){
        StringBuilder sb = new StringBuilder();
        List<String> names = new ArrayList<String>();
        for(int i=0;i<Utils.number;i++) {
            sb.delete(0, sb.length());
            for(int j=0;j<4;j++) {
                if(j==0) {
                    sb.append((char)('A'+rd.nextInt(26)));
                }
                else {
                    sb.append((char)('a'+rd.nextInt(26)));
                }
            }
            names.add(sb.toString());
        }
        return names;

    }
    private static List<String> getPhoneNumber(){
    
        String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
        StringBuilder sb = new StringBuilder();
        List<String> phoneNumbers = new ArrayList<String>();
        for(int i=0;i<Utils.number;i++) {
            sb.delete(0, sb.length());
            for(int j=0;j<9;j++) {
                if(j==0) {
                    sb.append(telFirst[rd.nextInt(telFirst.length)]);
                }
                else {
                    sb.append(rd.nextInt(10));
                }
            }
            phoneNumbers.add(sb.toString());
        }
        return phoneNumbers;
    }

    private static List<String> getPersonAddress(){
        List<String> addresses = new ArrayList<String>();
        String[] roads = {"HuanDao Road","WenZeng Road","ZhongShang Road","KaiHe Road","NanHua Road","SongBai Road","BinShui Road","ShiGu Road","JiYuan Road","YinJiang Road"};
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<Utils.number;i++) {
            sb.delete(0, sb.length());
            for(int j=0;j<2;j++) {
                if(j==1) {
                    sb.append(roads[rd.nextInt(roads.length)]);
                }
                else {
                    sb.append("Room "+rd.nextInt(100)+"-"+rd.nextInt(1000)+"-"+rd.nextInt(10000)+", ");
                }
            }
            addresses.add(sb.toString());
        }
        return addresses;
    }

    public static List<Person> getPersonList(){
        List<String> names = Utils.getPersonName();
        List<String> phones = Utils.getPhoneNumber();
        List<String> addresses = Utils.getPersonAddress();
        List<Person> persons = new ArrayList<Person>();

        for(int i=0;i<Utils.number;i++) {
            Person person = new Person();
            person.setId(i);
            person.setName(names.get(i));
            person.setPhoneNumber(phones.get(i));
            person.setAddress(addresses.get(i));
            persons.add(person);
        }
        return persons;
    }
}

输入格式:

一个正整数n

接下去的n行:

输入name以及phoneNumber

输出格式:

如果找到对象则输出该对象

未找到对象则输出"Not found!"。

输入样例:

5
Vern 15678785276
Paul 13369862612
Todd 15094198414
Mike 15079239680
Tony 13739215033

输出样例:

Not found!
Person [id=162538, name=Paul, phoneNumber=13369862612, address=Room 8-446-6866, ShiGu Road]
Person [id=139814, name=Todd, phoneNumber=15094198414, address=Room 8-856-5546, JiYuan Road]
Not found!
Person [id=184950, name=Tony, phoneNumber=13739215033, address=Room 71-896-9345, SongBai Road]
161999

代码长度限制

50 KB

时间限制

2500 ms

内存限制

256 MB

栈限制

8192 KB

java 复制代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Person> personList = Utils.getPersonList();
        Map<String, List<Person>> personMap = new HashMap<>();

        // 按姓名分类存储
        for (Person person : personList) {
            personMap.computeIfAbsent(person.getName(), k -> new ArrayList<>()).add(person);
        }

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine(); // 读取换行符

        for (int i = 0; i < n; i++) {
            String line = scanner.nextLine();
            String[] parts = line.split(" ");
            String name = parts[0];
            String phoneNumber = parts[1];

            if (personMap.containsKey(name)) {
                List<Person> persons = personMap.get(name);
                boolean found = false;
                for (Person person : persons) {
                    if (person.getPhoneNumber().equals(phoneNumber)) {
                        System.out.println(person);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    System.out.println("Not found!");
                }
            } else {
                System.out.println("Not found!");
            }
        }

        System.out.println(personMap.size()); // 输出map的数量
        scanner.close();
    }
}

class Person {
    private int id;
    private String name;
    private String phoneNumber;
    private String address;

    public Person() {
    }

    public Person(int id, String name, String phoneNumber, String address) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", phoneNumber=" + phoneNumber + ", address=" + address + "]";
    }
}

class Utils {

    private static final Random rd = new Random(3);
    private static final int number = 200000;

    private static List<String> getPersonName() {
        StringBuilder sb = new StringBuilder();
        List<String> names = new ArrayList<String>();
        for (int i = 0; i < Utils.number; i++) {
            sb.delete(0, sb.length());
            for (int j = 0; j < 4; j++) {
                if (j == 0) {
                    sb.append((char) ('A' + rd.nextInt(26)));
                } else {
                    sb.append((char) ('a' + rd.nextInt(26)));
                }
            }
            names.add(sb.toString());
        }
        return names;

    }

    private static List<String> getPhoneNumber() {

        String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
        StringBuilder sb = new StringBuilder();
        List<String> phoneNumbers = new ArrayList<String>();
        for (int i = 0; i < Utils.number; i++) {
            sb.delete(0, sb.length());
            for (int j = 0; j < 9; j++) {
                if (j == 0) {
                    sb.append(telFirst[rd.nextInt(telFirst.length)]);
                } else {
                    sb.append(rd.nextInt(10));
                }
            }
            phoneNumbers.add(sb.toString());
        }
        return phoneNumbers;
    }

    private static List<String> getPersonAddress() {
        List<String> addresses = new ArrayList<String>();
        String[] roads = {"HuanDao Road", "WenZeng Road", "ZhongShang Road", "KaiHe Road", "NanHua Road", "SongBai Road", "BinShui Road", "ShiGu Road", "JiYuan Road", "YinJiang Road"};
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < Utils.number; i++) {
            sb.delete(0, sb.length());
            for (int j = 0; j < 2; j++) {
                if (j == 1) {
                    sb.append(roads[rd.nextInt(roads.length)]);
                } else {
                    sb.append("Room " + rd.nextInt(100) + "-" + rd.nextInt(1000) + "-" + rd.nextInt(10000) + ", ");
                }
            }
            addresses.add(sb.toString());
        }
        return addresses;
    }

    public static List<Person> getPersonList() {
        List<String> names = Utils.getPersonName();
        List<String> phones = Utils.getPhoneNumber();
        List<String> addresses = Utils.getPersonAddress();
        List<Person> persons = new ArrayList<Person>();

        for (int i = 0; i < Utils.number; i++) {
            Person person = new Person();
            person.setId(i);
            person.setName(names.get(i));
            person.setPhoneNumber(phones.get(i));
            person.setAddress(addresses.get(i));
            persons.add(person);
        }
        return persons;
    }
}
相关推荐
没书读了23 分钟前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·31 分钟前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic1 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王1 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康1 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神2 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
qq_327342732 小时前
Java实现离线身份证号码OCR识别
java·开发语言
阿龟在奔跑3 小时前
引用类型的局部变量线程安全问题分析——以多线程对方法局部变量List类型对象实例的add、remove操作为例
java·jvm·安全·list
飞滕人生TYF3 小时前
m个数 生成n个数的所有组合 详解
java·递归
代码小鑫3 小时前
A043-基于Spring Boot的秒杀系统设计与实现
java·开发语言·数据库·spring boot·后端·spring·毕业设计