2014-01-20 40 views
1

如何按字母順序排列我的inventory數組?如何在Java中按字母順序排列自定義對象?

這是針對我正在開發的一個項目。我試圖使用Arrays.sort(inventory);,但它沒有奏效。

有人能告訴我我做錯了什麼嗎?

Book類:

//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 

圖書城類(主要方法):

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 
+0

創建['Comporator'](http://docs.oracle.com/javase/7/docs/ api/java/util/Comparator.html)作爲'Book'類。請參閱http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html –

+0

@ PM77-1:爲什麼選擇「Comparator」?用'Arrays.sort()'來比較'Comparable'會更容易/更直接/實際上**工作**? – Makoto

+2

執行'Comparator'讓他有不同的'Comparator's,所以他可以有不同的排序策略。這也讓他不要改變領域模型(他允許?)。 –

回答

4

Arrays.sort()使用自然排序的對象的,所以這意味着你需要他們來實施或者你會得到不可靠的排序行爲。

簡單地說,如果你想每本書的標題進行排序,然後把它落實Comparable

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

你想也想做同樣的子類EBook爲好。

0

您不能通過直接將該類放入array.sort來排序類的字符串屬性。 如果使用數組列表而不是數組,那麼可以使用Collection.sort()對書對象內部的列表使用字段進行排序。

公共靜態無效的主要(字串[] args){

// book array 

ArrayList<Book> inventory = new ArrayList<Book>(); 

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

Collections.sort(inventory, new Comparator<Book>() { 
    public int compare(Book result1, Book result2) { 
     return result1.getTitle().compareTo(result2.getTitle()); 
    } 
}); 

for (int i =0;i<inventory.size();i++){ 
    System.out.println(inventory.get(i).getTitle()); 
} 

}

相關問題