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

Design a program in java that tries to give people their first choice. If unable

ID: 3583474 • Letter: D

Question

Design a program in java that tries to give people their first choice. If unable to do so the program should give as many lowest choice numbers as possible. In addition, this program should be dynamic so that any number choices as desired can be picked.

Example array choices (1,1,1) is the best outcome

1,1,2 is the second best

1,2,2 third

2,2,2

etc...

X people and they have three preferences

Ex.person 1 chocolate,candy,suite.

person 2 candy, cookies, pie.

person 3 candy, chocolate, pie.

person 4 candy, chocolate, pie.

person 5 candy, chocolate, cookies.

person 6 candy, chocolate, cookies.

I am trying to make it so that they all have their first choice, but there can only be x number of the same type. In this case, there can only be two of the same. If no choice is possible it should keep track of who. Perhaps a better example would be room selection.

In this example I believe the best outcome would be

Ex.person 1 chocolate.

person 2 candy

person 3 candy

person 4 chocolate

person 5 chocolate

person 6 cookies

Explanation / Answer

Assuming possible choices and preference of each person is given as input.

Code :

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

public class ChoiceSelector {

   private HashMap<String, Integer> itemList;
   private ArrayList<String[]> preferences;
   private LinkedList<Integer> unassignedPersonId;
  
   ChoiceSelector() {
       itemList = new HashMap<String, Integer>();
       preferences = new ArrayList<String[]>();
       unassignedPersonId = new LinkedList<Integer>();
   }
  
   public void setPossibleChoices(String itemName, int itemQuantity) {
       itemList.put(itemName, itemQuantity);
   }
  
   public void setPreferences(int id, String[] choices) {
       preferences.add(id, choices);
   }
  
   public void assignPreference() {
       for(int i=0; i<preferences.size(); i++) { //for each person
           boolean assigned = false;
           String[] temp = preferences.get(i);  
               for(int j = 0; j<temp.length; j++) { //from their preference list
                   if (itemList.get(temp[j]) > 0) {
                       System.out.println("For person " + i + " available choice is " + temp[j]);
                       itemList.put(temp[j], (itemList.get(temp[j])) - 1);
                       assigned = true;
                       break;
                   }
               }
               if(assigned == false)
                   unassignedPersonId.add(i); //not assigned any preference
       }
       System.out.print("Could not be assigned to : ");
       for(int i = 0; i<unassignedPersonId.size(); i++)
           System.out.print(unassignedPersonId.get(i) + " ");
       System.out.println();
   }
  
   public static void main(String[] args) {
       ChoiceSelector c = new ChoiceSelector();
       String[] items = {"chocolate", "cookies", "candy", "peanuts", "toffee", "hazelnut", "wafer", "pie"};
       int perItemQuantity = 2; //assuming same, can be different for each item
      
       String[] person0Choices = {"hazelnut", "pie", "toffee"};
       String[] person1Choices = {"candy", "cookies", "pie", "toffee"};
       String[] person2Choices = {"candy", "cookies", "peanuts", "toffee", "hazelnut"};
       String[] person3Choices = {"candy", "cookies", "pie", "toffee"};
       String[] person4Choices = {"cookies", "pie", "toffee"};
       String[] person5Choices = {"candy", "cookies", "hazelnut", "toffee"};
       String[] person6Choices = {"hazelnut", "cookies", "candy", "wafer", "pie", "toffee"};
       String[] person7Choices = {"hazelnut", "cookies", "candy"};
       //int totalPersons = 7;
      
       //Set possible items
       for(String s : items)
           c.setPossibleChoices(s, perItemQuantity);
      
       //Set preferences for each person
       c.setPreferences(0, person0Choices);
       c.setPreferences(1, person1Choices);
       c.setPreferences(2, person2Choices);
       c.setPreferences(3, person3Choices);
       c.setPreferences(4, person4Choices);
       c.setPreferences(5, person5Choices);
       c.setPreferences(6, person6Choices);
       c.setPreferences(7, person7Choices);
      
       //assigned choice
       c.assignPreference();
   }
}

Output :

For person 0 available choice is hazelnut
For person 1 available choice is candy
For person 2 available choice is candy
For person 3 available choice is cookies
For person 4 available choice is cookies
For person 5 available choice is hazelnut
For person 6 available choice is wafer
Could not be assigned to : 7

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