Question: Rewrite The Following Java Class so that it performs the same as before but is more simplified than below to allow greater functionality and if
Rewrite The Following Java Class so that it performs the same as before but is more simplified than below to allow greater functionality and if possible test on junit provided
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner;
public class Friend implements Tester {
static HashMap < String, HashSet < String >> friendsMap;
Friend(HashMap < String, HashSet < String >> friendsMap) { //Constructor Creating the Hashmap Friend.friendsMap = friendsMap; }
public static void main(String[] args) { Friend friend = new Friend(new HashMap < String, HashSet < String >> ()); //Initializes map to be used Scanner sc = new Scanner(System.in); ArrayList < String > result = friend.compute(sc); for (String print: result) { System.out.println(print); } }
/* * This starts reading through the scanner input file and will check for * specific keywords before passing them on to other methods and * finally return the result of recommendations */ @Override public ArrayList < String > compute(Scanner log) { ArrayList < String > result = new ArrayList < > (); String curr; while (!(curr = log.nextLine()).equals("end")) { if (curr.endsWith("joins")) { joins(curr.split(" ")[0]); //Takes user name then adds user } else if (curr.endsWith("leaves")) { leaves(curr.split(" ")[0]); //Takes user name then removes user } else { String[] words = curr.split(" "); if (words[1].equals("friends")) { friends(words[0], words[2], result); //Takes users and makes friends } else if (words[1].equals("unfriends")) { unfriends(words[0], words[2]); //Takes users then removes friend status } } }
return result; }
/* * This Method checks for a users name then adds them onto the hashmap */ private void joins(String name) { System.out.println(name + " joined social networking"); if (!friendsMap.containsKey(name)) { friendsMap.put(name, new HashSet < String > ()); } }
/* * This Method removes every occurrence of a user using the network * if they leave they are no longer referenced */ private void leaves(String name) { System.out.println(name + " left social networking"); if (friendsMap.containsKey(name)) { friendsMap.remove(name); }
for (String key: friendsMap.keySet()) { if (friendsMap.get(key).contains(name)) { friendsMap.get(key).remove(name); } } }
/* * This Method removes users from the hashmap if the input displays * unfriend which then finds the user and removes them */ private void unfriends(String userA, String userB) { if (friendsMap.containsKey(userA)) { if (friendsMap.get(userA).contains(userB)) { friendsMap.get(userA).remove(userB); } } if (friendsMap.containsKey(userB)) { if (friendsMap.get(userB).contains(userA)) { friendsMap.get(userB).remove(userA); } } }
/* * This Method scans through the hashmap and will add recommendations based * on what is in the current hashmap */ private void friends(String userA, String userB, ArrayList < String > result) { for (String friend: friendsMap.get(userA)) { String temp = new String(""); if (friend.compareTo(userB) < 0) { temp = temp.concat(friend).concat(" and ").concat(userB).concat(" should be friends"); } else if (friend.compareTo(userB) > 0) { temp = temp.concat(userB).concat(" and ").concat(friend).concat(" should be friends"); } System.out.println(temp); result.add(temp); }
for (String friend: friendsMap.get(userB)) { String tmp = new String(""); if (friend.compareTo(userA) < 0) { tmp = tmp.concat(friend).concat(" and ").concat(userA).concat(" should be friends"); } else if (friend.compareTo(userA) > 0) { tmp = tmp.concat(userA).concat(" and ").concat(friend).concat(" should be friends"); } System.out.println(tmp); result.add(tmp); } friendsMap.get(userA).add(userB); friendsMap.get(userB).add(userA); } }
Do Not Change The Following Below (For Testing Purposes)
Tester Interface Class
import java.util.ArrayList; import java.util.Scanner; public interface Tester { // This method takes a Scanner object contianint the input // and returns an ArrayList object containing the output. // // Parameters: // Scanner input: is a Scanner object that is a stream of text // containing the input to your program. // // Returns: an ArrayList of Strings. Each string is a line of output // from your program. public ArrayList compute( Scanner log ); } Junit Test Class
import static org.junit.jupiter.api.Assertions.*; import java.util.ArrayList; import java.util.Scanner; import org.junit.jupiter.api.Test; class FriendTest { private static String testInput0 = "end"; private static String [] testOutput0 = {}; private static String testInput1 = "Alice joins " + "Carol joins " + "Bob joins " + "Bob friends Alice " + "Bob friends Carol " + "Alice leaves " + "Bob leaves " + "Carol leaves " + "end"; private static String [] testOutput1 = { "Alice and Carol should be friends" }; private static String testInput2 = "Alice joins " + "Carol joins " + "Bob joins " + "Bob friends Alice " + "Bob friends Carol " + "Bob unfriends Carol " + "Bob friends Carol " + "Alice leaves " + "Bob leaves " + "Carol leaves " + "end"; private static String [] testOutput2 = { "Alice and Carol should be friends", "Alice and Carol should be friends" }; private static String testInput3 = "Alice joins " + "Carol joins " + "Bob joins " + "Bob friends Alice " + "Bob friends Carol " + "Dave joins " + "Dave friends Bob " + "Alice leaves " + "Bob leaves " + "Carol leaves " + "Dave leaves " + "end"; private static String [] testOutput3 = { "Alice and Carol should be friends", "Alice and Dave should be friends", "Carol and Dave should be friends" }; private static String testInput4 = "Alice joins " + "Carol joins " + "Bob joins " + "Bob friends Alice " + "Bob friends Carol " + "Dave joins " + "Dave friends Bob " + "Carol leaves " + "Carol joins " + "Carol friends Alice " + "Alice leaves " + "Bob leaves " + "Carol leaves " + "Dave leaves " + "end"; private static String [] testOutput4 = { "Alice and Carol should be friends", "Alice and Dave should be friends", "Carol and Dave should be friends", "Bob and Carol should be friends" }; private static String testInput5 = "Alice joins " + "Carol joins " + "Bob joins " + "Bob friends Alice " + "Bob friends Carol " + "Dave joins " + "Dave friends Bob " + "Eve joins " + "Eve friends Bob " + "Dave leaves " + "Dave joins " + "Dave friends Bob " + "Dave unfriends Bob " + "Dave friends Bob " + "Fred joins " + "Alice friends Fred " + "Bob friends Fred " + "Carol friends Fred " + "Bob leaves " + "Fred leaves " + "Alice friends Carol " + "Alice leaves " + "Carol leaves " + "Dave leaves " + "end"; private static String [] testOutput5 = { "Alice and Carol should be friends", "Alice and Dave should be friends", "Carol and Dave should be friends", "Alice and Eve should be friends", "Carol and Eve should be friends", "Dave and Eve should be friends", "Alice and Dave should be friends", "Carol and Dave should be friends", "Dave and Eve should be friends", "Alice and Dave should be friends", "Carol and Dave should be friends", "Dave and Eve should be friends", "Bob and Fred should be friends", "Carol and Fred should be friends", "Dave and Fred should be friends", "Eve and Fred should be friends", "Alice and Carol should be friends" }; private static Scanner mkTest( String input ) { return new Scanner( input ); } private static ArrayList mkOutput( String [] output ) { ArrayList al = new ArrayList(); for( String s : output ) { al.add( s ); } return al; } private static boolean doTest( String input, String [] output ) { Tester t = new Friend(); ArrayList al = t.compute( mkTest( input ) ); System.out.println( "Input: " ); System.out.println( input ); System.out.println( "Generated output" ); for( String s : al ) { System.out.println( s ); } System.out.println( "Expected output" ); for( String s : output ) { System.out.println( s ); } System.out.println( "---------------------------------------------------" ); return al != null && al.equals( mkOutput( output ) ); } @Test void testEmpty() { assertTrue( doTest( testInput0, testOutput0 ), "Empty test" ); } @Test void test1() { assertTrue( doTest( testInput1, testOutput1 ), "Basic recommendation" ); } @Test void test2() { assertTrue( doTest( testInput2, testOutput2 ), "Repeat recommendation" ); } @Test void test3() { assertTrue( doTest( testInput3, testOutput3 ), "Multiple recommendation" ); } @Test void test4() { assertTrue( doTest( testInput4, testOutput4 ), "Lots of users leave" ); } @Test void test5() { assertTrue( doTest( testInput5, testOutput5 ), "Lots of recommendations" ); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
