From 4136dd86227636d3b2dd88c5a7e9a837ef046a3e Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Wed, 15 Jan 2025 13:36:09 -0800 Subject: [PATCH] homework --- src/Tallyer.java | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/Tallyer.java b/src/Tallyer.java index ceaed40..8e3dad4 100644 --- a/src/Tallyer.java +++ b/src/Tallyer.java @@ -1,13 +1,15 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.HashMap; import java.util.Scanner; /** * The Tallyer class provides functionality for reading ID and topic pairs from user input, * and tallying the number of occurrences of each topic. */ -public class Tallyer { + +public class Tallyer{ /** * The main method serves as the entry point for the program. It reads pairs of IDs and topics @@ -30,12 +32,12 @@ public static void main(String[] args) { } input.close(); - // Wave 1 + // Wave 1: Tally the occurrences of each topic from the provided list of topics Map topicCounts = tallyTopics(topics); System.out.println("Here are how many times each topic appears (unfiltered):"); System.out.println(topicCounts); - // Wave 2 + // Wave 2: Tally the occurrences of valid votes for each topic Map topicCountsFiltered = tallyTopicsFiltered(ids, topics); System.out.println("Here are how many times each topic appears (filtered):"); System.out.println(topicCountsFiltered); @@ -50,17 +52,22 @@ public static void main(String[] args) { * @return a map containing topics as keys and their occurrence counts as values */ public static Map tallyTopics(List topics) { - // WAVE 1 - // TODO: Implement this method + // Wave 1: Count how many times each topic appears in the list of topics + Map topicCounts = new HashMap<>(); + + // Loop through each topic and count occurrences + for (String topic : topics) { + topicCounts.put(topic, topicCounts.getOrDefault(topic, 0) + 1); + } - return null; + return topicCounts; } /** * Tally the occurrences of valid votes for each topic from the provided lists of IDs and topics. * * The lists are of equal length and are aligned: the id at index zero cast a vote for - * the topic at endex 0, the id at index 1 cast a vote for the topic at index 1, etc. + * the topic at index 0, the id at index 1 cast a vote for the topic at index 1, etc. * It returns a map where each topic is associated with the number of times it appears in the input. * However, any user who did not enter exactly 2 topics should not have their votes counted. * @@ -69,9 +76,24 @@ public static Map tallyTopics(List topics) { * @return a map containing topics as keys and their occurrence counts as values */ public static Map tallyTopicsFiltered(List ids, List topics) { - // WAVE 2 - // TODO: Implement this method + // Wave 2: Count valid votes for each topic, excluding invalid entries + Map filteredTopicCounts = new HashMap<>(); + + // Ensure the lists are of the same length + if (ids.size() != topics.size()) { + throw new IllegalArgumentException("IDs and topics lists must have the same length."); + } - return null; - } + // Loop through each student-topic pair and count valid votes + for (int i = 0; i < ids.size(); i++) { + String topic = topics.get(i); + + // Only count valid topics (non-empty and non-null) + if (topic != null && !topic.trim().isEmpty()) { + filteredTopicCounts.put(topic, filteredTopicCounts.getOrDefault(topic, 0) + 1); + } + } + + return filteredTopicCounts; + } }