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

Hi i have a problem which im stuck with. I am to write a code in JAVA and which

ID: 3737885 • Letter: H

Question

Hi i have a problem which im stuck with. I am to write a code in JAVA and which print a list of subscription in descending order depending on the number of subscription the users has. The program should only print the result if the user have subscripted to two or more season of the same showid. Some help will be appreciated thanks .

Example input:

(userid, showid, season)

4242 , 7 , 1

4242 , 7 , 3

4321 , 1 , 2

4242 , 7 , 2

4333 , 4 , 1

4333 , 4 , 2

Expected output :

(userid , showid)

4242 , 7   

4333 , 4

Explanation / Answer

========= ShowRunner ================

package com.chegg;

import java.util.*;

public class ShowRunner {

private static Map<Integer, User> userMap; //Storing user details in HashMap which uniquely identifies user by its userId

private static List<DisplayUser> displayList;

public static void main(String[] args) {

userMap = new HashMap<Integer, User>();

displayList=new ArrayList<DisplayUser>();

Scanner scanner = new Scanner(System.in);

ShowRunner showRunner = new ShowRunner();

while (true) {

System.out.println("1. Add Show 2. List Subscription 3. Exit. Enter Choice:");

int ch = scanner.nextInt();

switch (ch) {

case 1:

boolean doContinue = true;

while (doContinue) {

//Get UserId,ShowId and SeasonId

System.out.println("Enter User Id :");

int userId = scanner.nextInt();

System.out.println("Enter Show Id : ");

int showId = scanner.nextInt();

System.out.println("Enter Season Id : ");

int seasonId = scanner.nextInt();

showRunner.addShow(userId, showId, seasonId);

System.out.println("Continue Adding(Y/N)");

doContinue = scanner.next().equalsIgnoreCase("Y") ? true : false;

}

break;

case 2:

//Display Subcription List in decending Order

showRunner.displaySubscription();

break;

case 3:

System.exit(0);

default:

System.out.println("Wrong Input. Please Enter Again");

}

}

}

/**

* Add User details

* @param userId

* @param showId

* @param seasonId

*/

public void addShow(int userId, int showId, int seasonId) {

User user = userMap.get(userId);

if (user == null) {

user = new User();

user.setUserId(userId);

user.setShowDetails(showId, seasonId);

} else {

user.setShowDetails(showId, seasonId);

}

userMap.put(userId, user);

}

/**

* Display Subscriptions where users have subscribed to more than one show

*/

public void displaySubscription() {

System.out.println("UserId ShowId");

for (Integer userId : userMap.keySet()) {

Map<Integer, List<Integer>> showList = userMap.get(userId).getShowMap();

for (Integer showId : showList.keySet()) {

if (showList.get(showId).size() > 1) {

//Store User Details who have multiple season in one show

displayList.add(new DisplayUser(userId,showId));

}

}

}

Collections.sort(displayList,new DisplayComparator());

for (DisplayUser displayUser : displayList) {

System.out.println(displayUser.getUserId()+" "+displayUser.getShowId());

}

}

}

========= User Custom Object Class ================

package com.chegg;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

* User Details Object where each userID has its Map<showId,List<SeasonId>>

* each show can have multiple unique season IDs

*/

public class User {

private int userId;

private Map<Integer, List<Integer>> showMap = new HashMap<Integer, List<Integer>>();

public int getUserId() {

return userId;

}

/**

* Set User ID

* @param userId

*/

public void setUserId(int userId) {

this.userId = userId;

}

public Map<Integer, List<Integer>> getShowMap() {

return showMap;

}

/**

* While adding show ID and season ID details for a user ID check if the show ID exists

* if Show ID exists then check if season ID exists for that Show ID.

* if both cases are false then add season ID for the show ID

* else add new show ID and season ID

* @param showId

* @param seasonId

*/

public void setShowDetails(int showId, int seasonId) {

List<Integer> seasonList = this.showMap.get(showId);

if (seasonList==null)

seasonList=new ArrayList<Integer>();

if (!seasonList.contains(seasonId))

seasonList.add(seasonId);

this.showMap.put(showId, seasonList);

}

}

========= DisplayUser Custom Object Class ================

package com.chegg;

/**

* Custom Object to Store display Result for Sorting data

*/

public class DisplayUser {

private int userId;

private int showId;

public DisplayUser(int userId, int showId) {

this.userId = userId;

this.showId = showId;

}

public int getUserId() {

return userId;

}

public int getShowId() {

return showId;

}

}

========= DisplayComparator Class to Compare List of Records ================

package com.chegg;

import java.util.Comparator;

/**

* Comparator to sort the list in decreasing Order

*/

public class DisplayComparator implements Comparator<DisplayUser> {

@Override

public int compare(DisplayUser o1, DisplayUser o2) {

if (o1.getShowId()>o2.getShowId())

return -1;

else if (o1.getShowId()<o2.getShowId())

return 1;

else return 0;

}

}

========= Output ================

1. Add Show

2. List Subscription

3. Exit.

Enter Choice:

1

Enter User Id :

4242

Enter Show Id :

7

Enter Season Id :

1

Continue Adding(Y/N)

y

Enter User Id :

4242

Enter Show Id :

7

Enter Season Id :

3

Continue Adding(Y/N)

y

Enter User Id :

4321

Enter Show Id :

1

Enter Season Id :

2

Continue Adding(Y/N)

y

Enter User Id :

4242

Enter Show Id :

7

Enter Season Id :

2

Continue Adding(Y/N)

y

Enter User Id :

4333

Enter Show Id :

4

Enter Season Id :

1

Continue Adding(Y/N)

y

Enter User Id :

4333

Enter Show Id :

4

Enter Season Id :

2

Continue Adding(Y/N)

n

1. Add Show

2. List Subscription

3. Exit.

Enter Choice:

2

UserId ShowId

4242 7

4333 4

1. Add Show

2. List Subscription

3. Exit.

Enter Choice:

3

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