package Bab7gui;
/**
*
* @author mcsentul
*/
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Gaji extends Frame {
TextField txtthnmsk, txtgajipokok, txtbonus, txttunjangan;
TextField txttotalgaji, txtlama, txtlama2;
CheckboxGroup jenispeg;
Checkbox staff, manager, direktur;
Label lbJudul, lbthn, lbjbtn, lbstaff, lbmanager, lbdirektur;
Label lbgaji, lbbns, lbtunjangan, lbtot, lblama, lblama2;
Button btnhitung;
public Gaji() {
setLayout(null);
lbJudul = new Label("Perhitungan Gaji");
lbJudul.setFont(new Font("Arial", 1, 14));
add(lbJudul);
lbJudul.setBounds(100, 30, 170, 20);
lbthn = new Label("Tahun masuk (4 digit) :");
add(lbthn);
lbthn.setBounds(30, 70, 135, 20);
txtthnmsk = new TextField("2004");
add(txtthnmsk);
txtthnmsk.setBounds(165, 70, 90, 20);
lbjbtn = new Label("Jabatan :");
add(lbjbtn);
lbjbtn.setBounds(30, 90, 62, 20);
jenispeg = new CheckboxGroup();
staff = new Checkbox("Staff ", jenispeg, false);
this.add(staff).setBounds(60, 110, 100, 20);
manager = new Checkbox("Manager", jenispeg, false);
this.add(manager).setBounds(60, 130, 100, 20);
direktur = new Checkbox("Direktur", jenispeg, false);
this.add(direktur).setBounds(60, 150, 100, 20);
staff.addItemListener(new mainAction());
manager.addItemListener(new mainAction());
direktur.addItemListener(new mainAction());
lblama = new Label("Lama Kerja :");
add(lblama);
lblama.setBounds(30, 170, 90, 20);
txtlama = new TextField("0");
add(txtlama);
txtlama.setBounds(130, 170, 30, 20);
lblama2 = new Label("Tahun");
add(lblama2);
lblama2.setBounds(160, 170, 90, 20);
lbgaji = new Label("Gaji Pokok :");
add(lbgaji);
lbgaji.setBounds(30, 190, 90, 20);
txtgajipokok = new TextField("0");
add(txtgajipokok);
txtgajipokok.setBounds(130, 190, 90, 20);
lbbns = new Label("Bonus :");
add(lbbns);
lbbns.setBounds(30, 210, 90, 20);
txtbonus = new TextField("0");
add(txtbonus);
txtbonus.setBounds(130, 210, 90, 20);
lbtunjangan = new Label("Tunjangan :");
add(lbtunjangan);
lbtunjangan.setBounds(30, 230, 90, 20);
txttunjangan = new TextField("0");
add(txttunjangan);
txttunjangan.setBounds(130, 230, 90, 20);
lbtot = new Label("Gaji Total :");
add(lbtot);
lbtot.setBounds(30, 250, 100, 20);
txttotalgaji = new TextField("0");
add(txttotalgaji);
txttotalgaji.setBounds(130, 250, 90, 20);
btnhitung = new Button("Hitung");
add(btnhitung);
btnhitung.setBounds(120, 280, 100, 20);
btnhitung.addActionListener(new mainAction());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class mainAction implements ItemListener, ActionListener {
public void itemStateChanged(ItemEvent ev) {
int thnmsk = Integer.parseInt(txtthnmsk.getText().trim());
Object pilihan = ev.getItemSelectable();
Date today = new Date();
int thn = today.getYear() + 1900;
if (pilihan == staff) {
txtlama.setText(String.valueOf(thn - thnmsk));
txtgajipokok.setText(String.valueOf(1000000));
} else if (pilihan == manager) {
txtlama.setText(String.valueOf(thn - thnmsk));
txtgajipokok.setText(String.valueOf(5000000));
} else if (pilihan == direktur) {
txtlama.setText(String.valueOf(thn - thnmsk));
txtgajipokok.setText(String.valueOf(10000000));
}
}
@Override
public void actionPerformed(ActionEvent e) {
Date today = new Date();
double thnmsk = Double.parseDouble(txtthnmsk.getText().trim());
double thn = today.getYear() + 1900;
double gaji = Double.parseDouble(txtgajipokok.getText().trim());
double lamatot = thn - thnmsk;
double bonus = 0, tun = 0;
double tot = 0;
Button btn = (Button) e.getSource();
if (btn == btnhitung) {
if (gaji == 1000000) {
if (lamatot < 1) {
bonus = 0;
tun = 0;
tot = gaji;
} else if ((lamatot >= 1) && (lamatot <= 10)) {
bonus = 0.05 * gaji;
tun = 0;
tot = gaji + bonus + tun;
} else if (lamatot > 10) {
bonus = (lamatot / 100) * gaji;
tun = 0;
tot = gaji + bonus + tun;
}
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
String r = f.format(tot);
txtbonus.setText(String.valueOf(bonus));
txttunjangan.setText(String.valueOf(tun));
txttotalgaji.setText(String.valueOf(r));
} else if (gaji == 5000000) {
bonus = (0.05 + (lamatot / 100)) * gaji;
tun = 0.05 * gaji;
tot = gaji + (bonus + tun);
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
String r = f.format(tot);
txtbonus.setText(String.valueOf(bonus));
txttunjangan.setText(String.valueOf(tun));
txttotalgaji.setText(String.valueOf(r));
} else if (gaji == 10000000) {
bonus = (0.1 + (lamatot / 100)) * gaji;
tun = 0.1 * gaji;
tot = gaji + (bonus + tun);
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
String r = f.format(tot);
txtbonus.setText(String.valueOf(bonus));
txttunjangan.setText(String.valueOf(tun));
txttotalgaji.setText(String.valueOf(r));
}
}
}
}
public static void main(String args[]) {
Gaji window2 = new Gaji();
window2.setSize(300, 350);
window2.setVisible(true);
window2.show();
}
}
coba ditaruh di pastebin gan, jadi selebihnya postingan agan buat njelasin dari code yang agan buat.
ReplyDeleteSehingga bisa membantu orang lain untuk belajar java gan :)