contoh program function:
import java.util.Scanner;
public class perkakas{
static void silahkan(){
System.out.println("-SELAMAT DATANG DI TOKO PERKAKAS-");
}
static int hargapalu(){
int hargapalu =17000;
return hargapalu;
}
static int hargagergaji(){
int hargagergaji =20000;
return hargagergaji;
}
static int hargadiesel(){
int hargadiesel =65007500;
return hargadiesel;
}
static int hargabor(){
int hargabor =3000000;
return hargabor;
}
static int hargaobeng(){
int hargaobeng =5000;
return hargaobeng;
}
static int hargameteran(){
int hargameteran =50000;
return hargameteran;
}
static void pilihan(){
System.out.println("=====MENU PENJUALAN ALAT PERKAKAS=====");
System.out.println("1.PALU");
System.out.println("2.GERGAJI");
System.out.println("3.DIESEL");
System.out.println("4.BOR LISTRIK");
System.out.println("5.OBENG");
System.out.println("6.meteran");
System.out.println("7.KELUAR");
System.out.println("Masukan Pilihan");
}
static int palu(int hargapalu,int jumlah){
int total = hargapalu * jumlah;
return total;
}
static int gergaji(int hargagergaji,int jumlah){
int total = hargagergaji * jumlah;
return total;
}
static int diesel(int hargadiesel,int jumlah){
int total = hargadiesel * jumlah;
return total;
}
static int bor(int hargabor,int jumlah){
int total = hargabor * jumlah;
return total;
}
static int obeng(int hargaobeng,int jumlah){
int total = hargaobeng * jumlah;
return total;
}
static int meteran(int hargameteran,int jumlah){
int total = hargameteran * jumlah;
return total;
}
static int jumlah(){
Scanner input = new Scanner(System.in);
int jumlah = input.nextInt();
return jumlah;
}
public static void main(String[]args){
for(String ulang = "y"; ulang.equals("y") || ulang.equals("Y");){
Scanner input = new Scanner(System.in);
silahkan();
int angka = 7;
do{
pilihan();
try{
angka = input.nextInt();
switch(angka){
case 1 :
System.out.println("Harga palu= "+ hargapalu());
System.out.print("jumlah pembelian = ");
int piro = jumlah();
int rego = hargapalu();
palu(rego,piro);
System.out.println("Total harga= " + palu(rego,piro));
break;
case 2 :
System.out.println("Harga gergaji= "+ hargagergaji());
System.out.print("jumlah pembelian = ");
int pire = jumlah();
int regi = hargagergaji();
gergaji(regi,pire);
System.out.println("Total harga= " + gergaji(regi,pire));
break;
case 3 :
System.out.println("Harga diesel= "+ hargadiesel());
System.out.print("jumlah pembelian = ");
int pira = jumlah();
int rega = hargadiesel();
diesel(rega,pira);
System.out.println("Total harga= " + diesel(rega,pira));
break;
case 4 :
System.out.println("Harga bor= "+ hargabor());
System.out.print("jumlah pembelian = ");
int piroo = jumlah();
int reguh = hargabor();
bor(reguh,piroo);
System.out.println("Total harga= " + bor(reguh,piroo));
break;
case 5 :
System.out.println("Harga obeng= "+ hargaobeng());
System.out.print("jumlah pembelian = ");
int piru = jumlah();
int rege = hargaobeng();
obeng(rege,piru);
System.out.println("Total harga= " + obeng(rege,piru));
break;
case 6 :
System.out.println("Harga meteran= "+ hargameteran());
System.out.print("jumlah pembelian = ");
int piri = jumlah();
int regoh = hargameteran();
meteran(regoh,piri);
System.out.println("Total harga= " + meteran(regoh,piri));
break;
case 7 :System.out.println("ingin kembali ke menu? (untuk kembali ke menu tekan Y,untuk keluar ketik selain Y) "); break;
default :
System.out.println("maaf inputan anda salah");
}
}catch(Exception e){
System.out.println("------maaf inputan salah------");
System.out.println("ingin kembali ke menu? (untuk kembali ke menu tekan Y,untuk keluar ketik selain Y) ");
input.next();
}
}while(angka != 7);
ulang = input.next();
}
}
}