settingsAccountsettings
Menusettings

Q: Hands of cards - Java Task (Maps)

+13 votes

You are given a sequence of people and for every person what cards he draws from the deck. The input will be separate lines in the format:

{personName}: {PT, PT, PT,… PT}

Where P (2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A) is the power of the card and T (S, H, D, C) is the type. The input ends when a "JOKER" is drawn. The name can contain any ASCII symbol except ':'. The input will always be valid and in the format described, there is no need to check it.

A single person cannot have more than one card with the same power and type, if he draws such a card he discards it. The people are playing with multiple decks. Each card has a value that is calculated by the power multiplied by the type. Powers 2 to 10 have the same value and J to A are 11 to 14. Types are mapped to multipliers the following way (S -> 4, H-> 3, D -> 2, C -> 1).

Finally print out the total value each player has in his hand in the format:

{personName}: {value}

Examples:

Input

Output

Pesho: 2C, 4H, 9H, AS, QS

Slav: 3H, 10S, JC, KD, 5S, 10S

Peshoslav: QH, QC, QS, QD

Slav: 6H, 7S, KC, KD, 5S, 10C

Peshoslav: QH, QC, JS, JD, JC

Pesho: JD, JD, JD, JD, JD, JD

JOKER

Pesho: 167

Slav: 175

Peshoslav: 197

asked in Java category by user paulcabalit

1 Answer

+2 votes
 
Best answer

Here's the solution:

import java.util.*;

public class Pr_08_HandsOfCards {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        List<Character> powers = Arrays.asList('2', '3', '4', '5', '6', '7', '8', '9', '1', 'J', 'Q', 'K', 'A');
        List<Character> suits = Arrays.asList('C', 'D', 'H', 'S');

        LinkedHashMap<String, HashSet<String>> handsMap = new LinkedHashMap<>();

        while (true) {
            String[] input = scan.nextLine().trim().split(": ");//GETTING THE INPUT
            if (input[0].equals("JOKER")) {//IF THE INPUT IS "JOKER" - BREAK THE OPERATION
                break;
            }

            String name = input[0];//THE NAME OF THE PLAYER
            String[] hand = input[1].split(", ");//THE POWER AND THE TYPE (COLOR) OF THE CARD
            if (!handsMap.containsKey(name)) {
                handsMap.put(name, new HashSet<>());
            }
            for (int i = 0; i < hand.length; i++) {//ITERATING THROUGH THE hand STRING ARRAY
                handsMap.get(name).add(hand[i]);
            }
        }

        for (String name : handsMap.keySet()) {
            System.out.printf("%s: ", name);
            HashSet<String> currHand = handsMap.get(name);
            int handSize = 0;
            for (String card : currHand) {
                int power = powers.indexOf(card.charAt(0)) + 2;
                int suit = suits.indexOf(card.charAt(card.length() - 1)) + 1;
                handSize += power * suit;
            }
            System.out.println(handSize);
        }
    }
}
answered by user richard8502
edited by user golearnweb
...