class MaximumContainerProg2 {
public static void main (String [] args) {
MaximumContainer <Apple> ap = new MaximumContainer <Apple> (new Apple(4));
MaximumContainer <Orange> or = new MaximumContainer <Orange> (new Orange(8));
MaximumContainer <Pear> pr = new MaximumContainer <Pear> (new Pear(4));
ap.print();
ap.put(new Apple(2));
ap.print();
ap.put(new Apple(3));
ap.print();
ap.put(new Apple(2));
ap.print();
ap.put(new Apple(2));
ap.print();
ap.put(new Apple(3));
ap.print();
ap.put(new Apple(5));
ap.print();
System.out.println (ap.greater(or.get()));
}
static abstract class Fruit implements Comparable {
int val;
void test() {
}
@Override
public int compareTo (Object o) {
Fruit i = (Fruit) o;
if (this.val > i.val)
return 1;
else if (this.val < i.val)
return -1;
return 0;
}
@Override
public String toString () {
return Integer.toString(this.val);
}
public void store (int val) {
this.val = val;
}
}
static class Apple extends Fruit {
Apple (int i) {
store (i);
}
}
static class Pear extends Fruit {
Pear (int i) {
store (i);
}
}
static class Orange extends Fruit {
Orange (int i) {
store (i);
}
}
public static class MaximumContainer <P extends Fruit> {
P val;
MaximumContainer (P val) {
this.val = val;
}
void put (P val) {
if (this.val.compareTo(val) < 0)
this.val = val;
}
P get () {
return this.val;
}
void print () {
System.out.println (val.toString());
}
int greater(Fruit des) {
return (this.val.compareTo (des));
}
}
}