Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public enum Topics {
  5. POLITICS(0){
  6. public String getInfo() {
  7. StringBuilder sb = new StringBuilder();
  8. for(int i = 0; i < newss.size(); i++) {
  9. sb.append("\n" + newss.get(i));
  10. }
  11. return "" + sb;
  12. }
  13. public int getnonews() {
  14. return nonews;
  15. }
  16. },
  17. SPORT(0){
  18. public String getInfo() {
  19. StringBuilder sb = new StringBuilder();
  20. for(int i = 0; i < newss.size(); i++) {
  21. sb.append("\n" + newss.get(i));
  22. }
  23. return "" + sb;
  24. }
  25. public int getnonews() {
  26. return nonews;
  27. }
  28. },
  29. FASHION(0){
  30. public String getInfo() {
  31. StringBuilder sb = new StringBuilder();
  32. for(int i = 0; i < newss.size(); i++) {
  33. sb.append("\n" + newss.get(i));
  34. }
  35. return "" + sb;
  36. }
  37.  
  38. public int getnonews() {
  39. return nonews;
  40. }
  41. },
  42. CELEBRITIES(0){
  43. public String getInfo() {
  44. StringBuilder sb = new StringBuilder();
  45. for(int i = 0; i < newss.size(); i++) {
  46. sb.append("\n" + newss.get(i));
  47. }
  48. return "" + sb;
  49. }
  50.  
  51. public int getnonews() {
  52. return nonews;
  53. }
  54. };
  55.  
  56. int nonews;
  57. List<String> newss = new ArrayList<>();
  58.  
  59. Topics(int noofnews) {
  60. this.nonews = noofnews;
  61. }
  62.  
  63. public abstract String getInfo();
  64. public abstract int getnonews();
  65. }
  66.  
  67. public class PressAgency {
  68.  
  69. public void broadcast(Topics t, String news){
  70. t.newss.add(news);
  71. t.nonews++;
  72. }
  73.  
  74. public void subscribe(Topics t, Person p) {
  75. p.topics.add(t);
  76. }
  77.  
  78. }
  79. import java.util.ArrayList;
  80. import java.util.List;
  81.  
  82. public class Person {
  83.  
  84. private static int counter=1;
  85. int id;
  86. private String name;
  87. int length;
  88. Topics[] topic;
  89. List<Topics> topics = new ArrayList<>();
  90.  
  91. public Person(String name) {
  92. this.name = name;
  93. id=counter;
  94. counter++;
  95. }
  96.  
  97. public String getTopics() {
  98. StringBuilder sb = new StringBuilder();
  99. for(Topics to : topics) {
  100. if(to.getnonews() == 0)
  101. sb.append("\n" + to + ": " + "No news" + to.getInfo());
  102. else {
  103. sb.append("\n" + to + ": " + to.getnonews() + " news" + to.getInfo());
  104. }
  105. }
  106. return "" + sb;
  107. }
  108.  
  109. public String toString() {
  110. return name + "(id=" + id +")" + getTopics();
  111. }
  112.  
  113. }
  114.  
  115. public class Main {
  116.  
  117. public static void main(String[] args) {
  118. Person john = new Person("John");
  119. Person kate = new Person("Kate");
  120. Person bill = new Person("Bill");
  121.  
  122. PressAgency agency = new PressAgency();
  123. agency.subscribe(Topics.POLITICS, john);
  124. agency.subscribe(Topics.SPORT, john);
  125. agency.subscribe(Topics.POLITICS, kate);
  126. agency.subscribe(Topics.FASHION, kate);
  127. agency.subscribe(Topics.CELEBRITIES, bill);
  128. agency.subscribe(Topics.SPORT, bill);
  129.  
  130. agency.broadcast(Topics.POLITICS, "Obama's speech in Washington");
  131. agency.broadcast(Topics.FASHION, "Skirts get shorter this season!");
  132. agency.broadcast(Topics.SPORT, "Real-Atletico 2:1");
  133. agency.broadcast(Topics.POLITICS, "British-French summit in Paris");
  134. agency.broadcast(Topics.SPORT, "Hamilton wins in Australia");
  135. agency.broadcast(Topics.FASHION, "New handbags from Versace!");
  136.  
  137. System.out.println(bill);
  138. System.out.println();
  139. System.out.println(john);
  140. System.out.println();
  141. System.out.println(kate);
  142.  
  143. }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement