1
Versi 2.
/**
* The Itemclass represents amulti-mediaitem.
* Informationaboutthe itemisstoredandcan be retrieved.
* Thisclass servesasa superclassformore specificitms.
*
* @author Michael KollingandDavidJ.Barnes
* @version2008.03.30
*/
publicclassItem
{
private Stringtitle;
private intplayingTime;
private booleangotIt;
private Stringcomment;
/**
* Initialisethe fieldsof the item.
* @param theTitle The title of thisitem.
* @param time The runningtime of thisitem.
*/
publicItem(StringtheTitle,inttime)
{
title =theTitle;
playingTime=time;
gotIt= false;
comment= "";
}
2
/**
* Enter a commentforthisitem.
* @param commentThe commentto be entered.
*/
publicvoidsetComment(Stringcomment)
{
this.comment=comment;
}
/**
* @return The commentforthisitem.
*/
publicStringgetComment()
{
returncomment;
}
/**
* Setthe flagindicatingwhetherwe ownthisitem.
* @param ownIttrue if we ownthe item, false otherwise.
*/
publicvoidsetOwn(booleanownIt)
{
gotIt= ownIt;
}
/**
* @return true if we owna copyof thisitem.
*/
3
publicbooleangetOwn()
{
returngotIt;
}
/**
* Printdetailsaboutthisitemtothe textterminal.
*/
publicvoidprint()
{
System.out.print("title:"+ title + " (" + playingTime+" mins)");
if(gotIt) {
System.out.println("*");
} else {
System.out.println();
}
System.out.println(" " + comment);
}
}
4
/**
* The CD class representsaCD object. Informationabout the
* CD is storedandcan be retrieved.
*
* @author Michael KollingandDavidJ.Barnes
* @version2008.03.30
*/
publicclassCD extendsItem
{
private Stringartist;
private intnumberOfTracks;
/**
* Initializethe CD.
* @param theTitle The title of the CD.
* @param theArtistThe artistof the CD.
* @param tracks The numberof tracks on the CD.
* @param time The playingtime of the CD.
*/
publicCD(StringtheTitle,StringtheArtist,inttracks,inttime)
{
super(theTitle,time);
artist= theArtist;
numberOfTracks=tracks;
}
/**
* @return The artistfor thisCD.
*/
5
publicStringgetArtist()
{
returnartist;
}
/**
* @return The numberof tracks on thisCD.
*/
publicintgetNumberOfTracks()
{
returnnumberOfTracks;
}
}
6
/**
* The DVD class representsaDVD object. Informationabout the
* DVD isstoredand can be retrieved.We assume thatwe onlydeal
* withmovie DVDsat thisstage.
*
* @author Michael KollingandDavidJ.Barnes
* @version2008.03.30
*/
publicclassDVDextendsItem
{
private Stringdirector;
/**
* Constructorfor objectsof classDVD
* @param theTitle The title of thisDVD.
* @param theDirectorThe directorof thisDVD.
* @param time The runningtime of the mainfeature.
*/
publicDVD(StringtheTitle,StringtheDirector,inttime)
{
super(theTitle,time);
director= theDirector;
}
/**
* @return The directorforthisDVD.
*/
publicStringgetDirector()
{
7
returndirector;
}
}
8
importjava.util.ArrayList;
/**
* The database class provides afacility tostore entertainment
* itemobjects.A listof all itemscanbe printedtothe
* terminal.
*
* Thisversiondoesnotsave the data to disk,andit doesnot
* provide anysearchfunctions.
*
* @author Michael KollingandDavidJ.Barnes
* @version2008.03.30
*/
publicclassDatabase
{
private ArrayList<Item>items;
/**
* Constructan emptyDatabase.
*/
publicDatabase()
{
items= newArrayList<Item>();
}
/**
* Addan itemto the database.
* @param theItemThe itemtobe added.
*/
9
publicvoidaddItem(ItemtheItem)
{
items.add(theItem);
}
/**
* Printa listof all currentlystoreditemstothe
* textterminal.
*/
publicvoidlist()
{
for(Itemitem:items) {
item.print();
System.out.println(); // emptyline betweenitems
}
}
}

