Я пытаюсь создать проект, который добавляет информацию о cd / dvd / movie из main () в библиотеку коллекций.
затем распечатывает добавленную информацию.
как: вывод
-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
У меня сейчас 6 классов
1.project1 - main ()
2.Library - где я добавляю в базу данных
3.элемент - наследование (название и номер)
4.cd
5.dvd
6.movie
Я пытаюсь использовать наследование, поэтому я хочу сохранить файлы, которые у меня есть.
У меня вопрос, я пытаюсь добавить в коллекции в классе библиотеки. Я просто не уверен, как это сделать.
вот классы, которые, я думаю, вам нужно увидеть ..
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 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");
}
}
вот библиотечный класс, в котором у меня возникают проблемы с добавлением в коллекции ..
Как добавить книгу или компакт-диск в коллекции?
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;
}
}
вот класс предметов
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;
}
}
вот класс cd - класс dvd практически идентичен
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);
}
}
Я не уверен, смогу ли я объединить классы cd / dvd / movie в класс предметов?
Мой главный вопрос:
как мне добавить каждый CD / DVD в коллекции ?????
в классе библиотеки
я бы просто определил коллекцию для добавления в каждую addfunction (addMusicCD, addBandMembers, addMovieDVD и т. Д.) Или я должен поместить коллекцию в начало класса? и как мне добавить в эту коллекцию ???
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;
}
Я также пытаюсь вернуть предмет и не могу! какой предмет мне нужно вернуть ??
Я знаю, что это много информации. Сожалею.
Надеюсь, кто-то может помочь мне, а не просто высмеивать меня я пытаюсь научиться Java с C ++
Спасибо за любую помощь, которую вы можете мне дать ..