Java OOP: Case Presence / Absensi

Okay, this is my freetime works (although there are many task to be done πŸ˜› ). So this is about presence of student in a class. This code was made by me using Java OOP.

In this OOP, I made 4 classes:

  1. Mhs Class
    This class is about Student itself. Contain 2 attibutes: name and NRP (student ID). In this class, we can set and get both Name and NRP.
  2. Kelas Class
    This class is about Class itself (for example: Programming Class). Contain 1 attribute: Β student list. In this class, we can set student list, add student to class, remove student from class, get student list, get student, and check student whether exist in the class.
  3. Absen Class
    This class is about Presence itself (for example: First meet of Programming Class). Contain 2 attribute: class and list of student that has fill presence. In this class, we can do presence, check whether the student has fill presence or not.
  4. Main Class
    This is the main class. This class can be the interface / input-output class. This class also call another classes. In this class, I add 2 extra methods that works as: sample students in class RBPL, and sample student that has fill presence in first meet of class RBPL.

In method isiKelas of Main class, I add 3 students to class RBPL:

  1. Name: Amin. SID: 1001.
  2. Name: Budi. SID: 1002.
  3. Name: CIci. SID: 1003.

Then in method isiAbsen of Main class, 2 students have filled presence:

  1. Amin
  2. Budi

So, enjoy the code! πŸ˜‰

  1. Mhs Class

    [code language=”java”]
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package absensi;

    /**
    *
    * @author Fachri Hilmi
    * http://about.me/fhr93
    */
    public class Mhs {

    private String nrp;
    private String nama;

    public Mhs() {
    nrp = "";
    nama = "";
    }

    public Mhs(String nrp, String nama) {
    this.nrp = nrp;
    this.nama = nama;
    }

    public void setNrp(String nrp) {
    this.nrp = nrp;
    }

    public void setNama(String nama) {
    this.nama = nama;
    }

    public String getNrp() {
    return this.nrp;
    }

    public String getNama() {
    return this.nama;
    }

    public boolean isNrpNya(String kword) {
    return this.nrp.equalsIgnoreCase(kword);
    }

    public boolean isNamaNya(String kword) {
    return this.nama.equalsIgnoreCase(kword);
    }
    }
    [/code]

  2. Kelas Class

    [code language=”java”]
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package absensi;

    import java.util.ArrayList;

    /**
    *
    * @author Fachri Hilmi
    * http://about.me/fhr93
    */
    public class Kelas {

    private ArrayList listMhs;

    public Kelas() {
    this.listMhs = new ArrayList();
    }

    public Kelas(ArrayList listMhs) {
    this.listMhs = listMhs;
    }

    public void addMhs(Mhs mhs) {
    this.listMhs.add(mhs);
    }

    public void remMhs(Mhs mhs) {
    this.listMhs.remove(mhs);
    }

    public Mhs getMhs(String kword){
    for (int i = 0; i < this.listMhs.size(); i++) {
    Mhs temp = (Mhs) this.listMhs.get(i);
    if (temp.isNrpNya(kword)) {
    return temp;
    } else if (temp.isNamaNya(kword)) {
    return temp;
    }
    }

    return new Mhs();
    }

    public ArrayList getListMhs() {
    return this.listMhs;
    }

    public boolean isInKelas(String kword) {
    for (int i = 0; i < this.listMhs.size(); i++) {
    Mhs temp = (Mhs) this.listMhs.get(i);
    if (temp.isNrpNya(kword)) {
    return true;
    } else if (temp.isNamaNya(kword)) {
    return true;
    }
    }
    return false;
    }
    }

    [/code]

  3. Absen Class

    [code language=”java”]
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package absensi;

    import java.util.ArrayList;

    /**
    *
    * @author Fachri Hilmi
    * http://about.me/fhr93
    */
    public class Absen {

    private Kelas kelas;
    private ArrayList listMhsAbsen = new ArrayList();

    public Absen() {
    this.kelas = new Kelas();
    }

    public Absen(Kelas kelas) {
    this.kelas = kelas;
    }

    public String absen(String kword) {
    if (this.kelas.isInKelas(kword)) {
    Mhs mhs = kelas.getMhs(kword);
    if (!this.isAbsen(mhs)) {
    listMhsAbsen.add(mhs);
    return "Absen berhasil";
    } else {
    return "Absen gagal: Sudah absen";
    }
    } else {
    return "Absen gagal: Tidak termasuk kelas ini";
    }
    }

    public boolean isAbsen(Mhs mhs) {
    for (int i = 0; i < this.listMhsAbsen.size(); i++) {
    Mhs temp = (Mhs) this.listMhsAbsen.get(i);
    if (temp.isNrpNya(mhs.getNrp())) {
    return true;
    } else if (temp.isNamaNya(mhs.getNama())) {
    return true;
    }
    }
    return false;
    }
    }

    [/code]

  4. Main Class

    [code language=”java”]
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package absensi;

    /**
    *
    * @author Fachri Hilmi
    * http://about.me/fhr93
    */
    public class Main {

    static Kelas rbpl = new Kelas();
    static Absen meet1 = new Absen();

    public static void main(String[] args) {
    isiKelas();
    isiAbsen();

    String keyword = "Cici";
    System.out.println("Proses absen \"" + keyword + "\"…");
    System.out.println(meet1.absen(keyword));
    }

    public static void isiKelas() {
    rbpl.addMhs(new Mhs("1001", "Amin"));
    rbpl.addMhs(new Mhs("1002", "Budi"));
    rbpl.addMhs(new Mhs("1003", "Cici"));
    }

    public static void isiAbsen() {
    meet1 = new Absen(rbpl);
    meet1.absen("amin");
    meet1.absen("budi");
    }
    }

    [/code]

Output:

output3

Any question and comment would be appreciated. Thank you! πŸ˜€

4 thoughts on “Java OOP: Case Presence / Absensi

  1. om, ada yang lebi simpel. pake array, ngecek data existing nya pake Arrays.asList.
    bisa capture index sama contains juga.

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Situs ini menggunakan Akismet untuk mengurangi spam. Pelajari bagaimana data komentar Anda diproses.