Guest User

Untitled

a guest
Oct 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. public class Person {
  2. private String category;
  3. private String pref;
  4. private int id;
  5.  
  6. /**
  7. * The strings a,b will be A for A, B for B and A,B for Both
  8. * @param a expressing the category that they want to go
  9. * @param b expressing the preference
  10. */
  11. Person(int id,String a,String b){
  12. this.id=id;
  13. category=a;
  14. pref=b;
  15. }
  16.  
  17. public String getCategory(){
  18. return category;
  19. }
  20.  
  21. public String getPref(){
  22. return pref;
  23. }
  24.  
  25. public int getId(){
  26. return id;
  27. }
  28.  
  29. import java.io.BufferedReader;
  30. import java.io.FileNotFoundException;
  31. import java.io.FileReader;
  32. import java.io.IOException;
  33. import java.util.ArrayList;
  34.  
  35. public class Pairing {
  36.  
  37. private ArrayList<Person> categoryA;
  38. private ArrayList<Person> categoryB;
  39. private ArrayList<Person> categoryAB;
  40. private ArrayList<Person> queue;
  41. private ArrayList<String> matches;
  42. private int ids;
  43.  
  44. public Pairing() {
  45. categoryA = new ArrayList<>();
  46. categoryB = new ArrayList<>();
  47. categoryAB = new ArrayList<>();
  48. queue = new ArrayList<>();
  49. ids = 0;
  50.  
  51. }
  52.  
  53. public void addPerson(String category, String preference) {
  54. if (category.equals("A")) {
  55. categoryA.add(new Person(ids, category, preference));
  56. } else if (category.equals("B")) {
  57. categoryB.add(new Person(ids, category, preference));
  58. } else {
  59. categoryAB.add(new Person(ids, category, preference));
  60. }
  61. queue.add(new Person(ids, category, preference));
  62. ids++;
  63.  
  64. }
  65.  
  66. public ArrayList<String> getMatches() {
  67. matches = new ArrayList<>();
  68. System.out.println(ids);
  69. checkFor2Points();
  70. checkFor1Point();
  71. //matchTheRest();
  72. return matches;
  73. }
  74.  
  75. /**
  76. * At the begin matches the persons who has strict preferences and belongs
  77. * in the first 2 categories, then uses the flexibility that A,B category
  78. * provides to find the rest matches that will give 2 points.
  79. */
  80. private void checkFor2Points() {
  81. Person c, p;
  82. for (int j = 0; j < queue.size(); j++) {
  83. p = queue.get(j);
  84. System.out.println(p.getCategory() + " " + p.getPref());
  85. if ((p.getCategory().equals("A") || p.getCategory().equals("B")) && (p.getPref().equals("A") || p.getPref().equals("B"))) {
  86. for (int i = 0; i < queue.size(); i++) {
  87. c = queue.get(i);
  88. if (c.getPref().equals(p.getCategory()) && c.getCategory().equals(p.getPref()) && c.getId() != p.getId()) {
  89. matches.add(p.getId() + 1 + "+" + (c.getId() + 1));
  90. queue.remove(c);
  91. queue.remove(p);
  92. } else if (c.getCategory().equals("A,B") && c.getPref().equals(p.getCategory()) && c.getId() != p.getId()) {
  93. matches.add(p.getId() + 1 + "+" + (c.getId() + 1));
  94. queue.remove(c);
  95. queue.remove(p);
  96. }
  97. }
  98. }
  99. }
  100. for (int j = 0; j < queue.size(); j++) {
  101. p = queue.get(j);
  102. if (p.getPref().equals("A,B")) {
  103. for (int i = 0; i < queue.size(); i++) {
  104. c = queue.get(i);
  105. if (c.getPref().equals(p.getCategory()) && c.getId() != p.getId()) {
  106. matches.add(p.getId() + 1 + "+" + (c.getId() + 1));
  107. queue.remove(c);
  108. queue.remove(p);
  109. }
  110. }
  111. }
  112. }
  113.  
  114. }
  115.  
  116. private void checkFor1Point() {
  117. Person c, p;
  118. for (int j = 0; j < queue.size(); j++) {
  119. p = queue.get(j);
  120. for (int i = 0; i < queue.size(); i++) {
  121. c = queue.get(i);
  122. if ((p.getCategory().equals(c.getPref()) || p.getPref().equals(c.getCategory())) && p.getId() != c.getId()) {
  123. matches.add(p.getId() + 1 + "+" + (c.getId() + 1));
  124. queue.remove(c);
  125. queue.remove(p);
  126. }
  127. }
  128. }
  129.  
  130. }
  131.  
  132. private void matchTheRest() {
  133. for (int i = 0; i < queue.size(); i += 2) {
  134. matches.add(queue.get(i).getId() + "+" + queue.get(i + 1).getId());
  135. queue.remove(queue.get(i));
  136. queue.remove(queue.get(i + 1));
  137. if (queue.size() == 1) {// that means that the ids is an odd number
  138. return;
  139. }
  140. }
  141. }
  142.  
  143. /**
  144. * @param args the command line arguments
  145. * @throws java.io.FileNotFoundException
  146. */
  147. public static void main(String[] args) throws FileNotFoundException, IOException {
  148. String getLineString;
  149. String toSplit[];
  150. BufferedReader in = new BufferedReader(new FileReader("a.txt"));
  151. Pairing pair = new Pairing();
  152. getLineString = in.readLine();
  153. while (getLineString != null) {
  154. toSplit = getLineString.split(" -> ");
  155. pair.addPerson(toSplit[0], toSplit[1]);
  156. getLineString = in.readLine();
  157. }
  158. ArrayList<String> pairs = pair.getMatches();
  159. for (int i = 0; i < pairs.size(); i++) {
  160. System.out.println(pairs.get(i));
  161. }
  162.  
  163. }
Add Comment
Please, Sign In to add comment