Anda Pengunjung Ke..

Senin, 05 Oktober 2015

Tugas 3 NetBeans: Switch-Case

Cara membuat switch-case pada NetBeans

berikut adalah source codenya:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tokopakjono20152;

import javax.swing.JOptionPane;

/**
 *
 * @author DeLavigne
 */
public class tugas3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    String input0, input1, input2, input3, input4, input5, input6, input7, input8, input9, input10;
    int totalbelanja, pilihan, barang, jumlahbeli = 0;
    int beras, hasilberas, hargaberas, diskonberas=0, totalberas = 0;
    int telur, hasiltelur, hargatelur, diskontelur=0, totaltelur = 0;
    int susu, hasilsusu, hargasusu, diskonsusu=0, totalsusu = 0;
    int gula, hasilgula, hargagula, diskongula=0, totalgula = 0;
    int indomie, hasilindomie, hargaindomie, diskonindomie=0, totalindomie = 0;
    
    JOptionPane.showMessageDialog(null, "Daftar Barang\n1. Beras\n2. Telur\n3. Susu\n4. Gula\n5. Indomie");
    input0 = JOptionPane.showInputDialog("Masukkan kode barang :");
    pilihan = Integer.parseInt (input0);
    switch(pilihan){
            case 1: JOptionPane.showMessageDialog(null, "Anda memilih Beras");
                    input1 = JOptionPane.showInputDialog("Jumlah beras yang dibeli (dalam kg)");
                    input2 = JOptionPane.showInputDialog("Harga beras per kg");
                    beras = Integer.parseInt (input1);
                    hargaberas = Integer.parseInt(input2);
                    hasilberas = beras*hargaberas;
                    if (beras >= 5){
                        JOptionPane.showMessageDialog(null, "Anda mendapat diskon 2%");
                        diskonberas = hasilberas*2/100;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Anda tidak mendapat diskon 2%");
                         }
                    totalberas = hasilberas-diskonberas;
            break;
            case 2: JOptionPane.showMessageDialog(null, "Anda memilih Telor");
                    input3 = JOptionPane.showInputDialog("Jumlah telor yang dibeli (kg)");
                    input4 = JOptionPane.showInputDialog("Harga telor per kg");
                    telur = Integer.parseInt (input3);
                    hargatelur = Integer.parseInt(input4);
                    hasiltelur = telur*hargatelur;
                    if (telur >= 5){
                        JOptionPane.showMessageDialog(null, "Anda mendapat diskon 5%");
                        diskontelur = hasiltelur*5/100;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Anda tidak mendapat diskon 2%");
                         }
                    totaltelur = hasiltelur-diskontelur;
            break;
            case 3: JOptionPane.showMessageDialog(null, "Anda memilih Susu");
                    input5 = JOptionPane.showInputDialog("Jumlah susu yang dibeli (dalam kaleng)");
                    input6 = JOptionPane.showInputDialog("Harga susu per kaleng");
                    susu = Integer.parseInt (input5);
                    hargasusu = Integer.parseInt(input6);
                    hasilsusu = susu*hargasusu;
                    if (susu >= 5){
                        JOptionPane.showMessageDialog(null, "Anda mendapat diskon 8%");
                        diskonsusu = hasilsusu*8/100;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Anda tidak mendapat diskon 8%");
                         }
                    totalsusu = hasilsusu-diskonsusu;
            break;
            case 4: JOptionPane.showMessageDialog(null, "Anda memilih Gula");
                    input7 = JOptionPane.showInputDialog("Jumlah Gula yang dibeli (kg)");
                    input8 = JOptionPane.showInputDialog("Harga Gula per kg");
                    gula = Integer.parseInt (input7);
                    hargagula = Integer.parseInt(input8);
                    hasilgula = gula*hargagula;
                    if (gula >= 5){
                        JOptionPane.showMessageDialog(null, "Anda mendapat diskon 10%");
                        diskongula = hasilgula*10/100;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Anda tidak mendapat diskon 10%");
                         }
                    totalgula = hasilgula-diskongula;
            break;
            case 5: JOptionPane.showMessageDialog(null, "Anda memilih Indomie");
                    input9 = JOptionPane.showInputDialog("Jumlah Indomie yang dibeli (dalam pcs)");
                    input10 = JOptionPane.showInputDialog("Harga Indomie per pcs");
                    indomie = Integer.parseInt (input9);
                    hargaindomie = Integer.parseInt(input10);
                    hasilindomie = indomie*hargaindomie;
                    if (indomie >= 5){
                        JOptionPane.showMessageDialog(null, "Anda mendapat diskon 20%");
                        diskonindomie = hasilindomie*20/100;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Anda tidak mendapat diskon 20%");
                         }
                    totalindomie = hasilindomie-diskonindomie;
            break;}
    
    totalbelanja = totalberas+totaltelur+totalsusu+totalgula+totalindomie;
    JOptionPane.showMessageDialog(null, "Total Belanja Anda Rp."+totalbelanja, "Total Belanja", JOptionPane.PLAIN_MESSAGE);
    }}

Tidak ada komentar:

Posting Komentar