4.编写一个类Book,具有name,price,press(出版社),author 然后创建5个对象放入ArrayList中,并实现按照price大小排序,然后遍历ArrayList输出每个Book

public class Book implements Comparable<Book> {
    private String name;
    private int   price;
    private String press;
    private String author;

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", press='" + press + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getPress() {
        return press;
    }

    public void setPress(String press) {
        this.press = press;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Book(String name, int price, String press, String author) {
        this.name = name;
        this.price = price;
        this.press = press;
        this.author = author;
    }


    @Override
    public int compareTo(Book o) {
        return o.price - this.price;
    }

    public static void main(String[] args) {
        List<Book> books = new ArrayList();
        books.add(new Book("西游记", 20, "郑州出版社", "吴承恩"));
        books.add(new Book("水浒传", 24, "郑州出版社", "施耐庵"));
        books.add(new Book("三国演义", 21, "郑州出版社", "罗贯中"));
        books.add(new Book("红楼梦", 26, "郑州出版社", "曹雪芹"));
        books.add(new Book("平凡的世界", 30, "郑州出版社", "路遥"));
        System.out.println(books);
        for (Book book:books){
            System.out.println(book);
        }
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值