Versi 2

  • 1.
    1 Versi 2. /** * TheItemclass represents amulti-mediaitem. * Informationaboutthe itemisstoredandcan be retrieved. * Thisclass servesasa superclassformore specificitms. * * @author Michael KollingandDavidJ.Barnes * @version2008.03.30 */ publicclassItem { private Stringtitle; private intplayingTime; private booleangotIt; private Stringcomment; /** * Initialisethe fieldsof the item. * @param theTitle The title of thisitem. * @param time The runningtime of thisitem. */ publicItem(StringtheTitle,inttime) { title =theTitle; playingTime=time; gotIt= false; comment= ""; }
  • 2.
    2 /** * Enter acommentforthisitem. * @param commentThe commentto be entered. */ publicvoidsetComment(Stringcomment) { this.comment=comment; } /** * @return The commentforthisitem. */ publicStringgetComment() { returncomment; } /** * Setthe flagindicatingwhetherwe ownthisitem. * @param ownIttrue if we ownthe item, false otherwise. */ publicvoidsetOwn(booleanownIt) { gotIt= ownIt; } /** * @return true if we owna copyof thisitem. */
  • 3.
    3 publicbooleangetOwn() { returngotIt; } /** * Printdetailsaboutthisitemtothe textterminal. */ publicvoidprint() { System.out.print("title:"+title + " (" + playingTime+" mins)"); if(gotIt) { System.out.println("*"); } else { System.out.println(); } System.out.println(" " + comment); } }
  • 4.
    4 /** * The CDclass representsaCD object. Informationabout the * CD is storedandcan be retrieved. * * @author Michael KollingandDavidJ.Barnes * @version2008.03.30 */ publicclassCD extendsItem { private Stringartist; private intnumberOfTracks; /** * Initializethe CD. * @param theTitle The title of the CD. * @param theArtistThe artistof the CD. * @param tracks The numberof tracks on the CD. * @param time The playingtime of the CD. */ publicCD(StringtheTitle,StringtheArtist,inttracks,inttime) { super(theTitle,time); artist= theArtist; numberOfTracks=tracks; } /** * @return The artistfor thisCD. */
  • 5.
    5 publicStringgetArtist() { returnartist; } /** * @return Thenumberof tracks on thisCD. */ publicintgetNumberOfTracks() { returnnumberOfTracks; } }
  • 6.
    6 /** * The DVDclass representsaDVD object. Informationabout the * DVD isstoredand can be retrieved.We assume thatwe onlydeal * withmovie DVDsat thisstage. * * @author Michael KollingandDavidJ.Barnes * @version2008.03.30 */ publicclassDVDextendsItem { private Stringdirector; /** * Constructorfor objectsof classDVD * @param theTitle The title of thisDVD. * @param theDirectorThe directorof thisDVD. * @param time The runningtime of the mainfeature. */ publicDVD(StringtheTitle,StringtheDirector,inttime) { super(theTitle,time); director= theDirector; } /** * @return The directorforthisDVD. */ publicStringgetDirector() {
  • 7.
  • 8.
    8 importjava.util.ArrayList; /** * The databaseclass provides afacility tostore entertainment * itemobjects.A listof all itemscanbe printedtothe * terminal. * * Thisversiondoesnotsave the data to disk,andit doesnot * provide anysearchfunctions. * * @author Michael KollingandDavidJ.Barnes * @version2008.03.30 */ publicclassDatabase { private ArrayList<Item>items; /** * Constructan emptyDatabase. */ publicDatabase() { items= newArrayList<Item>(); } /** * Addan itemto the database. * @param theItemThe itemtobe added. */
  • 9.
    9 publicvoidaddItem(ItemtheItem) { items.add(theItem); } /** * Printa listofall currentlystoreditemstothe * textterminal. */ publicvoidlist() { for(Itemitem:items) { item.print(); System.out.println(); // emptyline betweenitems } } }