singkat kata berikut adalah contoh Source Code sederhana metoda Template Pattern yang saya tulis menggunakan java :
/*
contoh template pattern
author m42h31
param none
*/
public abstract class Motor{
protected String noPol;
protected static String merek,model;
protected final static int A1=100,A2=10;
protected final static int B1=1000,B2=100;
protected String warna=”Black”;
protected String jenisPembayaran=”Tunai”;
private static float bbm=0.06f;
private static int jml=0;
private static int tahun=2004;
private static int cc=A1;
private static int hargaPokok;
private static int bunga;
protected static int hargaBayar;
public Motor(){}
public Motor(String np,String mrk,String mdl){
noPol=np;
merek=mrk;
model=mdl;
jml++;
}
noPol=np;
merek=mrk;
model=mdl;
jml++;
}
public void tahunPertama(){
System.out.println(“Tahun Produksi :”+tahun+”,Jumlah Motor : “+jml);
System.out.println(“=============================”);
System.out.println(“Tahun Produksi :”+tahun+”,Jumlah Motor : “+jml);
System.out.println(“=============================”);
}
public void nextTahun(){
hargaPokok+=100;
tahun++;
tahunPertama();
hargaPokok+=100;
tahun++;
tahunPertama();
}
public void head(){
System.out.println(“Nomer Polisi : “+getNoPol());
System.out.println(“Merek : “+getMerek());
System.out.println(“Model : “+getModel());
System.out.println(“Warna : “+getWarna());
System.out.println(“JenisPembayaran : “+jenisPembayaran);
}
System.out.println(“Nomer Polisi : “+getNoPol());
System.out.println(“Merek : “+getMerek());
System.out.println(“Model : “+getModel());
System.out.println(“Warna : “+getWarna());
System.out.println(“JenisPembayaran : “+jenisPembayaran);
}
public abstract void harga();
public void tail(){
System.out.println(“BBM : “+bbm+” liter/km”);
System.out.println(“Besaran CC : “+cc+” cc”);
System.out.println(“============================”);
}
System.out.println(“BBM : “+bbm+” liter/km”);
System.out.println(“Besaran CC : “+cc+” cc”);
System.out.println(“============================”);
}
public void cetak(){
head();
harga();
tail();
}
head();
harga();
tail();
}
public static void tambahCc(){
cc+=A2;
bbm-=0.01f;
hargaPokok+=500;
}
cc+=A2;
bbm-=0.01f;
hargaPokok+=500;
}
public void setWarna(String wr){
warna=wr;
}
public void setJenisPembayaran(String jp){
jenisPembayaran=jp;
}
warna=wr;
}
public void setJenisPembayaran(String jp){
jenisPembayaran=jp;
}
public void setHargaPokok(int hp){
hargaPokok=hp;
}
hargaPokok=hp;
}
public void setBunga(int bg){
bunga=bg;
}
bunga=bg;
}
public int getHargaPokok(){
return hargaPokok;
}
return hargaPokok;
}
public int getBunga(){
return bunga;
}
return bunga;
}
public int getHargaBayar(){
return hargaBayar=hargaPokok+bunga;
}
return hargaBayar=hargaPokok+bunga;
}
public String getNoPol(){
return noPol;
}
return noPol;
}
public String getMerek(){
return merek;
}
return merek;
}
public String getModel(){
return model;
}
public String getWarna(){
return warna;
}
return model;
}
public String getWarna(){
return warna;
}
}
public class Tunai extends Motor{
public Tunai(String np,String mrk,String mdl){
super(np,mrk,mdl);
}
public String getNopol(){
return noPol;
}
public String getMerek(){
return merek;
}
public String getModel(){
return model;
}
public void harga(){
System.out.println(“Harga Pokok : “+getHargaPokok());
System.out.println(“Harga Bayar : “+getHargaBayar());
}
public Tunai(String np,String mrk,String mdl){
super(np,mrk,mdl);
}
public String getNopol(){
return noPol;
}
public String getMerek(){
return merek;
}
public String getModel(){
return model;
}
public void harga(){
System.out.println(“Harga Pokok : “+getHargaPokok());
System.out.println(“Harga Bayar : “+getHargaBayar());
}
}
public class Kredit extends Motor{
public Kredit(Tunai tun){
noPol=tun.getNoPol();
merek=tun.getMerek();
model=tun.getModel();
warna=tun.getWarna();
setBunga(B2);
tun.hargaBayar=getHargaPokok()+getBunga();
setJenisPembayaran(“Kredit”);
}
public void harga(){
System.out.println(“Harga Pokok : “+getHargaPokok());
System.out.println(“Bunga :”+getBunga());
System.out.println(“Harga Bayar : “+getHargaBayar());
}
}
public Kredit(Tunai tun){
noPol=tun.getNoPol();
merek=tun.getMerek();
model=tun.getModel();
warna=tun.getWarna();
setBunga(B2);
tun.hargaBayar=getHargaPokok()+getBunga();
setJenisPembayaran(“Kredit”);
}
public void harga(){
System.out.println(“Harga Pokok : “+getHargaPokok());
System.out.println(“Bunga :”+getBunga());
System.out.println(“Harga Bayar : “+getHargaBayar());
}
}
class testMotor{
public static void main(String []args){
Tunai vg=new Tunai(“D3154FA”,”Yamaha”,”Vega”);
vg.setHargaPokok(10000);
vg.setWarna(“biru”);
vg.tahunPertama();
vg.cetak();
vg.nextTahun();
vg.tambahCc();
vg.cetak();
public static void main(String []args){
Tunai vg=new Tunai(“D3154FA”,”Yamaha”,”Vega”);
vg.setHargaPokok(10000);
vg.setWarna(“biru”);
vg.tahunPertama();
vg.cetak();
vg.nextTahun();
vg.tambahCc();
vg.cetak();
Tunai sp=new Tunai(“D4353GD”,”Honda”,”Supra”);
sp.setHargaPokok(14000);
sp.setWarna(“merah”);
sp.tahunPertama();
sp.cetak();
sp.setHargaPokok(14000);
sp.setWarna(“merah”);
sp.tahunPertama();
sp.cetak();
Kredit spk=new Kredit(sp);
spk.cetak();
spk.tambahCc();
spk.cetak();
spk.cetak();
spk.tambahCc();
spk.cetak();
spk.nextTahun();
spk.tambahCc();
spk.cetak();
}
}
spk.tambahCc();
spk.cetak();
}
}
0 komentar:
Posting Komentar