Rabu, 25 Januari 2012

TestDepartment

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uas;

//import com.entity.Department;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author
 */
public class TestDepartment {
    public static void main(String[] args) {
        Department test = new Department();
        try {
            test.setDepartmentID(100);
        } catch (Exception ex) {
            System.out.println("salah");
            Logger.getLogger(TestDepartment.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            test.setDepartmentID(0);
        } catch (Exception ex) {
            Logger.getLogger(TestDepartment.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            test.setDepartmentName("personalia");
        } catch (Exception ex) {
            Logger.getLogger(TestDepartment.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            test.setDepartmentName("Eduardus Hardika Sandy Atmaja Eduardus Hardika Sandy Atmaja");
        } catch (Exception ex) {
            Logger.getLogger(TestDepartment.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

OraConnection

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uas;

import java.sql.Connection;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author
 */
public class OraConnection {

    public static Connection open(String url, String userId, String password) {
        Connection conn = null;
        try {
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(url);
            conn = ds.getConnection(userId, password);
            System.out.println("Koneksi is OK");
        } catch (SQLException e) {
            // perform error handling here
            System.out.println("error :" + e.getMessage());
        }

        return conn;
    }
}

Main

package uas;

/**
 *
 * @author
 */
public class Main {

    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
    }
}

Frame

package uas;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author  */
public class Frame extends JFrame implements ActionListener {

    Container contentPane;
    private JMenu dept, help;

    public Frame() {
        setSize(600, 500);
        setLocation(0, 0);
        setTitle("UAS");
        setResizable(true);

        setCursor(Cursor.HAND_CURSOR);

        contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());
        contentPane.setBackground(Color.WHITE);

        createDepartment();
        createHelp();

        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        menuBar.add(dept);
        menuBar.add(help);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        String menuName;
        menuName = e.getActionCommand();

        if (menuName.equals("Exit")) {
            System.exit(0);
        }
        if (menuName.equals("Add")) {

            DepartmentView d = new DepartmentView(this, true);
            d.setVisible(true);
            this.setVisible(false);
        }

    }

    private void createDepartment() {
        JMenuItem item;

        dept = new JMenu("Department");

        item = new JMenuItem("Add");
        item.addActionListener(this);
        dept.add(item);

        item = new JMenuItem("Exit");
        item.addActionListener(this);
        dept.add(item);
    }

    private void createHelp() {
        help = new JMenu("Help");
    }
}


DepartmentView

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DepartmentView.java
 *
 * Created on 20 Des 11, 6:47:57
 */
package uas;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author
 */
public class DepartmentView extends javax.swing.JDialog {

    /** Creates new form DepartmentView */
    public DepartmentView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtDepartmentID = new javax.swing.JTextField();
        txtDepartmentName = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblDepartment = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Department ID");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel2.setText("Department Name");

        txtDepartmentID.setColumns(5);
        txtDepartmentID.setFont(new java.awt.Font("Tahoma", 0, 18));

        txtDepartmentName.setColumns(30);
        txtDepartmentName.setFont(new java.awt.Font("Tahoma", 0, 18));

        btnSave.setFont(new java.awt.Font("Tahoma", 1, 24));
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        tblDepartment.setModel(new DepartmentTableModel());
        jScrollPane1.setViewportView(tblDepartment);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 583, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSave)
                            .addComponent(txtDepartmentID, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtDepartmentName, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtDepartmentID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(txtDepartmentName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnSave)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:      
        Department d = new Department();
        d.add(Integer.parseInt(txtDepartmentID.getText()), txtDepartmentName.getText());
    }//GEN-LAST:event_btnSaveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DepartmentView dialog = new DepartmentView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblDepartment;
    private javax.swing.JTextField txtDepartmentID;
    private javax.swing.JTextField txtDepartmentName;
    // End of variables declaration//GEN-END:variables
}

DepartmentTableModel

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uas;

//import com.entity.Department;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author  */
public class DepartmentTableModel extends AbstractTableModel {

    String columNames[] = {"DepartmentID", "DepartmentName"};
    ArrayList<Department> data;

    public DepartmentTableModel()
    {
         Department r=new Department();
        data = r.read();
    }
    public int getRowCount() {
        return data.size();
    }

    public int getColumnCount() {
        return columNames.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Department temp =  data.get(rowIndex);
        if (columnIndex == 0) {
            return temp.getDepartmentID();
        } else {
            return temp.getDepartmentName();
        }

    }

    @Override
    public String getColumnName(int col) {
        return columNames[col];
    }

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
}

Department

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uas;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author
 */
public class Department {

    private int departmentID;
    private String departmentName;
    private int managerID;
    private int locationID;

    /**
     * @return the departmentID
     */
    public int getDepartmentID() {
        return departmentID;
    }

    /**
     * DepartmentID punya panjang angka maksimal 4 digit atau dari 0001 sd 9999
     * @param departmentID the departmentID to set
     */
    public void setDepartmentID(int departmentID) throws Exception {
        if (departmentID >= 1 && departmentID <= 9999) {
            this.departmentID = departmentID;
        } else {
            throw new Exception("Departemen ID salah");
        }
    }

    /**
     * @return the departmentName
     */
    public String getDepartmentName() {
        return departmentName;
    }

    /**
     * DepartmentName punya panjang karakter sebesar 30.
     * DepartmentName hanya boleh mempunyai unsur alfabet, angka dan karakter lain tidak boleh.
     * @param departmentName the departmentName to set
     */
    public void setDepartmentName(String departmentName) throws Exception {
        if (departmentName.length() <= 30) {
            this.departmentName = departmentName;
        } else {
            throw new Exception("Nama terlalu panjang");
        }
    }

    /**
     * @return the managerID
     */
    public int getManagerID() {
        return managerID;
    }

    /**
     * @param managerID the managerID to set
     */
    public void setManagerID(int managerID) {
        this.managerID = managerID;
    }

    /**
     * @return the locationID
     */
    public int getLocationID() {
        return locationID;
    }

    /**
     * @param locationID the locationID to set
     */
    public void setLocationID(int locationID) {
        this.locationID = locationID;
    }

    /**
     * Fungsi ini untuk membaca tabel departemen
     * @return the list of department
     */
    public static ArrayList<Department> read() {
        ArrayList<Department> dept = new ArrayList<Department>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "mhs105314084", "mhs105314084");
        String sql = "select * from DEPARTMENTS";
        try {
            Statement statement = conn.createStatement();
            ResultSet result = statement.executeQuery(sql);
            while (result.next()) {
                Department temp = new Department();
                temp.setDepartmentID(result.getInt(1));
                temp.setDepartmentName(result.getString("department_name"));
                dept.add(temp);
            }
            conn.close();
        } catch (Exception ex) {
            System.out.println("error : " + ex.getMessage());
        }

        return dept;
    }

    public static boolean add(int id, String name) {
//        ArrayList<Department> depts = new ArrayList<Department>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "mhs105314084", "mhs105314084");
//        String sql = "select * from DEPARTMENTS";
        try {
            Statement statement = conn.createStatement();

            String queries = "INSERT INTO DEPARTMENTS VALUES ('" + id + "','" + name + "')";
            statement.executeUpdate(queries);
//            System.out.println("Data ");
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Department.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
}

Selasa, 24 Januari 2012

TestSoal3

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Soal3;

/**
 *
 * @author admin
 */
public class TesSoal3 {

    public static void main(String[] args) {
        Soal3 frame = new Soal3();
        frame.setVisible(true);
    }
}

TestSoal3

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Soal3;

/**
 *
 * @author admin
 */
public class TesSoal3 {

    public static void main(String[] args) {
        Soal3 frame = new Soal3();
        frame.setVisible(true);
    }
}

Soal3

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Soal3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Eduardus Hardika Sandy A. (1053140084)
 */
public class Soal3 extends JFrame implements ActionListener {

    private static final int lebar = 700;
    private static final int tinggi = 400;
    private static final int x = 0;
    private static final int y = 0;
    Container contentPane;
    private JMenu file;

    public Soal3() {
        setSize(lebar, tinggi);
        setTitle("Password");
        setLocation(x, y);
        setResizable(true);
        setBackground(Color.GRAY);

        contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());

        createfile();

        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        menuBar.add(file);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        String menuName;
        menuName = e.getActionCommand();

        if (menuName.equals("Exit")) {
            System.exit(0);
        }

        if (menuName.equals("Home Password")) {
            Panel j1 = new Panel();
            contentPane.add(j1);
            j1.revalidate();
            j1.setVisible(true);
        }
    }

    private void createfile() {
        JMenuItem item;

        file = new JMenu("File");

        item = new JMenuItem("Home Password");
        item.addActionListener(this);
        file.add(item);

        item = new JMenuItem("Exit");
        item.addActionListener(this);
        file.add(item);
    }
}

Panel

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Soal3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author admin
 */
public class Panel extends JPanel implements ActionListener {

    private JLabel name, paswd;
    private JTextField nm, pw;
    private JButton ok;

    public Panel() {

        setLayout(new GridLayout(2, 1));

        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(2, 2));

        name = new JLabel();
        name.setText("Input your User Name :");

        paswd = new JLabel();
        paswd.setText("Input your Password :");

        nm = new JTextField();
        nm.setColumns(22);
        nm.addActionListener(this);

        pw = new JTextField();
        pw.setColumns(22);
        pw.addActionListener(this);

        p1.add(name);
        p1.add(nm);
        p1.add(paswd);
        p1.add(pw);

        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        ok = new JButton("OK");
        ok.addActionListener(this);
        p2.add(ok);

        this.add(p1);
        this.add(p2);
    }

    public void actionPerformed(ActionEvent e) {
        String button;
        button = e.getActionCommand();

        if (button.equals("OK")) {
            if ((nm.getText().equals("root")) && (pw.getText().equals("123456"))) {
                JOptionPane.showMessageDialog(this, "ok");
            } else {
                JOptionPane.showMessageDialog(this, "Salah");
            }
        }
    }
}

TestKaryawan

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Soal2;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 *
 * @author Eduardus Hardika Sandy A. (1053140084)
 */
public class TesKaryawan {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int tgl, bln, thn;
        String nm;

        try {
            Karyawan k = new Karyawan();

            System.out.print("Masukkan Nama : ");
            nm = input.next();
            k.setNama(nm);
            System.out.print("Masukkan Tanggal : ");
            tgl = input.nextInt();
            k.setTglLahir(tgl);
            System.out.print("Masukkan Bulan : ");
            bln = input.nextInt();
            k.setBlnLahir(bln);
            System.out.print("Masukkan Tahun : ");
            thn = input.nextInt();
            k.setThnLahir(thn);

        } catch (InputMismatchException  f) {
            System.out.println("Salah Format");
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Karyawan

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Soal2;

/**
 *
 * @author Eduardus Hardika Sandy A. (1053140084)
 */
public class Karyawan {

    private String nama;
    private int tglLahir;
    private int blnLahir;
    private int thnLahir;

    public Karyawan() {
    }

    /**
     * @param nama the nama to set
     */
    public void setNama(String nama) {
        this.nama = nama;
    }

    /**
     * @param tglLahir the tglLahir to set
     */
    public void setTglLahir(int tanggal) throws Exception {
        if (tanggal > 0 && tanggal <= 31) {
            this.tglLahir = tanggal;
        } else {
            throw new Exception("Salah Tanggal");
        }
    }

    /**
     * @param blnLahir the blnLahir to set
     */
    public void setBlnLahir(int bulan) throws Exception {
        if (bulan > 0 && bulan <= 12) {
            this.blnLahir = bulan;
        } else {
            throw new Exception("Salah Bulan");
        }
    }

    /**
     * @param thnLahir the thnLahir to set
     */
    public void setThnLahir(int tahun) throws Exception {
        if (tahun > 0 && tahun >= 1900) {
            this.thnLahir = tahun;
        } else {
            throw new Exception("Salah Tahun");
        }
    }
}

TestSoal1

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Soal1;

/**
 *
 * @author Eduardus Hardika Sandy A. (1053140084)
 */
public class TesSoal1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Soal1 frame = new Soal1();
        frame.setVisible(true);
    }

}

TestSoal1

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Soal1;

/**
 *
 * @author Eduardus Hardika Sandy A. (1053140084)
 */
public class TesSoal1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Soal1 frame = new Soal1();
        frame.setVisible(true);
    }

}

Soal1

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Soal1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 *
 */
public class Soal1 extends JFrame implements ActionListener {

