Adding to collections - inheritance

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 # 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 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 { // 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 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(); // how should i add each cd/dvd to collections????? MusicCollection.add(title); return null; } 

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

+4
source share
4 answers

I think you need to learn a little more about object orientation.

The library should be able to add items. Library methods addBook (many parameters) and addDVD (), etc. Must be replaced with the more general addItem element (Item, String ... element).

Items can be CDs, DVDs, or movies. This is the CD class for adding group 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 you slip a little.

+2
source

In addMusicCD make a new CD (various bits).

Add the resulting CD to the collection.

Bring it back.

It might be easier for you to put it all together if you used Generics to announce collections, for example.

 class Library { private Set<CD> theCDs; public Item addCD(String title) { CD cd = new CD(title); theCDS.add(cd); return cd; } } 

etc. etc. etc.

0
source

I would add another inheritance tree, so the library is an abstract subclass, for example. 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 subclass a HashSet, all add and delete operations are done for you.

If you do not need special add-methods for each element, you can simply remove the abstract method of the library and simply use the generalized syntax to indicate a new type of library when subclassing.

You can consider a subclass of ArrayList or something similar instead, since the HashSet doesn't have a get () method that ArrayList does, the code above was just an example of what you could do.

If it is not clear, I will try to clarify a little more! But I hope you get the picture subcategory to inherit the functions you need, rather than creating them again. Generics, (you know generics?), To ensure type safety, so you cannot add DVDs to CDLibrary.

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

Hope this makes sense!

0
source

Need to have CDs, DVDs, and books in separate data structures? What are you using? If you need to get them separately, then the presence of different data structures is in order.

Otherwise, and I think it is, I think that you can get a Set<Item> and reset all your elements 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 subclasses, you have to override toString() and everything will print fine.

0
source

Source: https://habr.com/ru/post/1301399/


All Articles