exception handling

public class TestKaryawan {

    public static void main(String[] args) {
        Karyawan k = new Karyawan();
       
            int tgl = Integer.parseInt(JOptionPane.showInputDialog("masukkan tanggal lahir: "));
            k.setTglLahir(tgl);
            int bln = Integer.parseInt(JOptionPane.showInputDialog("masukkan bulan lahir :"));
            k.setBlnLahir(bln);
            int thn = Integer.parseInt(JOptionPane.showInputDialog("masukkan tahun lahir:"));
            k.setThnLahir(thn);
    }
}






import javax.swing.JOptionPane;

/**
 *
 * @author admin
 */
public class Karyawan {
private String nama;
private int tglLahir;
private int blnLahir;
private int thnLahir;

    public Karyawan(){
    }
    /**
     * @return the nama
     */
    public String getNama() {
        return nama;
    }

    /**
     * @param nama the nama to set
     */
    public void setNama(String nama) {
        this.nama = nama;
    }

    /**
     * @return the tglLahir
     */
    public int getTglLahir()  {
        return tglLahir;
    }

    /**
     * @param tglLahir the tglLahir to set
     */
    public void setTglLahir(int tanggal)  {
        try{
            if (tanggal>0 && tanggal<=31){
                this.tglLahir = tanggal;
            }
            else{
                throw new Exception ("tanggal harus benar!");
            }
        }
        catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    /**
     * @return the blnLahir
     */
    public int getBlnLahir() {
        return blnLahir;
    }

    /**
     * @param blnLahir the blnLahir to set
     */
    public void setBlnLahir(int bln) {
         try{
            if (bln>0 && bln<=12){
                this.blnLahir = bln;
            }
            else{
                throw new Exception ("bulan harus benar!");
            }
        }
        catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    /**
     * @return the thnLahir
     */
    public int getThnLahir() {
        return thnLahir;
    }

    /**
     * @param thnLahir the thnLahir to set
     */
    public void setThnLahir(int thn) {
         try{
            if (thn>0 && thn>1900){
                this.thnLahir = thn;
            }
            else{
                throw new Exception ("tahun harus benar!");
            }
        }
        catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }
}

Komentar

Postingan populer dari blog ini

Menyesap Rindu Dalam Coding (Sastra Edan #7)

Bikin Amplop Cantik

Kost Anyar Nyah...