    private static final int lebar = 700;
    private static final int tinggi = 400;
    private static final int x = 0;
    private static final int y = 0;
    Container contentPane;
    private JMenu utama_1;
    private JMenu utama_2;
    private JLabel input;
    private JTextField isiinput;
    private JButton ok;

    public Soal1() {
        setSize(lebar, tinggi);
        setTitle("UTS Satu");
        setLocation(x, y);
        setResizable(true);
        setBackground(Color.GRAY);

        contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());

        createUtama_1();
        createUtama_2();

        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        menuBar.add(utama_1);
        menuBar.add(utama_2);

        input = new JLabel();
        input.setText("Input Password");
        contentPane.add(input);

        isiinput = new JTextField();
        isiinput.setColumns(15);
        contentPane.add(isiinput);

        ok = new JButton("OK");
        contentPane.add(ok);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
       
    }

    private void createUtama_1() {
        JMenuItem item;

        utama_1 = new JMenu("Utama 1");

        item = new JMenuItem("Utama 1A");
        item.addActionListener(this);
        utama_1.add(item);

        utama_1.addSeparator();

        item = new JMenuItem("Utama 1B");
        item.addActionListener(this);
        utama_1.add(item);

        utama_1.addSeparator();

        item = new JMenuItem("Utama 1C");
        item.addActionListener(this);
        utama_1.add(item);
    }

    private void createUtama_2() {
        JMenuItem item;

        utama_2 = new JMenu("Utama 2");

        item = new JMenuItem("Utama 2A");
        item.addActionListener(this);
        utama_2.add(item);
    }
}