Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rewrite The Following Java Class so that it performs the same as before but is m

ID: 3737768 • Letter: R

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 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

Junit Test Class

Explanation / Answer

There are multiple ways to re-write the above code in an effective, easy and efficient way. Some of the ways are listed below. we can follow any of the given approach:-

And this with another switch statement:-

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
}

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
}
}

Hope this will help!!! There seems to be no change in logic as the logic is already simplified and apt.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote