import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeSet;
public class Main {
public static void main
(String[] args
) {
Allegro allegro = new Allegro();
allegro.add(new Monitor("Dell", 65, 5000));
allegro.add(new Monitor("Apple", 9.5, 6000));
allegro.add(new Monitor("Dell", 2.5, 100));
allegro.add(new Monitor("Samsung", 5.5, 4000));
allegro.add(new Monitor("Dell", 1.5, 3000));
allegro.add(new Monitor("Intel", 0.5, 2000));
System.
out.
println("Cenami\n");
allegro.Cenami();
System.
out.
println("------------------------------------\n");
System.
out.
println("Rozdzielczosciami:\n"); allegro.Rozdz();
}
}
/* implements Comparable<Monitor>*/
class Monitor implements Comparable<Monitor>
{
private double resolution;
private double cena;
public Monitor() {
this.firma = "nieznana";
this.resolution = 0.0;
this.cena = 0;
}
public Monitor
(String firma,
double resolution,
double cena
) { this.firma = firma;
this.resolution = resolution;
this.cena = cena;
}
return this.firma;
}
double getCena() {
return this.cena;
}
double getResolution() {
return this.resolution;
}
void Show() {
System.
out.
println("Monitor: " + this.
firma + " Rozdzielczosc: " + this.
resolution + " Cena: " + this.
cena); }
@Override
public int compareTo(Monitor o) {
if(this.cena>o.cena)
{
return 1;
}
else if(this.cena<o.cena)
{
return -1;
}
else
{
if(this.resolution>o.resolution)
{
return 1;
}
else if(this.resolution<o.resolution)
{
return -1;
}
else
{
return 0;
}
}
}
}
class Allegro {
// LinkedList<Monitor> list;
Iterator<Monitor> iterator;
TreeSet<Monitor> treeset;
public Allegro() {
this.treeset = new TreeSet<>();
}
void add(Monitor monitor) {
this.treeset.add(monitor);
}
void Cenami() {
iterator = treeset.iterator();
while (iterator.hasNext()) {
iterator.next().Show();
}
}
void Rozdz() {
//Collections.sort(lista);
class Rozdzielczosciami implements Comparator<Monitor> {
@Override
public int compare(Monitor o1, Monitor o2) {
if (o1.getResolution() > o2.getResolution())
{
return 1;
}
else if (o1.getResolution() < o2.getResolution())
{
return -1;
}
else
{
return 0;
}
}
}
iterator = treeset.iterator();
while (iterator.hasNext()) {
iterator.next().Show();
}
}
}