Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/com/school/faang/hashmap/задача_2/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

public class Solution {
public static void main(String[] args) {
UserDatabase userDatabase = new UserDatabase();

User user1 = new User(1L, "Иван Иванов", "2023-01-15");
User user2 = new User(2L, "Петр Петров", "2023-02-20");
User user3 = new User(3L, "Мария Сидорова", "2023-03-10");
User user4 = new User(4L, "Анна Кузнецова", "2023-04-05");

userDatabase.addUser(user1);
userDatabase.addUser(user2);
userDatabase.addUser(user3);
userDatabase.addUser(user4);

userDatabase.printAllUsersFormatted();
}
}
62 changes: 62 additions & 0 deletions src/com/school/faang/hashmap/задача_2/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.school.faang.hashmap.задача_2;

import java.util.Objects;

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 String getFullName() {
return fullName;
}

public String getRegistrationDate() {
return registrationDate;
}

public void setUserId(long userId) {
this.userId = userId;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public void setRegistrationDate(String registrationDate) {
this.registrationDate = registrationDate;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(fullName, user.fullName);
}

@Override
public int hashCode() {
return Objects.hashCode(fullName);
}

@Override
public String toString() {
return "User{" +
"userId=" + userId +
", fullName='" + fullName + '\'' +
", registrationDate='" + registrationDate + '\'' +
'}';
}
}
28 changes: 28 additions & 0 deletions src/com/school/faang/hashmap/задача_2/UserDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.school.faang.hashmap.задача_2;

import java.util.HashMap;
import java.util.Map;

public class UserDatabase {
private Map<String, User> myDataBase = new HashMap<>();

public void addUser(User user) {
if (user == null) {
throw new IllegalArgumentException("Где информация о пользователе?");
}
if (myDataBase.containsKey(user.getFullName())) {
throw new IllegalArgumentException("Пользователь с именем '" +
user.getFullName() + "' уже есть в базе данных");
} else {
myDataBase.put(user.getFullName(), user);
}
}

// "Пользователь @username, полное имя: Full Name"
public void printAllUsersFormatted() {
for (Map.Entry<String, User> entry : myDataBase.entrySet()) {
String username = "@" + entry.getKey().toLowerCase().replace(" ", "_");
System.out.println("Пользователь " + username + ", полное имя: " + entry.getKey());
}
}
}