Class yang akan digunakan antara lain (beserta source code) :
1. Item
public class Item
{
private String judul;
private int waktuplay;
private boolean dapat;
private String komen;
public Item(String judul, int waktuplay){ //constructor
this.judul = judul;
this.waktuplay = waktuplay;
dapat = false;
komen = "";
}
public void setkomen(String komen){ //metode untuk memasukkan komen item
this.komen = komen;
}
public String getkomen(){ //metode untuk return komen item
return komen;
}
//penanda yang menandakan kita sudah memilikinya atau tidak
public void setOwn(boolean dapat){
this.dapat = dapat;
}
public boolean getOwn(){ //return nilai ketersediaan item
return dapat;
}
//metode untuk mencetak item
public void cetaks(){
System.out.println("Judul : "+judul+"("+waktuplay+"menit)");
if(dapat) {
System.out.println("Tersedia");
}
else{
System.out.println("Tidak Tersedia");
}
System.out.println("komen :" +komen);
}
}
2. CD
public class CD extends Item
{
private String artist;
private int numberoftracks;
public CD (String judul, int waktuplay, String artist, int numberoftracks) //constructor
{
super(judul,waktuplay);
this.artist=artist;
this.numberoftracks=numberoftracks;
}
public void setkomen(String komen){
super.setkomen(komen);
}
public void setOwn(boolean dapat){
super.setOwn(dapat);
}
public String getArtist(){
return artist;
}
public int getNumbertracks(){
return numberoftracks;
}
public void print(){
System.out.println("Artist : " +artist);
System.out.println("Number of tracks : " +numberoftracks);
}
}
3. DVD
public class DVD extends Item
{
private String director;
public DVD(String judul, int waktuplay, String director) //constructor
{
super(judul, waktuplay);
this.director=director;
}
public void setkomen(String komen){
super.setkomen(komen);
}
public void setOwn(boolean dapat){
super.setOwn(dapat);
}
public String getDirector(){
return director;
}
public void cetak(){
System.out.println ("Director : " +director);
}
}
import java.util.ArrayList;
public class Database
{
private ArrayList<Item> things;
public Database(){ //constructor
things=new ArrayList<Item>();
}
public void addItem(Item Items){
things.add(Items);
}
public void hapuspindah(Item Items){
things.remove(Items);
}
public void cetak()
{
for(Item y: things)
{
y.cetaks();
if(y instanceof CD){
CD x = (CD) y;
x.print();
}
else{
DVD x = (DVD) y;
x.cetak();
}
System.out.println();
}
}
}
Tidak ada komentar:
Posting Komentar