簡體   English   中英

如何使用類的字符串屬性來引用 Java 中的該類?

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

我有以下 3 個類,用於表示一個程序,該程序允許用戶創建一種數據庫,該數據庫可以添加人員及其電話號碼和地址,然后在創建后進行搜索。

以下是課程:

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(存儲人員 + 他們的電話號碼)

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(提供用戶與程序交互的方式)

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");
                }


            }










        }









    }

}

我對 UI 中的代碼有疑問

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

當命令以 2 給出時,我向用戶詢問一個名稱並以字符串形式給出,然后我如何使用該字符串在 HashMap 下找到具有該名稱的 person 對象的值,即與相關聯的一組數字此人。

顯然我不能用地圖上的 get 方法來做到這一點,因為鍵是人對象而不是字符串,我不知道該怎么做(我是否需要在字符串名稱和人名之間存儲一個映射),我相信這個上下文名字是唯一的?

還有一個額外的問題:當我執行命令 1 並給出一個不是所有數字的數字(例如“045-4558”)時,為什么會出現錯誤。

謝謝

也許是這樣的:

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