Oke satu lagi guys, kali ini program belanja sederhana menggunakan array pada java. kodingannya ada dibawah guys.
import java.io.*;
public class soalno2{
public static void main(String[] args) throws Exception{
String[]nmbrg = new String[50];
int[]jum = new int[100]; int[]hrg = new int[100];
DataInputStream barang = new DataInputStream(System.in);
System.out.println("Berikut pilihan yang dapat anda beli ^_^");
System.out.print("\n1.Sprite Rp.4000\t4.Fanta Rp.4000\t\t7.Cocacola Rp.4000");
System.out.print("\n2.Mizone Rp.5000\t5.Pocari Rp.7000\t8.Aqua Rp.3000");
System.out.print("\n3.Nescafe Rp.8000\t6.FruitTea Rp.6000\t9.GreenTea Rp.6000");
System.out.println("\n\nMasukan jumlah barang yang akan dibeli : ");
String jml=barang.readLine();
int data = Integer.valueOf(jml).intValue();
for(int i=0;i<data; i++){
System.out.print("\nNama Barang Ke- "+(i+1)+ ": ");
String nm=barang.readLine();
nmbrg[i]=nm;
System.out.print("Jumlah : ");
String jlh=barang.readLine();
int jlah = Integer.valueOf(jlh).intValue();
jum[i]=jlah;
System.out.print("Harga Rp. ");
String har=barang.readLine();
int harg = Integer.valueOf(har).intValue();
hrg[i]=harg;
}
System.out.println("\nRincian Pembelian");
System.out.println("================================================");
System.out.println("NO NAMA BARANG JUMLAH HARGA TOTAL ");
System.out.println("================================================");
int tpenj=0;
for(int i=0;i<data; i++)
{
System.out.println((i+1)+" "+nmbrg[i]+" "+jum[i]+" "+hrg[i]+
" "+(jum[i]*hrg[i])); tpenj = tpenj + (jum[i]*hrg[i]);
}
System.out.println("================================================");
System.out.println("TOTAL BARANG : "+data);
System.out.println("TOTAL HARGA : Rp."+tpenj);
}
}
EmoticonEmoticon