Administrasi Kartu Pintar
Class KartuPintar
public class KartuPintar{
public static final int SALDO_AWAL=10000;
//attribute
private String id;
private int saldo;
//constructor
public KartuPintar(){
id="0000";
saldo=SALDO_AWAL;
}
//constructor
public KartuPintar(String id, int saldo){
this.id=id;
this.saldo=saldo;
}
//implementor
public void topUp(){
saldo += SALDO_AWAL;
}
//implementor
public void topUp(int value){
saldo +=value;
}
public String toString(){
return id+"->saldo: " + saldo;
}
//getter
public String getId(){
return id;
}
//getter
public int getSaldo(){
return saldo;
}
//implementor
public void debet(int value){
if(checkSaldo(value))
saldo -=value;
else
System.out.println("Saldo Anda Tidak Cukup !");
}
private boolean checkSaldo(int value){
return ((saldo-value)>=0);
}
}
CounterKartuPintar
import java.util.*;
public class CounterKartuPintar{
private Vector daftarKartu=new Vector();
public void kartuBaru(KartuPintar kartuBaru){
daftarKartu.add(kartuBaru);
}
public void viewAllKartu(){
for(KartuPintar kartu:daftarKartu)
System.out.println(kartu);
}
public void topUp(String idKartu, int value){
int idx=cariKartu(idKartu);
if(idx!=-1){
daftarKartu.elementAt(idx).topUp(value);
System.out.println("Id Kartu: " +idKartu + " BERHASIL di topUp");
System.out.println("Saldo Saat ini: ");
cetakSaldo(idKartu);
}
else
System.out.println("No Kartu Tidak Ada !");
}
public void debet(String idKartu, int value){
}
public void cetakSaldo(String id){
int idx=cariKartu(id);
if(idx!=-1){
System.out.println(daftarKartu.elementAt(idx));
}
else
System.out.println("No Kartu Tidak Ada !");
}
private int cariKartu(String id){
int idx=-1;
int i=0;
do{
if(daftarKartu.elementAt(i).getId().equals(id))
{
idx=i;
i=daftarKartu.size();
}
i++;
}while(i