简体   繁体   English

如何使用类的字符串属性来引用 Java 中的该类?

[英]How can I use a string attribute of a class to refer to that class in java?

I have the following 3 classes that is meant to represent a program that allows a user to create a sort of database that can add people and their phone numbers and addresses and then search for them once created.我有以下 3 个类,用于表示一个程序,该程序允许用户创建一种数据库,该数据库可以添加人员及其电话号码和地址,然后在创建后进行搜索。

Here are the classes:以下是课程:

Person

import java.util.*;


public class Person implements Comparable<Person> {

 private final String name;
 private String street;
 private String city;
 private String address;

    public Person(String name) {
        this.name = name;
        this.address = "address unknown";
    }

    public String getName() {
        return name;
    }

    public void setAddress(String street, String city){
        this.city = city;
        this.street = street;
        this.address = this.street + " " + this.city;
    }

    public String getAddress() {
        return address;
    }

    @Override
    public int compareTo(Person o) {
        return this.name.compareToIgnoreCase(o.getName());
    }











}

PhoneNumbers (to store the person + their phone numbers) PhoneNumbers(存储人员 + 他们的电话号码)

import java.util.*;
import java.util.Collections;



public class PhoneNumbers {

    private Map<Person, Set<String>> numbers;

    public PhoneNumbers() {
        this.numbers = new HashMap<Person, Set<String>>();

    }

    public void addPhoneNumber(Person person, String phoneNumber){
        if(!this.numbers.keySet().contains(person)){
            this.numbers.put(person, new HashSet<String>());
        }

        this.numbers.get(person).add(phoneNumber);
    }

    public Set<String> searchByPerson(Person person){
        if(!this.numbers.keySet().contains(person)){
            return this.numbers.get(person);
        }

        return null;
    }

    public String searchByNumber(String number){
        for(Person person : this.numbers.keySet()){
            Set<String> x = this.numbers.get(person);
            for(String y : x){
                if(y.equals(number)){
                    return person.getName();
                }                
            }
        }
        return "  not found";
    }

    public void personalSearch(Person person){
        System.out.println("  " + person.getAddress());
        if(this.numbers.get(person).size() == 0){
            System.out.println("  phone number not found");
        }
        for(String x : this.numbers.get(person)){
            System.out.println("  phone numbers");
            System.out.println("   " + x);
        }
    }

    public void remove(Person person){
        this.numbers.remove(person);
    }

    public List<Person> masterSearch(String search){
        ArrayList<Person> names = new ArrayList<Person>();

        for(Person person : this.numbers.keySet()){
            if(person.getName().contains(search) || person.getAddress().contains(search)){
                names.add(person);
            }
        }

        Collections.sort(names);

        return names;
    }

    public Map<Person, Set<String>> getNumbers() {
        return numbers;
    }







}

UI (to provide the way for the user to interact with the program) UI(提供用户与程序交互的方式)

import java.util.Scanner;

public class UI {

    private Scanner reader;
    private PhoneNumbers phoneNumbers;

    public UI() {
        this.reader = new Scanner(System.in);
        this.phoneNumbers = new PhoneNumbers();
    }





    public void start(){

        System.out.println("phone search\n" +
        "available operations:\n" +
        " 1 add a number\n" +
        " 2 search for a number\n" +
        " 3 search for a person by phone number\n" +
        " 4 add an address\n" +
        " 5 search for personal information\n" +
        " 6 delete personal information\n" +
        " 7 filtered listing\n" +
        " x quit\n");

        while(true){

            System.out.print("command: ");
            int command = Integer.parseInt(reader.nextLine());
            System.out.println("");

            if(command == 1){


                System.out.print("whose number: ");
                String name = reader.nextLine();
                System.out.print("number: ");
                String number = reader.nextLine();

                Person person = new Person(name);
                this.phoneNumbers.addPhoneNumber(person, number);


            }

            else if(command == 2){
                System.out.print("whose number: ");
                String person = reader.nextLine();
                //something here
                    }
                }
                else{
                    System.out.println("  not found");
                }


            }










        }









    }

}

I have a question with the code in the UI specifically我对 UI 中的代码有疑问

else if(command == 2){
                System.out.print("whose number: ");
                String person = reader.nextLine();

when the command is given as 2 I ask the user for a name and that is given as a string, how do I then use that String to find the value of the person object with that name under the HashMap ie the set of numbers associated with the person.当命令以 2 给出时,我向用户询问一个名称并以字符串形式给出,然后我如何使用该字符串在 HashMap 下找到具有该名称的 person 对象的值,即与相关联的一组数字此人。

Obviously I cannot do this with a get method on the map as the keys are person objects not strings and I don't know what to do (do I need to store a map between String name and Person name), I believe in this context names are unique?显然我不能用地图上的 get 方法来做到这一点,因为键是人对象而不是字符串,我不知道该怎么做(我是否需要在字符串名称和人名之间存储一个映射),我相信这个上下文名字是唯一的?

Also bonus question: why would I be getting an error when I do command 1 and give a number that is not all digits like "045-4558" for instance.还有一个额外的问题:当我执行命令 1 并给出一个不是所有数字的数字(例如“045-4558”)时,为什么会出现错误。

Thank you谢谢

Maybe something like that:也许是这样的:

for (Map.Entry<Person, Set<String>> entry : numbers.entrySet()) {
    Person key = entry.getKey();

    if (key.getName().equals(nameWhichYouAreLookingFor)) {
       //Do whatever you want
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM