Blogroll

Monday, 5 October 2015


Assalamualaikum,
Kali ini saya akan melanjutkan post saya yaitu tentang penggunaan switchcase untuk menambahkan diskon.
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 tugas3;
import javax.swing.JOptionPane;
/**
 *
 * @author dell
 */
public class BINTANGF {
   
    public static void main(String[] args) {
    String input0, input1, input2, input3, input4, input5, input6, input7, input8, input9, input10;
    int total, harga, diskon, totalbelanja, pilihan = 0, barang, jumlahbeli = 0;
    int beras, hasilberas, hargaberas, diskonberas=0, totalberas = 0;
    int gula, hasilgula, hargagula, diskongula=0, totalgula = 0;
    int kopi, hasilkopi, hargakopi, diskonkopi=0, totalkopi = 0;
    int susu, hasilsusu, hargasusu, diskonsusu=0, totalsusu = 0;
    int telur, hasiltelur, hargatelur, diskontelur=0, totaltelur = 0;
   
    JOptionPane.showMessageDialog(null, "Daftar Barang\n1. Beras\n2. Gula\n3. Kopi\n4. Susu\n5. Telur");
    input0 = JOptionPane.showInputDialog("Masukkan nomer barang pilihan Anda :");
    pilihan = Integer.parseInt (input0);
    switch(pilihan){
        case 1: JOptionPane.showMessageDialog(null, "Anda memilih Beras");
                    input1 = JOptionPane.showInputDialog("Jumlah beras yang dibeli (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 Gula");
                    input3 = JOptionPane.showInputDialog("Jumlah Gula yang dibeli (kg)");
                    input4 = JOptionPane.showInputDialog("Harga Gula per kg");
                    gula = Integer.parseInt (input3);
                    hargagula = Integer.parseInt(input4);
                    hasilgula = gula*hargagula;
                    if (gula >= 5){
                        JOptionPane.showMessageDialog(null, "Anda mendapat diskon 5%");
                        diskongula = hasilgula*5/100;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Anda tidak mendapat diskon 5%");
                         }
                    totalgula = hasilgula-diskongula;
            break;
        case 3: JOptionPane.showMessageDialog(null, "Anda memilih Kopi");
                    input5 = JOptionPane.showInputDialog("Jumlah Kopi yang dibeli (kg)");
                    input6 = JOptionPane.showInputDialog("Harga Kopi per kg");
                    kopi = Integer.parseInt (input5);
                    hargakopi = Integer.parseInt(input6);
                    hasilkopi = kopi*hargakopi;
                    if (kopi >= 5){
                        JOptionPane.showMessageDialog(null, "Anda mendapat diskon 7%");
                        diskonkopi = hasilkopi*7/100;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Anda tidak mendapat diskon 7%");
                         }
                    totalkopi = hasilkopi-diskonkopi;
            break;
        case 4: JOptionPane.showMessageDialog(null, "Anda memilih Susu");
                    input7 = JOptionPane.showInputDialog("Jumlah Susu yang dibeli (liter)");
                    input8 = JOptionPane.showInputDialog("Harga Susu per liter");
                    susu = Integer.parseInt (input7);
                    hargasusu = Integer.parseInt(input8);
                    hasilsusu = susu*hargasusu;
                    if (susu >= 5){
                        JOptionPane.showMessageDialog(null, "Anda mendapat diskon 10%");
                        diskonsusu = hasilsusu*10/100;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Anda tidak mendapat diskon 10%");
                         }
                    totalsusu = hasilsusu-diskonsusu;
            break;
        case 5: JOptionPane.showMessageDialog(null, "Anda memilih Telur");
                    input9 = JOptionPane.showInputDialog("Jumlah Telur yang dibeli (kg)");
                    input10 = JOptionPane.showInputDialog("Harga Telur per kg");
                    telur = Integer.parseInt (input9);
                    hargatelur = Integer.parseInt(input10);
                    hasiltelur = telur*hargatelur;
                    if (telur >= 5){
                        JOptionPane.showMessageDialog(null, "Anda mendapat diskon 8%");
                        diskontelur = hasiltelur*8/100;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Anda tidak mendapat diskon 8%");
                         }
                    totaltelur = hasiltelur-diskontelur;
            break;}
   
       totalbelanja = totalberas+totalgula+totalkopi+totalsusu+totaltelur;
       JOptionPane.showMessageDialog(null, "Total Belanja Anda Rp."+totalbelanja, "Total Belanja", JOptionPane.PLAIN_MESSAGE);
    }} 

sekian artikel saya mengenai penggunaan switchcase semoga bermanfaat.
Terima Kasih.
03:56 No comments » by Unknown
Posted in

0 comments:

Post a Comment

Search

Bookmark Us

Delicious Digg Facebook Favorites More Stumbleupon Twitter

Blogroll

Night Diamond - Link Select

Pages

About