From b557fa781f36d9a6da0f944b33d55c24b908f934 Mon Sep 17 00:00:00 2001 From: Rossana Date: Tue, 24 Jun 2025 17:44:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B8=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Solution.java" | 24 ++++++++ .../User.java" | 60 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 "src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_2/User.java" diff --git "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_2/Solution.java" "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_2/Solution.java" index 0c9133b..bb45a46 100644 --- "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_2/Solution.java" +++ "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_2/Solution.java" @@ -1,7 +1,31 @@ package com.school.faang.hashmap.задача_2; +import java.util.HashMap; +import java.util.Map; + public class Solution { + public static Map users = new HashMap<>(); + public static void main(String[] args) { + Solution solution = new Solution(); + + solution.addUser("@Harry", new User(1, "Гарри Поттер", "31.07.2025")); + solution.addUser("@Hermione", new User(2, "Гермиона Грейнджер", "19.09.2025")); + solution.addUser("@Ron", new User(3, "Рон Уизли", "01.03.2025")); + solution.addUser("@Ron", new User(4, "Рон Уизли", "01.03.2025")); + + for (Map.Entry entry : users.entrySet()) { + String userName = entry.getKey(); + String fullName = entry.getValue().getFullName(); + System.out.printf("Пользователь %s, полное имя: %s.%n", userName, fullName); + } + } + public void addUser(String username, User user) { + if (users.containsKey(username) && users.containsValue(user)) { + System.out.println("Пользователь уже существует."); + } else { + users.put(username, user); + } } } diff --git "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_2/User.java" "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_2/User.java" new file mode 100644 index 0000000..7d8dc9c --- /dev/null +++ "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_2/User.java" @@ -0,0 +1,60 @@ +package com.school.faang.hashmap.задача_2; + +import java.util.Objects; + +public class User { + private long userId; + private String fullName; + private String registrationDate; + + public User(long userId, String fullName, String registrationDate) { + this.userId = userId; + this.fullName = fullName; + this.registrationDate = registrationDate; + } + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + public String getFullName() { + return fullName; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } + + public String getRegistrationDate() { + return registrationDate; + } + + public void setRegistrationDate(String registrationDate) { + this.registrationDate = registrationDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof User user)) return false; + return userId == user.userId && Objects.equals(fullName, user.fullName) && Objects.equals(registrationDate, user.registrationDate); + } + + @Override + public int hashCode() { + return Objects.hash(userId, fullName, registrationDate); + } + + @Override + public String toString() { + return "User{" + + "userId=" + userId + + ", fullName='" + fullName + '\'' + + ", registrationDate='" + registrationDate + '\'' + + '}'; + } +}