S
S
Sergey Larionov2021-07-13 07:21:00
Java
Sergey Larionov, 2021-07-13 07:21:00

HashMap phone directory?

I can't figure out the task: a task about a telephone directory with support for contact groups. One contact can belong to several groups. For example, if you are working with your friend, he will be in the Friends and Work groups. The user should be able to create groups and contacts, add one contact to several groups.

import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Программа справочник" +
                "\nВведите название группы контактов:");

        while (true) {
            String input = scanner.nextLine();
            if ("нет".equals(input)) break;
            PhoneContacts.addGroup(input);

        }
        PhoneContacts.toStringList();
    }
}

import java.util.Objects;

public class Contact {
    protected static String name;
    protected static String number;

    public Contact(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Contact contact = (Contact) o;
        return Objects.equals(name, contact.name) && Objects.equals(number, contact.number);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, number);
    }

    @Override
    public String toString() {
        return "Contact{" +
                "name='" + name + '\'' +
                ", number='" + number + '\'' +
                '}';
    }

    public static void addContact(String input) {
        System.out.println("Создать контакт (введите наименование и его номер или введите нет)");
        String[] parts = input.split(" ", 2);
        Contact user = new Contact(parts[0], parts[1]);
    }
}

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PhoneContacts {

    static List<String> groups = new ArrayList<>();
    static Map<String, Contact> note = new HashMap<>();

    public static void addGroup(String input) {
        System.out.println("Создать контакт (введите наименование и его номер или введите нет)?");
        groups.add(input);
    }

    public static void toStringList() {
        System.out.println(groups.toString());
    }
}


The condition says that the user must enter the contact's name and phone number separated by a space: "John Smith +7(911)111-11-11"
How can this record be translated into an object of the Contact class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-07-13
@LarionovSergey

0. You have static in every field. It should not be.
Let it remain only in void main.
1. Your assignment says: "phone directory with support for contact groups." So add a list of groups to the contact model as well.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question