如何在Java数组中按字母顺序对自定义对象进行排序?

Madmardagin

我如何inventory按字母顺序数组进行排序?

这是我正在从事的项目。我尝试使用,Arrays.sort(inventory);但是没有

有人可以告诉我我做错了吗?

书本类:

//Filename: BookStore.java
//Description: Purpose is to display what books are in stock and ISBN, title, author name, year, publishers name, and price.
//Author Name: David Finocchi
//Date: 01/19/2014
import java.util.Arrays;
import java.text.DecimalFormat;

class Book {
    protected long isbn;
    protected String title;
    protected String autName;
    protected int year;
    protected String pubName;
    protected double price;
    protected int quantity;


    // Constructor
    public Book(long isbn, String title, String autName, int year, String pubName, double price) {
        setIsbn(isbn);
        setTitle(title);
        setAut(autName);
        setYear(year);
        setPub(pubName);
        setPrice(price);
    }

    // methods for class variables
    public long getIsbn() {
        return isbn;
    }

    public void setIsbn(long isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAut() {
        return autName;
    }

    public void setAut(String autName) {
        this.autName = autName;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getPub() {
        return pubName;
    }

    public void setPub(String pubName) {
        this.pubName = pubName;
    }

    public double getPrice() {
        return price;
    }

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

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double calTotal() {
        return price;
    }

    //toString for the book array
    public String toString() {
        return "ISBM: " + isbn + "\n" + "Title: " + title + "\n" + "Author's Name: " + autName + "\n" + "Year Published: " + year + "\n" + "Publisher's Name: " + pubName + "\n" + "Price: $" + price + "\n\n";
    }

}// end class Book

电子书类:

class EBook extends Book {

    private String website;

    //constructor and super
    public EBook(long isbn, String title, String autName, int year,
                 String pubName, double price, String website) {
        super(isbn, title, autName, year, pubName, price);
        setWebsite(website);
    }

    // get and set
    public String getWebsite() {
        return website;
    }


    public void setWebsite(String website) {
        this.website = website;
    }

    public double discount() {
        return price * 0.10;
    }

    DecimalFormat money = new DecimalFormat("$0.00");

    //toString for the Book array
    public String toString() {
        return "ISBM: " + isbn + "\n" + "Title: " + title + "\n" + "Author's Name: " + autName + "\n" + "Year Published: " + year + "\n" + "Publisher's Name: " + pubName + "\n" + "Price: " + money.format(price) + "\n" + "Website: " + website + "\n" + "Discount: " + money.format(discount()) + "\n\n";
    }

}//end class EBook

BookStore类(使用main方法):

public class BookStore {
    //main method
    public static void main(String[] args) {
        // book array
        Book[] inventory = new Book[5];

        EBook a = new EBook(75260012L, "David goes to School", "David Shannon", 2010, "Shannon Rock", 11.98, "http://www.tinyurl.qqwert67o9");
        Book b = new Book(7423540089L, "No David!", "David Shannon", 2009, "Shannon Rock", 12.99);
        Book c = new Book(743200616L, "Simple Abundance", "Sarah Breathnach", 2009, "Scribner", 14.99);
        EBook d = new EBook(78137521819L, "The very hungry caterpillar", "Eric Carle", 2005, "Philomel Books", 13.99, "http://www.tinyurl.fguopt8u90");
        Book e = new Book(9781416987116L, "We're going on a bear hunt", "Michael Rosen", 2009, "McElderry", 15.99);

        inventory[0] = a;
        inventory[1] = b;
        inventory[2] = c;
        inventory[3] = d;
        inventory[4] = e;

        double total = 0.0;

        //print text to user
        System.out.println("Bookstore Items:" + "\n");
        //print array
        for (int i = 0; i < inventory.length; i++) {
            System.out.println(inventory[i]);
            total += inventory[i].getPrice();
        }
        //print total
        DecimalFormat money = new DecimalFormat("$0.00");
        System.out.printf("Total Inventory Value: " + money.format(total));
    }

}// end class BookStore

Arrays.sort()使用对象自然顺序,因此这意味着您需要实现它们Comparable,否则您将获得不可靠的排序行为。

简而言之,如果您想按书名对每本书进行排序,则可以实现Comparable

public class Book implements Comparable<Book> {
    public int compareTo(Book otherBook) {
        return title.compareTo(otherBook.getTitle());
    }
}

您还希望对子类也执行相同的操作EBook

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何按自定义顺序对值进行排序

使用字母数字值对Java中的自定义对象列表进行排序

Swift如何按属性值对自定义对象数组进行排序

如何在Node JS中按字母顺序对对象数组进行排序?

按日期快速对自定义对象数组进行排序

如何在Java中按字母顺序对字符串数组进行排序?

在Eloquent中按自定义顺序对集合进行排序

如何使用自定义模型类按节按字母顺序对tableView中的数据排序?

如何使用Java 8中的自定义Comparator接口对对象数组进行排序?

如何在Laravel中按字母顺序对记录进行排序

按自定义顺序对对象数组进行排序

如何在Swift 3中按自定义状态值对自定义对象数组进行排序?

ElasticSearch-定义自定义字母顺序进行排序

属性中对象的按自定义顺序排序

如何使用自定义管道在Angular中按属性值对对象数组进行排序?

如何在Power BI中按自定义日期层次结构按时间顺序对图表进行排序

自定义排序(usort)/按两个值对数组排序后,按多维索引数组中的数组值按字母顺序排序

按特定顺序对自定义对象进行排序

如何使用自定义排序顺序在angular js中对对象进行排序?

Java:按自定义顺序对Collection <Class>进行排序

按自定义顺序对php数组进行排序

对象数组的自定义排序顺序

uasort性能差,按自定义顺序对对象数组进行排序

如何在 PHP 中按字母顺序对多元素数组进行排序

按自定义字段排序对象数组,然后按字母顺序

如何使用 lodash 的 _sortBy() 根据自定义要求按字母顺序对这个列表进行排序?

如何在 angular2+ 中按字母顺序对数组进行排序、分组和划分

SwiftUI 按 id 对自定义对象数组进行排序

如何按字母顺序对这个对象数组进行排序