Професионална програма
Loading...
Svilen12 avatar Svilen12 3 Точки

Проблем със задача Forcebook

Здравейте, имам проблем със задачата, не знам как да  напиша output-та на тази задача, искам да го направя с API и ламбда, стигнал съм до там където трябва да извеждам, но не знам как да го направя.

https://pastebin.com/mxQBtgWK


You should store information for every unique forceUser, registered in the application.
You will receive several input lines in one of the following formats:
{forceSide} | {forceUser}
{forceUser} -> {forceSide}
The forceUser and forceSide are strings, containing any character.
If you receive forceSide | forceUser you should check if such forceUser already exists, and if not - add him/her to
the corresponding side.



If you receive a forceUser -> forceSide you should check if there is such forceUser already and if so, change his/her
side. If there is no such forceUser, add him/her to the corresponding forceSide, treating the command as new
registered forceUser.
Then you should print on the console: "{forceUser} joins the {forceSide} side!"
You should end your program when you receive the command "Lumpawaroo". At that point you should print each
force side, ordered descending by forceUsers count, than ordered by name. For each side print the forceUsers,
ordered by name.
In case there are no forceUsers in a side, you shouldn`t print the side information.
Input / Constraints
 The input comes in the form of commands in one of the formats specified above
 The input ends when you receive the command "Lumpawaroo"
Output
 As output for each forceSide, ordered descending by forceUsers count, then by name, you must print all
the forceUsers, ordered by name alphabetically
 The output format is:
Side: {forceSide}, Members: {forceUsers.Count}
! {forceUser}
! {forceUser}
! {forceUser}
 In case there are NO forceUsers, don`t print this side

 

Examples
Input 
Light | Gosho
Dark | Pesho
Lumpawaroo

Output

Side: Dark, Members: 1
! Pesho
Side: Light, Members: 1
! Gosho

Comment

We register Gosho in the Light side
and Pesho in the Dark side. After
receiving "Lumpawaroo", we print
both sides, ordered by
membersCount and then by name.

Input

Lighter | Royal                                                      
Darker | DCay
Ivan Ivanov -> Lighter
DCay -> Lighter
Lumpawaroo

Output

Ivan Ivanov joins the Lighter side!
DCay joins the Lighter side!
Side: Lighter, Members: 3
! DCay
! Ivan Ivanov
! Royal

 

Тагове:
0
Fundamentals Module 18/11/2020 17:23:11
Axiomatik avatar Axiomatik 1068 Точки

2 solutions from your collegues from Java-Fundamentals, just enter 'Forcebook' using the search button (between 'Live' and 'Contacts') and you can see all of the submission for any given exercise:

1.

        sides.entrySet()
                .stream()
                .filter(users -> users.getValue().size() > 0)
                .sorted((f, s) -> {
                    int result = s.getValue().size() - f.getValue().size(); //sorted by size descending
                    if (result == 0){
                    result = f.getKey().compareTo(s.getKey()); //by name
                }
                return result;
                }).forEach(s -> {
                    System.out.printf("Side: %s, Members: %d%n", s.getKey(), s.getValue().size());
 
                    s.getValue()
                            .stream()
                            .sorted(String::compareTo)
                            .forEach(user -> System.out.printf("! %s%n", user));
                });

2.

        forceBook.entrySet()
                .stream()
                .filter(e -> e.getValue().size() > 0)
                .sorted((f, s) -> {
                    int result = s.getValue().size() - f.getValue().size();
                    if (result == 0) {
                        return f.getKey().compareTo(s.getKey());
                    }
                    return result;
                }).forEach(e -> {
            System.out.printf("Side: %s, Members: %d%n", e.getKey(), e.getValue().size());
            e.getValue().stream()
                    .sorted((f, s) ->
                            f.compareTo(s))
                    .forEach(s -> System.out.println("! " + s));
        });

 

0
Svilen12 avatar Svilen12 3 Точки

Thank you

0