1. Class abstract Disc untuk menjadi parent
abstract class Disc{
String title;
int playingTime;
boolean gotIt;
String comment;
public void setComment(String comment){
this.comment = comment;
}
public String getComment(){
return comment;
}
public void setOwn(boolean own){
gotIt = own;
}
public boolean getOwn(){
return gotIt;
}
}
2. Class CD dg parent Disc
public class CD extends Disc{
private String artist;
private int numberOfTracks;
public CD(String title, String artist, int tracks, int time){
this.title = title;
this.artist = artist;
numberOfTracks = tracks;
playingTime = time;
gotIt = false;
comment = "<no comment>";
}
public void print(){
System.out.println("CD: "+title+" ("+playingTime+" mins)");
System.out.println(" by "+artist);
System.out.println(" "+numberOfTracks+" tracks");
System.out.println(" Info: "+comment);
if(gotIt){
System.out.println("You own this CD");
}
else
System.out.println("You don't have this CD");
}
}
3. Class DVD dg parent Disc
public class DVD extends Disc{
private String director;
public DVD(String title, String director, int time){
this.title = title;
this.director = director;
playingTime = time;
gotIt = false;
comment = "<no comment>";
}
public void print(){
System.out.println("DVD: "+title+" ("+playingTime+" mins)");
System.out.println(" by "+director);
System.out.println(" Info: "+comment);
if(gotIt)
System.out.println("You own this DVD");
else
System.out.println("You don't have this DVD");
}
}
4. Class Database untuk menyimpan CD dan DVD< serta eksekusi program
import java.util.ArrayList;
public class Database{
private ArrayList<CD> cds;
private ArrayList<DVD> dvds;
public Database(){
cds = new ArrayList<CD>();
dvds = new ArrayList<DVD>();
}
public void addCD(CD cd){
cds.add(cd);
}
public void addDVD(DVD dvd){
dvds.add(dvd);
}
public void list(){
for(CD cd : cds){
cd.print();
System.out.println(); //garis kosong tiap entri
}
for(DVD dvd : dvds){
dvd.print();
System.out.println(); //garis kosong tiap entri
}
}
public static void main(String[] args){
Database db = new Database();
CD cd1 = new CD("Ballads 1", "Joji", 12, 2106);
cd1.setOwn(true);
CD cd2 = new CD("A Head Full of Dreams", "Coldplay", 11, 2745);
db.addCD(cd1);
db.addCD(cd2);
DVD dvd1 = new DVD("Endgame", "Anthony Russo", 10860);
dvd1.setOwn(true);
DVD dvd2 = new DVD("Joker", "Todd Phillips", 7320);
dvd1.setOwn(true);
db.addDVD(dvd2);
db.list();
}
}
5. Screenshot
Comments
Post a Comment