Polymorphism

Class Item
Untuk menambah Item ke dalam list
 /**  
  * The Item class represents a multi-media item.  
  * Information about the item is stored and can be retrieved.  
  * This class serves as a superclass for more specific itms.  
  *   
  * @author Michael Kolling and David J. Barnes  
  * @version 2008.03.30  
  */  
 public class Item  
 {  
   private String title;  
   private int playingTime;  
   private boolean gotIt;  
   private String comment;  
   /**  
    * Initialise the fields of the item.  
    * @param theTitle The title of this item.  
    * @param time The running time of this item.  
    */  
   public Item(String theTitle, int time)  
   {  
     title = theTitle;  
     playingTime = time;  
     gotIt = false;  
     comment = "<no comment>";  
   }  
   /**  
    * Enter a comment for this item.  
    * @param comment The comment to be entered.  
    */  
   public void setComment(String comment)  
   {  
     this.comment = comment;  
   }  
   /**  
    * @return The comment for this item.  
    */  
   public String getComment()  
   {  
     return comment;  
   }  
   /**  
    * Set the flag indicating whether we own this item.  
    * @param ownIt true if we own the item, false otherwise.  
    */  
   public void setOwn(boolean ownIt)  
   {  
     gotIt = ownIt;  
   }  
   /**  
    * @return Information whether we own a copy of this item.  
    */  
   public boolean getOwn()  
   {  
     return gotIt;  
   }  
   /**  
    * Print details of this item to the text terminal.  
    */  
   public void print()  
   {  
     System.out.print(title + " (" + playingTime + " mins)");  
     if(gotIt) {  
       System.out.println("*");  
     } else {  
       System.out.println();  
     }  
     System.out.println("  " + comment);  
   }  
 }  


Class Database
Untuk Menyimpan item 
 import java.util.ArrayList;  
 /**  
  * The database class provides a facility to store CD and video   
  * objects. A list of all CDs and videos can be printed to the  
  * terminal.  
  *   
  * This version does not save the data to disk, and it does not  
  * provide any search functions.  
  *   
  * @author Michael Kolling and David J. Barnes  
  * @version 2008.03.30  
  */  
 public class Database  
 {  
   private ArrayList<Item> items;  
   /**  
    * Construct an empty Database.  
    */  
   public Database()  
   {  
     items = new ArrayList<Item>();  
   }  
   /**  
    * Add an item to the database.  
    * @param theItem The item to be added.  
    */  
   public void addItem(Item theItem)  
   {  
     items.add(theItem);  
   }  
   /**  
    * Print a list of all currently stored CDs and videos to the  
    * text terminal.  
    */  
   public void list()  
   {  
     for(Item item : items)  
     {  
       item.print();  
     }  
   }  
 }  


Class DVD
 /**  
  * The DVD class represents a DVD object. Information about the   
  * DVD is stored and can be retrieved. We assume that we only deal   
  * with movie DVDs at this stage.  
  *   
  * @author Michael Kolling and David J. Barnes  
  * @version 2008.03.30  
  */  
 public class DVD extends Item   
 {  
   private String director;  
   /**  
    * Constructor for objects of class DVD  
    * @param theTitle The title of this DVD.  
    * @param theDirector The director of this DVD.  
    * @param time The running time of the main feature.  
    */  
   public DVD(String theTitle, String theDirector, int time)  
   {  
     super(theTitle, time);  
     director = theDirector;  
   }  
   /**  
    * @return The director for this DVD.  
    */  
   public String getDirector()  
   {  
     return director;  
   }  
   /**  
    * Print details of this DVD to the text terminal.  
    */  
   public void print()  
   {  
     System.out.println("  director: " + director);  
   }  
 }  


Class CD
 /**  
  * The CD class represents a CD object. Information about the   
  * CD is stored and can be retrieved.  
  *   
  * @author Michael Kolling and David J. Barnes  
  * @version 2008.03.30  
  */  
 public class CD extends Item  
 {  
   private String artist;  
   private int numberOfTracks;  
   /**  
    * Initialize the CD.  
    * @param theTitle The title of the CD.  
    * @param theArtist The artist of the CD.  
    * @param tracks The number of tracks on the CD.  
    * @param time The playing time of the CD.  
    */  
   public CD(String theTitle, String theArtist, int tracks, int time)  
   {  
     super(theTitle, time);  
     artist = theArtist;  
     numberOfTracks = tracks;  
   }  
   /**  
    * @return The artist for this CD.  
    */  
   public String getArtist()  
   {  
     return artist;  
   }  
   /**  
    * @return The number of tracks on this CD.  
    */  
   public int getNumberOfTracks()  
   {  
     return numberOfTracks;  
   }  
   /**  
    * Print details of this CD to the text terminal.  
    */  
   public void print()  
   {  
     System.out.println("  " + artist);  
     System.out.println("  tracks: " + numberOfTracks);  
   }  
 }  



Setelah memasukkan data judul dan director DVD dan judul dan penyanyi CD, Serta memasuukan data item - item ke database. Lalu terakhir eksekusi void list()

Komentar

Postingan Populer