Advertisement
dzocesrce

[NP] Membership

Apr 29th, 2025
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. public class Membership {
  2.  
  3.     static List<User> users =new ArrayList<>();;
  4.     static Map<String,Integer> userByNumberOfRegistrations= new HashMap<>();
  5.  
  6.  
  7.     public static void createUser(String username, String password, String email, String country, Integer age) {
  8.            Membership.users.add(new User(username,password,email,country,age));
  9.            userByNumberOfRegistrations.putIfAbsent(email,0);
  10.            userByNumberOfRegistrations.computeIfPresent(email,(k,v)->v=v+1);
  11.     }
  12.  
  13.     public static Map<String,Integer> getNDifferentUsersByEmail() {
  14.         return userByNumberOfRegistrations.entrySet().stream()
  15.                 .sorted(Comparator.comparing((Map.Entry<String,Integer> entry)->entry.getValue(),Comparator.reverseOrder()))
  16.                 .collect(Collectors.toMap(
  17.                         entry->entry.getKey(),
  18.                         entry->entry.getValue(),
  19.                         (e1, e2) -> e1,
  20.                         LinkedHashMap::new
  21.                 ));
  22.  
  23.     }
  24.  
  25.     public static List<User> getUsersOrderedByEmail() {
  26.         return users.stream().sorted().collect(Collectors.toList());
  27.     }
  28.  
  29.     public static boolean deleteUser(int userId) {
  30.         User u = users.stream().filter(i->i.id == userId).findAny().orElse(null);
  31.         if(u != null){
  32.             users.remove(u);
  33.             userByNumberOfRegistrations.computeIfPresent(u.getEmail(),(k,v)->v=Math.max(0,v-1));
  34.             if(userByNumberOfRegistrations.get(u.getEmail())==0){
  35.                 userByNumberOfRegistrations.remove(u.getEmail());
  36.             }
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41. }
  42.  
  43. public class User implements Comparable<User>{
  44.     String username;
  45.     String password;
  46.     String email;
  47.     PersonalInformation personalInformation;
  48.     int id;
  49.     public static int ID_GENERATOR=0;
  50.  
  51.     public User(String username, String password, String email, String country, Integer age) {
  52.         this.username= username;
  53.         this.password = password;
  54.         this.email = email;
  55.         this.personalInformation = new PersonalInformation(country, age);
  56.         ++ID_GENERATOR;
  57.         this.id = ID_GENERATOR;
  58.     }
  59.  
  60.     public String getUsername() {
  61.         return username;
  62.     }
  63.  
  64.     public String getPassword() {
  65.         return password;
  66.     }
  67.  
  68.     public String getEmail() {
  69.         return email;
  70.     }
  71.  
  72.     public PersonalInformation getPersonalInformation() {
  73.         return personalInformation;
  74.     }
  75.  
  76.     public int getId() {
  77.         return id;
  78.     }
  79.  
  80.     @Override
  81.     public String toString() {
  82.         return String.format("%d: %s, %s", id, username, email);
  83.     }
  84.  
  85.     @Override
  86.     public int compareTo(User o) {
  87.         return Comparator.comparing(User::getEmail).compare(this,o);
  88.     }
  89. }
  90.  
  91. public class PersonalInformation {
  92.     String country;
  93.     int age;
  94.  
  95.     public PersonalInformation(String country, int age) {
  96.         this.country = country;
  97.         this.age = age;
  98.     }
  99.  
  100.     public String getCountry() {
  101.         return country;
  102.     }
  103.  
  104.     public int getAge() {
  105.         return age;
  106.     }
  107. }
  108.  
  109. public class MembershipTest {
  110.     public static void main(String[] args) {
  111.         Scanner scanner = new Scanner(System.in);
  112.         int length = scanner.nextInt();
  113.         scanner.nextLine();
  114.         for(int i=0;i<length;i++){
  115.             String username = scanner.nextLine();
  116.             String password = scanner.nextLine();
  117.             String email = scanner.nextLine();
  118.             String country = scanner.nextLine();
  119.             Integer age = scanner.nextInt();
  120.             scanner.nextLine();
  121.             Membership.createUser(username,password,email,country,age);
  122.         }
  123.  
  124.         for(int i=0;i<length;i++){
  125.             System.out.println(Membership.getNDifferentUsersByEmail());
  126.             System.out.println(Membership.getUsersOrderedByEmail());
  127.             System.out.println(Membership.deleteUser(i+1));
  128.         }
  129.  
  130.         System.out.println(Membership.deleteUser(1));
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement