fork download
  1. import java.util.Collections;
  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.LinkedList;
  5. import java.util.TreeSet;
  6.  
  7.  
  8. public class Main {
  9.  
  10.  
  11. public static void main(String[] args) {
  12.  
  13.  
  14. Allegro allegro = new Allegro();
  15.  
  16. allegro.add(new Monitor("Dell", 65, 5000));
  17. allegro.add(new Monitor("Apple", 9.5, 6000));
  18. allegro.add(new Monitor("Dell", 2.5, 100));
  19. allegro.add(new Monitor("Samsung", 5.5, 4000));
  20. allegro.add(new Monitor("Dell", 1.5, 3000));
  21. allegro.add(new Monitor("Intel", 0.5, 2000));
  22.  
  23.  
  24. System.out.println("Cenami\n");
  25.  
  26. allegro.Cenami();
  27. System.out.println("------------------------------------\n");
  28.  
  29.  
  30. System.out.println("Rozdzielczosciami:\n");
  31. allegro.Rozdz();
  32. }
  33.  
  34. }
  35.  
  36.  
  37.  
  38. /* implements Comparable<Monitor>*/
  39. class Monitor implements Comparable<Monitor>
  40. {
  41. private String firma;
  42. private double resolution;
  43. private double cena;
  44.  
  45. public Monitor() {
  46. this.firma = "nieznana";
  47. this.resolution = 0.0;
  48. this.cena = 0;
  49. }
  50.  
  51. public Monitor(String firma, double resolution, double cena) {
  52. this.firma = firma;
  53. this.resolution = resolution;
  54. this.cena = cena;
  55. }
  56.  
  57. String getFirma() {
  58. return this.firma;
  59. }
  60.  
  61. double getCena() {
  62. return this.cena;
  63. }
  64.  
  65. double getResolution() {
  66. return this.resolution;
  67. }
  68.  
  69. void Show() {
  70. System.out.println("Monitor: " + this.firma + " Rozdzielczosc: " + this.resolution + " Cena: " + this.cena);
  71. }
  72.  
  73.  
  74. @Override
  75. public int compareTo(Monitor o) {
  76.  
  77. if(this.cena>o.cena)
  78. {
  79. return 1;
  80. }
  81.  
  82. else if(this.cena<o.cena)
  83. {
  84. return -1;
  85. }
  86. else
  87. {
  88. if(this.resolution>o.resolution)
  89. {
  90. return 1;
  91. }
  92.  
  93. else if(this.resolution<o.resolution)
  94. {
  95. return -1;
  96. }
  97. else
  98. {
  99. return 0;
  100. }
  101. }
  102. }
  103. }
  104.  
  105.  
  106.  
  107.  
  108. class Allegro {
  109. // LinkedList<Monitor> list;
  110. Iterator<Monitor> iterator;
  111. TreeSet<Monitor> treeset;
  112.  
  113. public Allegro() {
  114. this.treeset = new TreeSet<>();
  115. }
  116.  
  117. void add(Monitor monitor) {
  118. this.treeset.add(monitor);
  119. }
  120.  
  121.  
  122. void Cenami() {
  123. iterator = treeset.iterator();
  124. while (iterator.hasNext()) {
  125. iterator.next().Show();
  126. }
  127.  
  128. }
  129.  
  130. void Rozdz() {
  131.  
  132.  
  133. //Collections.sort(lista);
  134.  
  135.  
  136. class Rozdzielczosciami implements Comparator<Monitor> {
  137.  
  138. @Override
  139. public int compare(Monitor o1, Monitor o2) {
  140. if (o1.getResolution() > o2.getResolution())
  141. {
  142. return 1;
  143. }
  144. else if (o1.getResolution() < o2.getResolution())
  145. {
  146. return -1;
  147. }
  148. else
  149. {
  150. return 0;
  151. }
  152.  
  153. }
  154.  
  155.  
  156. }
  157. iterator = treeset.iterator();
  158. while (iterator.hasNext()) {
  159. iterator.next().Show();
  160. }
  161.  
  162. }
  163.  
  164. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
Cenami

Monitor: Dell Rozdzielczosc: 2.5 Cena: 100.0
Monitor: Intel Rozdzielczosc: 0.5 Cena: 2000.0
Monitor: Dell Rozdzielczosc: 1.5 Cena: 3000.0
Monitor: Samsung Rozdzielczosc: 5.5 Cena: 4000.0
Monitor: Dell Rozdzielczosc: 65.0 Cena: 5000.0
Monitor: Apple Rozdzielczosc: 9.5 Cena: 6000.0
------------------------------------

Rozdzielczosciami:

Monitor: Dell Rozdzielczosc: 2.5 Cena: 100.0
Monitor: Intel Rozdzielczosc: 0.5 Cena: 2000.0
Monitor: Dell Rozdzielczosc: 1.5 Cena: 3000.0
Monitor: Samsung Rozdzielczosc: 5.5 Cena: 4000.0
Monitor: Dell Rozdzielczosc: 65.0 Cena: 5000.0
Monitor: Apple Rozdzielczosc: 9.5 Cena: 6000.0