Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Membership {
- static List<User> users =new ArrayList<>();;
- static Map<String,Integer> userByNumberOfRegistrations= new HashMap<>();
- public static void createUser(String username, String password, String email, String country, Integer age) {
- Membership.users.add(new User(username,password,email,country,age));
- userByNumberOfRegistrations.putIfAbsent(email,0);
- userByNumberOfRegistrations.computeIfPresent(email,(k,v)->v=v+1);
- }
- public static Map<String,Integer> getNDifferentUsersByEmail() {
- return userByNumberOfRegistrations.entrySet().stream()
- .sorted(Comparator.comparing((Map.Entry<String,Integer> entry)->entry.getValue(),Comparator.reverseOrder()))
- .collect(Collectors.toMap(
- entry->entry.getKey(),
- entry->entry.getValue(),
- (e1, e2) -> e1,
- LinkedHashMap::new
- ));
- }
- public static List<User> getUsersOrderedByEmail() {
- return users.stream().sorted().collect(Collectors.toList());
- }
- public static boolean deleteUser(int userId) {
- User u = users.stream().filter(i->i.id == userId).findAny().orElse(null);
- if(u != null){
- users.remove(u);
- userByNumberOfRegistrations.computeIfPresent(u.getEmail(),(k,v)->v=Math.max(0,v-1));
- if(userByNumberOfRegistrations.get(u.getEmail())==0){
- userByNumberOfRegistrations.remove(u.getEmail());
- }
- return true;
- }
- return false;
- }
- }
- public class User implements Comparable<User>{
- String username;
- String password;
- String email;
- PersonalInformation personalInformation;
- int id;
- public static int ID_GENERATOR=0;
- public User(String username, String password, String email, String country, Integer age) {
- this.username= username;
- this.password = password;
- this.email = email;
- this.personalInformation = new PersonalInformation(country, age);
- ++ID_GENERATOR;
- this.id = ID_GENERATOR;
- }
- public String getUsername() {
- return username;
- }
- public String getPassword() {
- return password;
- }
- public String getEmail() {
- return email;
- }
- public PersonalInformation getPersonalInformation() {
- return personalInformation;
- }
- public int getId() {
- return id;
- }
- @Override
- public String toString() {
- return String.format("%d: %s, %s", id, username, email);
- }
- @Override
- public int compareTo(User o) {
- return Comparator.comparing(User::getEmail).compare(this,o);
- }
- }
- public class PersonalInformation {
- String country;
- int age;
- public PersonalInformation(String country, int age) {
- this.country = country;
- this.age = age;
- }
- public String getCountry() {
- return country;
- }
- public int getAge() {
- return age;
- }
- }
- public class MembershipTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int length = scanner.nextInt();
- scanner.nextLine();
- for(int i=0;i<length;i++){
- String username = scanner.nextLine();
- String password = scanner.nextLine();
- String email = scanner.nextLine();
- String country = scanner.nextLine();
- Integer age = scanner.nextInt();
- scanner.nextLine();
- Membership.createUser(username,password,email,country,age);
- }
- for(int i=0;i<length;i++){
- System.out.println(Membership.getNDifferentUsersByEmail());
- System.out.println(Membership.getUsersOrderedByEmail());
- System.out.println(Membership.deleteUser(i+1));
- }
- System.out.println(Membership.deleteUser(1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement