I try to create a project that adds the cd / dvd / movie information from main () to the collection library then prints the information. for example: output
-Book- author: Robert A. Heinlein
I currently have 6 classes
1.project1 - main ()
2.Library - where im adding to the database
3.item - inheritance (name and number)
4.cd
5.dvd
6.movie
I am trying to use inheritance, so I want to save the files that I have. My question is: I'm trying to add to collections in a class library. I'm just not sure how to do this.
here are the classes that 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 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 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 printItems (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 a library class in which i am having problems adding to collections.
How to add a book or CD to the collection?
import java.io.PrintStream; import java.util.Collection; import java.util.*; public class Library {
here is the item 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'm not sure if I can combine the cd / dvd / movie classes into an item class?
My main question is:
How do I add each cd / dvd to the collection ?????
in the library class
Would I just define the collection to add to each addfunction (addMusicCD, addBandMembers, addMovieDVD, etc.), or should I put the collection at the top of the class? and how can I add to this collection ???
public Item addMusicCD(String title, String band, int nSongs, String... keywords) { Collection MusicCollection = new HashSet();
I am also trying to return the Item and cannot! Which item will I need to return? I know this is a lot of information. I'm sorry. I hope someone can help me, and not just mock me. I am trying to learn Java from C ++
Thanks for any help you can give me ..