集册 Java实例教程 实现可比接口

实现可比接口

欢马劈雪     最近更新时间:2020-01-02 10:19:05

358
实现可比较接口

public class Item implements Comparable {

    private String id;

    private String name;

    private double retail;
    /* 
    *来 自
     NowJava.com
    */

    private int quantity;

    private double price;


    Item(String idIn, String nameIn, String retailIn, String quanIn) {

        id = idIn;

        name = nameIn;

        retail = Double.parseDouble(retailIn);

        quantity = Integer.parseInt(quanIn);


        if (quantity > 400)

            price = retail * .5D;
            /*
            来 自*
             N  o w  J a v a . c o m
            */

        else if (quantity > 200)

            price = retail * .6D;

        else

            price = retail * .7D;

        price = Math.floor( price * 100 + .5 ) / 100;

    }


    public int compareTo(Object obj) {

        Item temp = (Item)obj;

        if (this.price < temp.price)

            return 1;

        else if (this.price > temp.price)

            return -1;

        return 0;

    }


    public String getId() {

        return id;

    }


    public String getName() {

        return name;

    }


    public 
展开阅读全文