开发者

adding to a collections - inheritance

I am trying to make a project that adds cd / dvd /movie info from main() to a collections library then prints info added. like: output

-Book-
author:   Robert A. Heinlein
# pages:  325
title:    Starship Troopers
keywords: science fiction, war, weapons

-Music-
band:     five finger death punch
# songs:  15
members:  Zoltan Bathory, Ivan Moody,Jeremy Spencer,Matt Snell,Jason Hook 
title:    War is the answer
keywords: rock

I currently have 6 classes

1.project1 - main()

2.Library - where im adding to database

3.item - inheritance(title & number)

4.cd

5.dvd

6.movie

i am trying to use inheritance so i want to keep the files i have. My question is i am trying to add to the collections in the library class. I am just not sure how to do it.

here is the classes i think you will need to see..

import java.io.PrintStream;
import java.util.Collection;



public class project
{
private static Library library = new Library();

public static void main(String[] args)
{
    PrintStream out = System.out;                       // we will be printing to the standard output stream
    Item        item;



    // add items to library
    out.println(">>> adding items to library:\n");
    item = library.addBook("The Curious Incident of the Dog in the Night-Time", "Mark Haddon", 240, "autism", "Asperger's Syndrome");
    if (item != null)
        library.printItem(out, item);
    item = library.addBook("Starship Troopers", "Robert A. Heinlein", 325, "science fiction", "war", "weapons");
    if (item != null)
        library.printItem(out, item);
    item = library.addBook("The Moon Is A Harsh Mistress", "Robert A. Heinlein", 389, "science fiction", "moon", "social structures");
    if (item != null)
        library.printItem(out, item);
    item = library.addMusicCD("Europe In '72", "Grateful Dead", 12, "acid rock", "sixties", "jam bands");
    if (item != null) {
        library.addBandMembers(item, "Jerry Garcia", "Bill Kreutzman", "Keith Godcheaux");
        library.printItem(out, item);
        }
    item = library.addMusicCD("Don't Let Go", "Jerry Garcia Band", 15, "acid rock", "jam bands");
    if (item != null) {
        library.addBandMembers(item, "Jerry Garcia", "Keith Godcheaux");
        library.printItem(out, item);
        }
    item = library.addMusicCD("Sergeant Pepper's Lonely Hearts Club Band", "Beatles", 10, "acid rock", "sixties");
    if (item != null) {
        library.addBandMembers(item, "John Lennon", "George Harrison", "Ringo Starr");
        library.printItem(out, item);
        }
    item = library.addMovieDVD("Lost In Translation", "Sofia Coppola", 14, "Japan", "loneliness");
    if (item != null) {
        library.addCast(item, "Bill Murray", "Scarlett Johansson");
        library.printItem(out, item);
        }
    item = library.addMovieDVD("Groundhog Day", "Harold Ramis", 14, "newscaster", "groundhog", "time");
    if (item != null) {
        library.addCast(item, "Bill Murray", "Andie MacDowell");
        library.printItem(out, item);
        }

    // print books, musicCDs, movies
    out.println(">>> books:\n");
    printItems(out, library.books());
    out.println(">>> music CDs:\n");
    printItems(out, library.musicCDs());
    out.println(">>> movies:\n");
    printItems(out, library.movies());

    // print items for keyword
    printItemsForKeyword(out, "science fiction");
    printItemsForKeyword(out, "jam bands");
    printItemsForKeyword(out, "xxx");

    // items by artist
    out.println(">>> books by Robert A. Heinlein:\n");
    printItems(out, library.booksByAuthor("Robert A. Heinlein"));
    out.println(">>> music by the Grateful Dead:\n");
    printItems(out, library.musicByBand("Grateful Dead"));
    out.println(">>> music by the Rolling Stones:\n");
    printItems(out, library.musicByBand("Rolling Stones"));
    out.println(">>> movies by Sofia Coppola:\n");
    printItems(out, library.moviesByDirector("Sofia Coppola"));
    out.println(">>> music by Jerry Garcia:\n");
    printItems(out, library.musicByMusician("Jerry Garcia"));
    out.println(">>> movies with Bill Murray:\n");
    printItems(out, library.moviesByActor("Bill Murray"));
}

private static void printItemsForKeyword (PrintStream out, String keyword)
{
    Collection<Item>    items;

    out.printf(">>> items for keyword: %s\n\n", keyword);
    items = library.itemsForKeyword(keyword);
    printItems(out, items);
}

private static void printI开发者_运维百科tems (PrintStream out, Collection<Item> items)
{
    if (items != null && items.size() > 0)
        for (Item item : items)
            library.printItem(out, item);
    else
        out.println("none\n");
}
}

here is the library class where i am having trouble adding to the collections..

How would i add a book or a cd to the collections?

import java.io.PrintStream;
import java.util.Collection;
import java.util.*;

