Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Car implements Comparable {
- private String model;
- private double price;
- private int maxSpeed;
- public Car(String model, double price, int maxSpeed) {
- this.model = model;
- this.price = price;
- this.maxSpeed = maxSpeed;
- }
- public String getModel() {
- return model;
- }
- public void setModel(String model) {
- this.model = model;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public int getMaxSpeed() {
- return maxSpeed;
- }
- public void setMaxSpeed(int maxSpeed) {
- this.maxSpeed = maxSpeed;
- }
- @Override
- public String toString() {
- return "Car [model=" + model + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
- }
- @Override
- public int compareTo(Object obj) {
- Car other = (Car)obj;
- if (this.maxSpeed > other.maxSpeed) {
- return -1;
- } else if (this.maxSpeed < other.maxSpeed) {
- return 1;
- }
- return 0;
- }
- }
- import java.util.ArrayList;
- import java.util.Collections;
- public class Program {
- public static void main(String[] args) {
- ArrayList<Car> cars = new ArrayList<Car>();
- cars.add(new Car("Mercedes-Benz", 50055.3, 230));
- cars.add(new Car("Lada Niva", 5005.3, 330));
- cars.add(new Car("Opel Astra", 15005.3, 200));
- Collections.sort(cars);
- for (Car c : cars) {
- System.out.println(c);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement