2017-03-05 94 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