2017-03-05 92 views
0

所以我有兩個類,項目類而這從項目擴展Book類。理論上,Book對象也應該被視爲Item對象。我有一個書店有一個集合,並希望存儲書籍。 它給這個錯誤: 類型不匹配:不能從收集項目轉換爲藏書類型不匹配無法一種類型轉換爲另一種

我如何使它所以它識別書籍項目也是如此。應該能夠替代項目a =新書();謝謝!

圖書城等級:

public class Book extends Item { 
    private Author author; 
    private Date datePublished; 
    private String title; 
    private BookType genre; 

public Book(double weightKg, double manufacturingPriceDollars, double suggestedRetailPriceDollars, String uniqueID, 
     Author author, Date datePublished, String title, BookType genre) { 
    super(weightKg, manufacturingPriceDollars, suggestedRetailPriceDollars, uniqueID); 
    this.author = author; 
    this.datePublished = datePublished; 
    this.title = title; 
    this.genre = genre; 
} 

Item類是這樣的:

public class BookStore extends Store { 
private BookType specialty; 

public BookStore(Address address, String name, BookType specialty) { 
    super(address, name); 
    this.setSpecialty(specialty); 
    addBooks(); 
} 

public void displayAllBooksWrittenByAuthorsOverThisAge(int ageInYears) { 
    Collection<Book> books = getCollectionOfItems(); // error type mismatch cannot convert from Collection<Item to Collection<Book> 
    Iterator<Book> it = books.iterator(); 
    boolean displayedSome = false; 
    while (it.hasNext()) { 
     Book b = it.next(); 
     int ageYears = b.getDatePublished().getYear() - b.getAuthor().getBirthDate().getYear(); 
     if (ageYears > ageInYears) { 
      System.out.println(b.getTitle() + " was written by " + b.getAuthor().getName().getLastName() 
        + " at age " + ageYears + ", which is more than " + ageInYears); 
      displayedSome = true; 
     } 
    } 
    if (displayedSome == false) { 
     System.out.println("No books by authors over age " + ageInYears); 
    } 
} 

Book類這是

public class Item { 

private double weightKg; 
private double manufacturingPriceDollars; 
private double suggestedRetailPriceDollars; 
private String uniqueID; 

public Item(double weightKg, double manufacturingPriceDollars, double suggestedRetailPriceDollars, 
     String uniqueID) { 

    this.weightKg = weightKg; 
    this.manufacturingPriceDollars = manufacturingPriceDollars; 
    this.suggestedRetailPriceDollars = suggestedRetailPriceDollars; 
    this.uniqueID = uniqueID; 
} 

商店類別:

public class Store { 
private Address streetAddress; // instance variable of the street address. 
private String name; // name of the store. 
private HashMap<String, Item> itemsForSale; // store's products for sale. 

/** 
* Constructor of Store. 
*/ 
public Store(Address streetAddress, String name) { 
    this.streetAddress = streetAddress; 
    this.name = name; 
    itemsForSale = new HashMap<>(); 
} 
public void addItem(Item item) { 
    itemsForSale.put(item.getUniqueID(), item); 
} 

/** 
* method that gets the item when entering key 
* 
* @param key 
* @return itemsForSale.get(key) 
*/ 
public Item getItemByKey(String key) { 
    return itemsForSale.get(key); 
} 

/** 
* method that returns back all the items 
* 
* @return itemsForSale.value() 
*/ 
public Collection<Item> getCollectionOfItems() { 
    return itemsForSale.values(); 
} 
+3

你在哪兒你的錯誤到底?張貼一致的堆棧跟蹤。 – pzaenger

+0

我在 public void displayAllBooksWrittenByAuthorsOverThisAge(int ageInYears)方法中得到錯誤。在\t \t收藏書= getCollectionOfItems(); – Bomiheko

+0

請發佈一個[MCVE] – c0der

回答

2

BookStore類,您呼叫

Collection<Book> books = getCollectionOfItems(); 

返回Item注的集合Book可鑄造成Item而不是倒過來。所以,你需要改變上述

Collection<Item> books = getCollectionOfItems(); 

然後,如果您想顯示所有書籍,遍歷所有項目,並檢查當前的項目是一本書,顯示它之前,

public void displayAllBooksWrittenByAuthorsOverThisAge(int ageInYears) { 
    Iterator<Item> items = getCollectionOfItems().iterator(); 
    while (it.hasNext()) { 
     Item item = it.next(); 
     if(item instanceof Book){ 
     displayBook((Book)item); 
     } 
    } 
} 
+0

是的,我現在將其更改爲,它似乎工作,但是我從我的教授提供的任務中複製了此方法,因此我不確定是否可以更改他的代碼。 – Bomiheko

+0

IMO'getCollectionOfItems'將被命名爲'getCollectionOfBooks',如果它是返回'書籍'而不是'項目'的集合。 –

+0

根據教授的說法,我無法更改他的代碼,所以'getCollectionOfItems'保留。 'getCollectionOfItems'和'displayAllBookWrittenByAuthorOverThisAge'方法已經由prof創建,我必須使用它。所以這意味着Item或Book類都有問題。它不會正確地將Book作爲項目。 – Bomiheko