A
A
Amir Kenesbay2021-09-09 19:23:49
Java
Amir Kenesbay, 2021-09-09 19:23:49

How to add a contact to a HashMap value?

How to add a contact to a HashMap value? (Strictly do not judge, the task is educational).
1. There is a PhoneContacts class in which we will store all created groups.
2. Let's create a HashMap in the PhoneContacts class: the key for this collection will be the name of the group, and the value will be the collection of contacts.
3. Create a Contact class with fields: name and phone number (override hashCode, equals, toString if necessary)
4. Create methods for adding a group and a contact in a group in the PhoneContacts class
5. Create new groups
in a loop 6. Create new ones in another loop contacts and add them to groups
7. Display all groups with program contacts in the code, create several contacts and add them to several groups.

This is an example of how the program works

Program directory
Enter the name of the contact group:
Family
Create another group (enter a name or enter none)?
Friends
Create another group (enter a name or enter none)?
no
Create a contact (enter a name and number, or enter no)?
John Smith +7(911)111-11-11
Specify contact groups separated by a space
Family
Create a contact (enter a name or enter none)?
no
Groups in the directory:
- Family:
Name: John Smith, Phone: +7(911)111-11-11

In principle, here is my code, I did everything as it should, I even created a list where contacts will be stored, but the question is how put it in the map value?

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

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

        while (true) {
            String input = scanner.next();
            if (input.equals("no")){
                break;
            } else {
                phoneContacts.addGroup(input);
                System.out.println("Создать еще одну группу (введите название или введите нет)?");
            }
        }
        System.out.println(phoneContacts.groupMap);


        while (true){
            System.out.println("Создать контакт (введите наименование и его номер или введите нет)?");
            String nameOfContact = scanner.nextLine();
            String numberOfContact = scanner.nextLine();
            if(nameOfContact.equals("нет")){
                break;
            }
            Contact newContact = Contact.createContact(nameOfContact, numberOfContact);
            phoneContacts.addContact(newContact);
            System.out.println("Список контактов");
            phoneContacts.showContact();

            System.out.println("Укажите группу контактов через пробел");
            String inputGroupNames = scanner.nextLine();
            String[] arrayOfInputGroupNames = inputGroupNames.split(" ");
        }

        System.out.println("Группы в справочнике: ");
        System.out.println(phoneContacts.groupMap);
    }
}


import java.util.*;

public class PhoneContacts {
    Map<String, List<Contact>> groupMap = new HashMap<>();
    List<Contact> contactList = new ArrayList<>();

    public void addGroup(String inputName) {
        if (!groupMap.containsKey(inputName)) {
            groupMap.put(inputName, new ArrayList<>());
        } else {
            System.out.println("Повторяется имя группы, введите заново");
        }
    }

    public void addContact(Contact contact) {
        contactList.add(contact);
    }

    public void showContact() {
        for (Contact contact : contactList) {
            String s = contact.getName() + " " + contact.getNumber();
            System.out.println(s);
        }
    }

    public void addContactToGroup(Contact contact, String[] strings) {

    }
}


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

public class Contact {
    private String name;
    private String number;

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

    public static Contact createContact(String name, String phoneNumber) {
        return new Contact(name, phoneNumber);
    }

    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 + '\'' +
                '}';
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question