public class Library
{


// returns all of the items which have the specified keyword
public Collection<Item> itemsForKeyword(String keyword)
{
    return null;
}

// print an item from this library to the output stream provided
public void printItem(PrintStream out, Item item)
{
}



// adds a book to the library
public Item addBook(String title, String author, int nPages, String... keywords)
{


    return null;
}

// returns all of the books by the specified author
public Collection<Item> booksByAuthor(String author)
{
    return null;
}

// returns all of the books in the library
public Collection<Item> books()
{
    return null;
}

// music-related methods

// adds a music CD to the library
public Item addMusicCD(String title, String band, int nSongs, String... keywords)
{
    Collection MusicCollection = new HashSet();

    MusicCollection.add(title);
    return null;
}

// adds the specified band members to a music CD
public void addBandMembers(Item musicCD, String... members)
{
}

// returns all of the music CDs by the specified band
public Collection<Item> musicByBand(String band)
{
    return null;
}

// returns all of the music CDs by the specified musician
public Collection<Item> musicByMusician(String musician)
{
    return null;
}

// returns all of the music CDs in the library
public Collection<Item> musicCDs()
{
    return null;
}

// movie-related methods

// adds a movie to the library
public Item addMovieDVD(String title, String director, int nScenes, String... keywords)
{
    return null;
}

// adds the specified actors to a movie
public void addCast(Item movie, String... members)
{
}

// returns all of the movies by the specified director
public Collection<Item> moviesByDirector(String director)
{
    return null;
}

// returns all of the movies by the specified actor
public Collection<Item> moviesByActor(String actor)
{
    return null;
}

// returns all of the movies in the library
public Collection<Item> movies()
{
    return null;
}   
}

here is the items class

import java.io.PrintStream;
import java.util.Collection;
import java.util.*;

public class Item
{
private String title;
private int number;

public Item(String theTitle, int theNumber)
{
number = theNumber;
title = theTitle;

}

public String getTitle()
{
return title;
}

public int getNumber()
{
return number;
}

}

here is the cd class - the dvd class is almost identical

import java.util.*;


public class CD extends Item
{

private String artist;
private String members;


public CD(String theTitle, String theArtist, String theMembers, int number)
{
    super(theTitle,number);
    artist = theArtist;
    members = theMembers;

}


public String getArtist()
{

    return artist;

}

public String getMembers()
{
 return members;   
}

public void print()
{
    System.out.println("-Music-");
    System.out.println("band: " + artist);
}

}

I am not sure if i could combine the cd/dvd/movie classes into items class?

My main QUESTION is:

how should i add each cd/dvd to collections?????

in the library class

would i just define a collection to add in every addfunction(addMusicCD,addBandMembers,addMovieDVD,etc..) or should i put the collection in the beginning of the class? and how do i add to that collection???

public Item addMusicCD(String title, String band, int nSongs, String... keywords)
{
  Collection MusicCollection = new HashSet(); // how should i add each cd/dvd to collections?????

  MusicCollection.add(title);
  return null;
}

I am also trying to return an Item and cannot! what item would i need to return?? I know this is alot of information. Sorry. Hopefully someone can help me and not just make fun of me. i am trying to learn java from c++

Thank you for any help you can give me..


I think you need to study Object Orientation Principles a bit more.

The Library should be able to add Items. The Library methods addBook(many params) and addDVD() etc should be replaced by a more generic addItem(Item item, String... keywords).

Items can be CDs, DVDs or Movies. It's up the the CD class to add band members, not the Library class. Adding an item to the library becomes something like

CD cd = new CD("Europe In '72", "Grateful Dead", 12);
cd.addBandMembers("Jerry Garcia", "Bill Kreutzman", "Keith Godcheaux");
library.addItem(cd, "acid rock", "sixties", "jam bands"));

Hope this helps a little to get you on track.


In addMusicCD, do a new CD(the various bits).

Add the resulting CD to the collection.

Return it.

It might be easier for you to fit this all together if you used Generics to declare collections, e.g.

class Library {
    private Set<CD> theCDs;

    public Item addCD(String title) {
        CD cd = new CD(title);
        theCDS.add(cd);
        return cd;
    }
}

etc, etc. etc.


I would probably add another tree of inheritance, so a Library is an abstract subclass of e.g. HashSet:

public abstract class Library<T extends Item> extends HashSet<T>{

...

abstract void addItem(T item);

}

Then you create all your libraries by subclassing your Library-class:

public class CDLibrary extends Library<Cd>{

...

@Override
public void addItem(Cd item){
  // Maybe add some information of cd to a hashmap for lookups, or whatever
  ...
  this.add(item);
}

When you're subclassing HashSet, all the add and delete operations are done for you.

If you dont need specific add-methods for each Item, you can simply remove the abstract mathod of Library, and just take advantage of the generic-syntax, to specify a new type of library when subclassing.

You might consider subclassing ArrayList, or something similar instead, as HashSet does not have a get() method, which ArrayList does, the above code was just an example of what you could do.

If its unclear, I'll try to clarify a bit more! But I hope you get the picture - subclassing to inherit the functions you need, instead of creating them again. Generics, (do you know generics?) are to ensure type-safety, so you cannot add a DVD to a CDLibrary.

Also, be sure to override equals() and hashcode() of your Items, to make sure you can distinguish between two Items.

I hope it makes sense!


Do you need to have the CDs, DVDs, and Books into separate data structures? What's your use case? If you need to get them in a separate way, then having different data structures is OK.

Otherwise, and I think this is the case, I think you could be fine having a Set<Item> and dump all your items in it.

public static class Library{
  private Set<Item> items = new HashSet<Item>();
  public void add(Item i){
    items.add(i);
  }
  public String toString(){
    return items.toString()
  }
}

And, in your Item sub-classes, you have to have toString() overridden, and everything will print itself just fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