6-4 jmu-Java-m06 根据姓名以及电话号码查找联系人 (20分)
该程序包含:
Person类 :
属性:int id, String name, String phoneNumber,String address。
方法:
无参构造方法,有参构造方法(id, name, phoneNumber,addres),
各个参数的getter/setter方法,
toString()方法。
Utils类 :
用于生成通讯录(具体实现下面已给出)
main方法:
使用Utils类创建出一个通讯录personList,使用Map对personList中的person对象进行分类存储,其中key为person的name值,value为对应的person对象的List列表(即 将相同name值的person对象放在一起)
输入一个number
接下去输入number行的name以及phoneNumber值,以空格分开
查找map,如果找到结果则输出对应的person对象,找不到则输出提示信息
最后一行输出map的数量,即map.size()(该行输出在输出样例中未显示)
Utils类实现如下:
class Utils{
private static final Random rd = new Random(3);
private static final int number = 200000;
private static List getPersonName(){
StringBuilder sb = new StringBuilder();
List names = new ArrayList();
for(int i=0;i 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 phoneNumbers = new ArrayList();
for(int i=0;i getPersonAddress(){
List addresses = new ArrayList();
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 getPersonList(){
List names = Utils.getPersonName();
List phones = Utils.getPhoneNumber();
List addresses = Utils.getPersonAddress();
List persons = new ArrayList();
for(int i=0;i
}
输入格式:
一个正整数number
接下去的number行:
输入name以及phoneNumber
输出格式:
如果找到对象则输出该对象
未找到对象则输出"Not Find!"。
输入样例:
5
Vern 15678785276
Paul 13369862612
Todd 15094198414
Mike 15079239680
Tony 13739215033
输出样例:
Not Find!
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 Find!
Person [id=184950, name=Tony, phoneNumber=13739215033, address=Room 71-896-9345, SongBai Road]
161999
作者
郑如滨
单位
集美大学
代码长度限制
50 KB
时间限制
2000 ms
内存限制
125 MB
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Utils personList = new Utils();
ArrayList<Person> list = new ArrayList<>();
list.addAll(personList.getPersonList());
Map<String, Object> map = new HashMap<String, Object>();
for(Person x:list)
{
map.put(x.getName(), x);
}
int number = sc.nextInt();
String [] key = new String [number];
for(int i=0;i<number;i++)
{
String name = sc.next();
String phoneNumber = sc.next();
if(map.containsKey(name)&&((Person)map.get(name)).getPhoneNumber().equals(phoneNumber))
{
System.out.println(map.get(name));
}
else
{
System.out.println("Not Find!");
}
}
System.out.println(map.size());
}
}
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.address=address;
this.name=name;
this.phoneNumber=phoneNumber;
}
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;
}
public String toString()
{
return "Person [id="+getId()+", name="+getName()+", phoneNumber="+getPhoneNumber()+", address="+getAddress()+"]";
}
}
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;
}
}