Social Items

SPACE IKLAN ( ADS ) CLICK HERE
mengurangi-pengulangan-code-menggunakan-method-java


Example


Before Code


if(brtBrg > maxBrt)
                {
                   Double hitBrg;
                   hitBrg = brtBrg - 15;
                   totaLebih = (jmlOrg * ekTicket) + (biayaTmbh * hitBrg);
                 infoYogi = "\n Jurusan : " +jurS
                        + "\n Kelas : " +klsEk
                        + "\n Berat Barang Bawaan : " +brtBrg
                        + "\n Berat Barang MAX : " +maxBrt +" Kg"
                        + "\n Denda Barang Rp." +biayaTmbh
                        + "\n Jumlah Orang : " +jmlOrg
                        + "\n Total Biaya Rp." +totaLebih;
                JOptionPane.showMessageDialog(null, infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
                
                }
                else
                {
                    total = (jmlOrg * ekTicket) + (biayaTmbh * brtBrg);
                    infoYogi = "\n Jurusan : " +jurB
                        + "\n Kelas : " +klsEk
                        + "\n Berat Barang Bawaan : " +brtBrg
                        + "\n Berat Barang MAX : " +maxBrt +" Kg"
                        + "\n Denda Barang Rp." +biayaTmbh
                        + "\n Jumlah Orang : " +jmlOrg
                        + "\n Total Biaya Rp." +total;
                JOptionPane.showMessageDialog(null, infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
                }


After Using Method


if(yogi.brtBrg > yogi.maxBrt)
                {
                   Double hitBrg;
                   hitBrg = yogi.brtBrg - 15;
                   yogi.totaLebih = (yogi.jmlOrg * yogi.ekTicket) + (yogi.biayaTmbh * hitBrg);

                yogi.infoReport(); //memanggil method report
                yogi.infoYogi = "\n Total Biaya Rp." +yogi.totaLebih;
                JOptionPane.showMessageDialog(null, yogi.infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
                
                }
                else
                {
                   yogi.total = (yogi.jmlOrg * yogi.ekTicket) + (yogi.biayaTmbh * yogi.brtBrg);
                   yogi.infoReport();
                   yogi.infoYogi = "\n Total Biaya Rp." +yogi.total;
                JOptionPane.showMessageDialog(null, yogi.infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
                }

Sebelumnya buatlah variable nya menjadi public

Example


Before


public static void main(String[] args) {
        // menu for jenis tiket dan kelas tiket
        String jnsTicketnKls = "";
        String infoYogi = "";
        String brtBr;
        String jmlOrgY;
//......
}

After



public class BusTicket 
{
    public String infoYogi = "";
    public String brtBr;
    public String jmlOrgY;

 public static void main(String[] args) {
//        menu for jenis tiket dan kelas tiket
//        String jnsTicketnKls = "";
//        String infoYogi = "";
//        String brtBr;
//        String jmlOrgY;

Buat Methodnya



public class BusTicket 
{
    public String infoYogi = "";
    public String brtBr;
    public String jmlOrgY;

 public static void main(String[] args) 
{
   //code here
}
 public void infoReport()
       {
           infoYogi = "\n Jurusan : " +jurB
                        + "\n Kelas : " +klsEk
                        + "\n Berat Barang Bawaan : " +brtBrg
                        + "\n Berat Barang MAX : " +maxBrt +" Kg"
                        + "\n Denda Barang Rp." +biayaTmbh
                        + "\n Jumlah Orang : " +jmlOrg;
           JOptionPane.showMessageDialog(null, infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
       }

Untuk pemanggilan atau menggunaan variable public di dalam main , harus menggunakan object , like as yogi.namavariable ( nama_method.nama_variable) else other method


//buat object untuk class nya
        BusTicket yogi; //BusTicket adalah class name
        yogi = new BusTicket(); // this for method

Berikut Source Code
jam 12 siang akan saya update;

Mengurangi Pengulangan Code Menggunakan Method Java

mengurangi-pengulangan-code-menggunakan-method-java


Example


Before Code


if(brtBrg > maxBrt)
                {
                   Double hitBrg;
                   hitBrg = brtBrg - 15;
                   totaLebih = (jmlOrg * ekTicket) + (biayaTmbh * hitBrg);
                 infoYogi = "\n Jurusan : " +jurS
                        + "\n Kelas : " +klsEk
                        + "\n Berat Barang Bawaan : " +brtBrg
                        + "\n Berat Barang MAX : " +maxBrt +" Kg"
                        + "\n Denda Barang Rp." +biayaTmbh
                        + "\n Jumlah Orang : " +jmlOrg
                        + "\n Total Biaya Rp." +totaLebih;
                JOptionPane.showMessageDialog(null, infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
                
                }
                else
                {
                    total = (jmlOrg * ekTicket) + (biayaTmbh * brtBrg);
                    infoYogi = "\n Jurusan : " +jurB
                        + "\n Kelas : " +klsEk
                        + "\n Berat Barang Bawaan : " +brtBrg
                        + "\n Berat Barang MAX : " +maxBrt +" Kg"
                        + "\n Denda Barang Rp." +biayaTmbh
                        + "\n Jumlah Orang : " +jmlOrg
                        + "\n Total Biaya Rp." +total;
                JOptionPane.showMessageDialog(null, infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
                }


After Using Method


if(yogi.brtBrg > yogi.maxBrt)
                {
                   Double hitBrg;
                   hitBrg = yogi.brtBrg - 15;
                   yogi.totaLebih = (yogi.jmlOrg * yogi.ekTicket) + (yogi.biayaTmbh * hitBrg);

                yogi.infoReport(); //memanggil method report
                yogi.infoYogi = "\n Total Biaya Rp." +yogi.totaLebih;
                JOptionPane.showMessageDialog(null, yogi.infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
                
                }
                else
                {
                   yogi.total = (yogi.jmlOrg * yogi.ekTicket) + (yogi.biayaTmbh * yogi.brtBrg);
                   yogi.infoReport();
                   yogi.infoYogi = "\n Total Biaya Rp." +yogi.total;
                JOptionPane.showMessageDialog(null, yogi.infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
                }

Sebelumnya buatlah variable nya menjadi public

Example


Before


public static void main(String[] args) {
        // menu for jenis tiket dan kelas tiket
        String jnsTicketnKls = "";
        String infoYogi = "";
        String brtBr;
        String jmlOrgY;
//......
}

After



public class BusTicket 
{
    public String infoYogi = "";
    public String brtBr;
    public String jmlOrgY;

 public static void main(String[] args) {
//        menu for jenis tiket dan kelas tiket
//        String jnsTicketnKls = "";
//        String infoYogi = "";
//        String brtBr;
//        String jmlOrgY;

Buat Methodnya



public class BusTicket 
{
    public String infoYogi = "";
    public String brtBr;
    public String jmlOrgY;

 public static void main(String[] args) 
{
   //code here
}
 public void infoReport()
       {
           infoYogi = "\n Jurusan : " +jurB
                        + "\n Kelas : " +klsEk
                        + "\n Berat Barang Bawaan : " +brtBrg
                        + "\n Berat Barang MAX : " +maxBrt +" Kg"
                        + "\n Denda Barang Rp." +biayaTmbh
                        + "\n Jumlah Orang : " +jmlOrg;
           JOptionPane.showMessageDialog(null, infoYogi,"Program Penjualan Ticket", JOptionPane.INFORMATION_MESSAGE);
       }

Untuk pemanggilan atau menggunaan variable public di dalam main , harus menggunakan object , like as yogi.namavariable ( nama_method.nama_variable) else other method


//buat object untuk class nya
        BusTicket yogi; //BusTicket adalah class name
        yogi = new BusTicket(); // this for method

Berikut Source Code
jam 12 siang akan saya update;
Load Comments

Subscribe Our Newsletter